The Servlet method anatomy:
What does the 1.service () method do?
What does 2.doGet () and Dopost () do?
Reply
What does the 1.service () method do?
If your service method does not write Super.service (req, resp); So the consequence is that the doget () and Dopost () methods are never called. Why is that?
We can check the source code of HttpServlet: (Ps: Has been deleted, the complete code in the record ...)
------------------------Service ()--------------------------------------------------
protected void Service (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
String method = Req.getmethod ();
if (Method.equals (Method_get)) {//if "request" is a GET method, then call Doget, otherwise see if it is post
Long lastmodified = getlastmodified (req);
if (lastmodified = =-1) {
Doget (req, resp);//Call
}else if (method.equals (method_post)) {
DoPost (req, resp);
....... There's a if-else statement on the back.
From this source code you can see that the service () is basically what it is-----He is used to distinguish between a GET method or a POST method
What did 2.doGet do in there?
Next sell a Xiaoguanzi, please see this code is right, will not appear abnormal .....
The answer is: there must be an exception, because you wrote a super.doget ();
This invokes the Doget () method of the parent class, which results in a 405 (request error) or illegalxxxexception
Why? Why can't doget () call the method of the parent class?
The reason is:
This source code is telling us: Anyway, he will call the Requset.senderror method,
If you still want to get the parameters under Super.doget () that's wrong, because the context has changed since senderror, you can't get the parameters;
For example:
This one:
Then after the call:
-------------------------------------------------------
/**
* Receives standard HTTP requests from the public
* <code>service</code> method and Dispatches
* them to the <code>do</code><i>Method</i> methods defined in
* This class. This method was an http-specific version of the
* {@link Javax.servlet.servlet#service} method. There ' s no
* Need to override the This method.
*
* @param req The {@link HttpServletRequest} object that
* Contains the request the client made of
* The servlet
*
* @param resp the {@link HttpServletResponse} object that
* Contains the response the servlet returns
* To the Client
*
* @exception IOException If an input or output error occurs
* While the servlet is handling the
* HTTP Request
*
* @exception Servletexception if the HTTP request
* Cannot be handled
*
* @see Javax.servlet.servlet#service
*/
protected void Service (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
String method = Req.getmethod ();
if (Method.equals (Method_get)) {
Long lastmodified = getlastmodified (req);
if (lastmodified = =-1) {
Servlet doesn ' t support if-modified-since, no reason
To go through further expensive logic
Doget (req, resp);
} else {
Long ifmodifiedsince;
try {
Ifmodifiedsince = Req.getdateheader (header_ifmodsince);
} catch (IllegalArgumentException iae) {
Invalid Date Header-proceed As if none was set
Ifmodifiedsince =-1;
}
if (Ifmodifiedsince < (lastmodified/1000 * 1000)) {
If The servlet mod time is later, call Doget ()
Round down to the nearest second for a proper compare
A Ifmodifiedsince of-1 would always is less
Maybesetlastmodified (resp, lastmodified);
Doget (req, resp);
} else {
Resp.setstatus (httpservletresponse.sc_not_modified);
}
}
} else if (Method.equals (Method_head)) {
Long lastmodified = getlastmodified (req);
Maybesetlastmodified (resp, lastmodified);
Dohead (req, resp);
} else if (Method.equals (method_post)) {
DoPost (req, resp);
} else if (Method.equals (method_put)) {
DoPut (req, resp);
} else if (Method.equals (Method_delete)) {
DoDelete (req, resp);
} else if (Method.equals (method_options)) {
Dooptions (REQ,RESP);
} else if (Method.equals (Method_trace)) {
Dotrace (REQ,RESP);
} else {
//
Note that this means NO servlet supports whatever
method is requested, anywhere on the this server.
//
String errmsg = lstrings.getstring ("http.method_not_implemented");
object[] Errargs = new Object[1];
Errargs[0] = method;
ErrMsg = Messageformat.format (errmsg, Errargs);
Resp.senderror (httpservletresponse.sc_not_implemented, errmsg);
}
}
Talk: Servlet (2)