0

I am trying to create a simple servlet app, but when I deploy it into my Tomcat server (8.5), localhost:8080 gives me 405 method not allowed error. I don't know to handle this error

enter image description here

my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.devcolibri</groupId>
    <artifactId>com.devcolibri.servlet</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>

</project>

My MainServlet.java

    package com.devcolibri.servlet;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    public class MainServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        PrintWriter out = resp.getWriter();
        out.print("<h1>Hello Servlet</h1>");

    }
}

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <servlet>
        <servlet-name>mainServlet</servlet-name>
        <servlet-class>com.devcolibri.servlet.MainServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>mainServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
socm_
  • 783
  • 11
  • 28
  • How are you trying to access your servlet? Just by visiting it's URL in your browser? Or are you trying to post form data to it? – Jon Sampson Mar 22 '17 at 19:43
  • @JonSampson yep, just by visiting it's URL – socm_ Mar 22 '17 at 19:46
  • I'm not seeing a problem with what you have posted, other than maybe you can try adding `provided` to your `javax.servlet` dependency in the `pom.xml`. 405 is returned when you try to access a servlet resource that isn't implemented to return your request. So if you'd done a POST/PUT/HEAD to your URL you would see that. You implemented doGet and your browser should be doing a GET, so. . . . – Jon Sampson Mar 22 '17 at 20:07
  • Give your question a proper title that actually expresses the probem. If you want attention from people who know the answer, that is. – user207421 Mar 23 '17 at 00:33

2 Answers2

0

I think I've been missing the obvious: I suspect that your war file is not being deployed on slash. It may be deployed under your artifact ID. To deploy it on the root of the host requires more configuration. Try http://localhost:8080/com.devcolibri.servlet/

Edit:

or http://localhost:8080/com.devcolibri.servlet-1.0-SNAPSHOT/ It should be your war file name.

Jon Sampson
  • 1,473
  • 1
  • 21
  • 31
-2

It looks like the way to access of the servlet is not working with protected type of doGet method. Please try doGet as public.

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    PrintWriter out = resp.getWriter();
    out.print("<h1>Hello Servlet</h1>");

}

You can refer here for more information about protected and public: what is the difference between public service() method and protected service() method of httpservlet

Community
  • 1
  • 1
Arindam
  • 555
  • 1
  • 8
  • 24
  • This may not be the right avenue. `doGet` is protected and there is only a single version of it in `HttpServlet`. We're not dealing with the `service` method in this case. See [Java Servlet return error 405 (Method Not Allowed) for POST request](http://stackoverflow.com/questions/19965253/java-servlet-return-error-405-method-not-allowed-for-post-request) – Jon Sampson Mar 22 '17 at 20:23
  • @Jon Sampson, if you see the comment you can see clearly that works for both public and protected, the issue was only with the IllegalStateException and once this exception removed, both works fine. Please correct me if I am wrong. – Arindam Mar 22 '17 at 20:28
  • I'm hesitant due to the fact I have deployed servlets using `protected`. You may be correct in this case, however. We'll see what @socm_ says. – Jon Sampson Mar 22 '17 at 20:30
  • thank you very much, my war file was built up incorrectly. protected doGet function was correct @JonSampson – socm_ Mar 29 '17 at 14:14