Alejandro
Alejandro

Reputation: 3082

"HTTP 405" when creating Servlet? (doPost and doGet implemented)

I have a servlet named HelloServlet..

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class HelloServlet extends HttpServlet
    {
public void doGet(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{


        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        pw.println("<html>");
        pw.println("<head>");
        pw.println("<title> Hello World </title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<h1> Hello, World!</h1>");
        pw.println("</body>");
        pw.println("</html>");
        pw.close();




}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
    doGet(request, response);
}
    }

I compile it to a .class file and i place it in my tomcat directory under C:/tomcat/webapps/ALTest1/WEB-INF/classes/ - I made the servlet mapping (I put the url-pattern as /hi) and servlet entry for it in C:/tomcat/webapps/ALTest1/WEB-INF/web.xml

Everything works fine but when I go to my browser to access it at http://localhost:8080/ALTest1/hi I get an error message saying:

HTTP STATUS 405 - HTTP METHOD GET is not supported by this URL

...which is strange, because I have BOTH doPost and doGet methods in my HelloServlet.class file. So, even if doGet isn't supported, I have the doPost method to take care of POST yet it still isn't working.

Upvotes: 2

Views: 1051

Answers (2)

Steven You
Steven You

Reputation: 459

It seems there is no package for your servlet java class. For normal java programs running throw main methord, if there is no package specified the java class is in default package. However, for web application, you need to specify the package name in the web.xml.

Upvotes: 0

Thilo
Thilo

Reputation: 262584

 public void doGet(ServletRequest request, ServletResponse response)

That should have been HttpServletRequest and HttpServletResponse, just like in doPost.

Always use @Override, that would have told you about this error.

Upvotes: 8

Related Questions