Servlet is responsible for receiving user requests and distributing processed requests. Each servlet class must inherit the httpservlet class. In
Five methods are defined in the servlet interface, three of which represent the servlet declaration cycle:
Init method, responsible for initializing servlet objects
Service method, responsible for the request of the corresponding customer
Destory method. When the servlet Object exits the Declaration cycle, it is responsible for releasing the occupied resources.
The servlet container is responsible for creating the httpservlet object and encapsulating the HTTP request directly into the httpservlet object, which greatly simplifies
The amount of work required for HTTP servlet to parse request data. The process for the httpservlet container to respond to a Web client request is as follows:
1) the Web Client sends an HTTP request to the servlet container;
2) The servlet container parses the HTTP request of the Web Client;
3) The servlet container creates an httprequest object, which encapsulates HTTP request information;
4) Create an httpresponse object in the servlet container;
5) The servlet container calls the service method of httpservlet and uses the httprequest and httpresponse objects as the service method
Parameters are passed to the httpservlet object;
6) httpservlet calls the httprequest method to obtain the HTTP request information;
7) httpservlet calls httpresponse methods to generate response data;
8) the servlet container sends the HTTP servlet response result to the Web Client.
The preceding request process shows that the service method is responsible for all user requests, including Delete, get, options, post, and put.
And trace, and then the service forwards different requests to different request functions, including dodelete (), doget (), dooptions
(), Dopost (), doput (), and dotrace (). Therefore, there are two ways to handle different user needs. One is to directly use the service method
And overwrite the odelete (), doget (), dooptions (), dopost (), doput (), and dotrace () methods.
Note that to process requests in the service, the method of the parent class cannot be implemented, that is, the super () cannot be written, otherwise the request will be automatically distributed,
The processing part of your service is useless.
Appendix: differences between GET requests and post requests
1. Http defines different methods for interaction with the server. The most basic methods are get and post. In fact, get applies to most requests.
While the reserved post is only used to update the site. According to HTTP specifications, get is used for information retrieval and should be secure and idempotent.
. The so-called security means that this operation is used to obtain information instead of modifying information. In other words, get requests generally do not have side effects. Idempotence
This means that the same results should be returned for multiple requests of the same URL. The complete definition is not as strict as it looks. Fundamentally speaking
The goal is that when a user opens a link, she can be confident that the resource has not changed from her own perspective. For example, the front page of a news site
Update constantly. Although the second request will return a different batch of news, this operation is still considered safe and idempotent because it always returns
Return to the current news. And vice versa. POST requests are not that easy. Post indicates a request that may change resources on the server. Still
Taking a news site as an example, the reader's Annotation of the article should be implemented through the POST request, because after the annotation is submitted, the site is already different (compared
Fang said that an annotation appears below the article );
2. If method is not specified during form submission, the default value is get. The data submitted in form will be appended to the URL,
To? Separated from the URL. The letter and digit characters are sent as they are, but spaces are converted to "+". Other symbols are converted to % XX, where XX is the character.
ASCII (or ISO Latin-1) value in hexadecimal notation. For GET requests, place the data submitted in the HTTP Request Header, while for post
The submitted data is stored in the object data;
3. The data submitted in get mode can only contain a maximum of 1024 bytes, whereas the POST method does not.