Miscellaneous: Servlet (2), miscellaneous servlet
Servlet method analysis:
1. What is done in the service () method?
2. What does doGet () and doPost () do? What should I do?
Answer
1. What is done in the service () method?
If your service method does not contain super. service (req, resp), the consequence is that the doget () and dopost () methods will never be called. Why?
Let's take a look at the source code of HttpServlet: (Ps: deleted, 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, call doget. Otherwise, check whether it is post.
Long lastModified = getLastModified (req );
If (lastModified =-1 ){
DoGet (req, resp); // call
} Else if (method. equals (METHOD_POST )){
DoPost (req, resp );
... Is followed by an if-else statement.
From this source code, we can see what the Service () mainly does. ----- It is used to distinguish whether it is a get method or a post method.
2. What does doGet do?
Please check if this code is correct. Will there be exceptions .....
The answer is: there will be exceptions, because you wrote a super. doGet ();
This will call the doGet () method of the parent class. The result is: 405 (request error) or illegalXXXException.
Why? Why can't doget () call the method of the parent class?
The reason is:
This source code tells us that, in any case, he will call the requset. sendError method,
If you still want to get the parameter under super. doGet (), it is wrong. Because the context has changed after sendError, you cannot get the parameter;
For example:
This:
After the call:
------- Makeup ------------------------------------------------
/**
* Es 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 is an HTTP-specific version of
* {@ Link javax. servlet. Servlet # service} method. There's no
* Need to override this method.
*
* @ Param req the {@ link HttpServletRequest} object that
* Contains the request the client made
* 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
* 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 will always be 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 was requested, anywhere on 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 );
}
}