Understand doPost() and doGet() parameters
Hi,
I've realized a web project using JSP and servlet;
All works fine!
But I want to better understand what happens when doPost or doGet method is invoked;
that is, a simple servlet look like this:
publicclass SimpleServlet
extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet{
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
this.doPost(request, response);
}
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
}
My questions:
1) AsHttpServletRequest andHttpServletResponse are interfaces, therequestandresponse parameter are interfaces?
or Does it meanrequest andresponse must be objects of a Class that implements, respectively,HttpServletRequest andHttpServletResponse?
2)Then, who does invoke doGet() and doPost() methods?
The webserver?
3)What way does the caller use in order to create request and response objects, that must be passed to doPost and doGet?
I hope it's clear.
Thank you in advance.
MargNat
[1748 byte] By [
MargNata] at [2007-11-27 3:28:42]

# 1
HttpServlet#doGet is executed when a HTTP GET request is invoked. For example a plain link or a <form method=get> submit. The request parameters are visible in the URI query string, for example http://example.com/webapp/page.jsp?param1=value1¶m2=value2
HttpServlet#doPost is executed when a HTTP POST request is invoked. For example a <form method=post> submit. The request parameters are not visible in the URI query.
# 2
Well i'm not a big expert in webservers but i'd like to share the little knowledge i have.
here is what happens ?
Whenever a webserver / appserver starts it starts a ServletContainer which cud be addressed as a Kernel to a Webserver.
whenever a ServletContainer Starts it loads(deploys) all the webapplications (creating ApplicationContext / ServletContext for all these appln's) into Memory & consequently reads the the deployment descriptors of every webapplication and instantiates a Singleton Object of each every servlet mentioned inside the deployment descriptor(web.xml).
Just for an example the below are the set of classes which are responsible for the following action in context of TOMCAT
http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/package-summary.html
http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/deploy/package-summary.html
http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/core/package-summary.html
Now as per deployment descriptor the servlet container either calls init(ServletConfig) method @ the load of Application or calls it it whenevr the first request is being made by a 'X' client.
Now here is where the real thing happens whenever a request is made to a Servlet @ the client side the Request would be Marshalled along with other set of Things like Protocol,Application Port,Hostname,ApplicationContext refered to,Service / Servlet,Request Parameters inform of a Map<Key,Value>,Request method GET / POST, enctype and etc.... and then places the request through a Socket.
at the server side the ServletContainer recieves the Marshalled Object & then Unmarshalls it and then maps it to an instance of a Class which implements HttpServletRequest / ServletRequest interfaces.
in context of Tomcat you may refer to
http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/connector/Request.html
after creating the mapped request object it then creates a dummy instance of a class which implents HttpServletResponse / ServletResponse Object.
http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/connector/Response.html
and then depending of the mappings metioned under deployment descriptors it calls respective servlet instance's service() or doXX() (could be doGet(),doPost(),doPut(),doTrace(),doDelete(),doHead(),doOptions()) & passes the created Instances of Request & Reponse Implementation classes method depending on the request which was at the client side.
please refer to HttpServlet interface for more insight information.
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServlet.html
now depending the on the response params we design as per our choice the ServletContainer Marshalls the Reponse & then sends to the client side and at the client side the Marshalled is Unmarshalled and expressed as the stated response.
and when the application server is shutdown it calls destroy method of all the descripted servlets and then nullifies the created instances from memory and then ends the JVM.
Hope that answers all your questions. If that doesn't i'd request you to read more about Webservers by getting some useful information by googling and if you are targeting Tomcat i think Tomcat Core API docus could very well be helpful
http://tomcat.apache.org/tomcat-6.0-doc/api/index.html
http://www.coreservlets.com/Apache-Tomcat-Tutorial/
Hope that might help :)
REGARDS,
RaHuL
# 3
Answering your questions:
1) As HttpServletRequest and HttpServletResponse are interfaces, the request and response parameter are interfaces?
or Does it mean request and response must be objects of a Class that implements, respectively, HttpServletRequest and HttpServletResponse?
The latter. The objects passed to this method are of a class that implements the interface HttpServletRequest/HttpServletResponse.
2)Then, who does invoke doGet() and doPost() methods?
The webserver?
If you look at the API a bit more you will see that there are a parallel set of Servlet/Request/Response. One for generic servlets (javax.servlet) and another for HttpServlets (javax.servlet.http).
HttpServlet extends the standard servlet.
Standard servlet implements an interface, which has a single entry point: service(request, response)
The dipatching to the correct method doGet/doPost is actually done by the overridden service method in HttpServlet.
ie the HttpServlet service method looks something like
public void service(HttpServletRequest req,HttpServletResponse resp){
if (is GET request){
doGet(req, resp);
}
else if (is POST request){
doPost(req, resp);
}
}
3)What way does the caller use in order to create request and response objects, that must be passed to doPost and doGet?
The "caller" in this case would be the servlet engine.
The servlet engine receives an incoming HTTP request.
It reads that request, and creates an HttpServletRequest object to hold all the data that was sent with it, and then calls the service() method of the appropriate servlet.
All of this is internal to the servlet engine, and you don't need to know about it.
# 4
web container calls doGet() and soPost() methods in response to incoming requests...
doGet() will be called if client makes a get request to a servlet.. all request parameters( passed as query string will be available as request onject)
doPost() will be called if client makes post method (form method="post")
all form parameters will be available as request object
Hope tht helps,
Regards
Sudhir
http://www.jyog.com