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.
That should have been HttpServletRequest and HttpServletResponse, just like in doPost.
Always use @Override, that would have told you about this error.