• CONTEST ALERT - Experience the power of DDR5 memory with Kingston Click for details
Status
Not open for further replies.

karthik_rcs

Broken In
Let us assume i am request from a HTML page using GET METHOD in FORM ACTION parameter.
<FORM action = "HelloWorld">
HelloWorld is the Servlet class.
The code for HelloWorld is as follows.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
PrintWriter pw = response.getWriter();
pw.println("hi");

}
}


My Question is as follows. I am requesting from HTML using GET METHOD.
When the servlet gets initialized and service method is invoked on the servlet instance,
the service method calls the doPost or doGet based on my Http Method.

When i tried deploying this in Tomcat, i got HTTP 405 Error
HTTP method GET is not supported by this URL.

Why should i get this error when there is doGet of HttpServlet class and overrided doPost method in HelloWorld.


doPost has nothing to do in this situation as HTTP METHOD used is GET.
By the rule of inheritance we have doGet of HttpServlet in HelloWorld.
 

enoonmai

Cyborg Agent
The basic rule of a servlet is that whatever form action you use, be it a GET or a POST, it has to be overridden in the servlet code. You're getting that error because what a form action code does is speficy an action attribute that specifies the program on the server that will handle the form data, and also, the type of request that will be handled. So when the servlet executes, the form action uses the GET method (which means that the data is appended to the URL) to submit the data to the servlet, which is told to listen ONLY for a POST method (where in data is sent separately to the servlet and is not appended in the URL)
So, even if by hierarchy and inheritance, you would expect the servlet to handle the GET method, you've overridden just the doPost() method. You have to override it in your servlet, whether its a doGet() or a doPost(), for it to work. Which is why you're getting the error that the GET method is not supported by this URL. Hope that makes it clearer.
 
Status
Not open for further replies.
Top Bottom