First, Initial introduction:
The Init method is designed to be called only once. It is called the first time the Servlet is created, and is no longer invoked on subsequent user requests.
The service () method is the primary way to perform the actual task. The Servlet container (that is, the WEB server) invokes the service () method to process requests from the client (browser) and writes the formatted response back to the client.
Each time the server receives a Servlet request, the server generates a new thread and invokes the service. The service () method checks the HTTP request type (GET, POST, PUT, DELETE, and so on) and calls Doget, DoPost, Doput,dodelete, and so on, when appropriate.
The service () method is called by the container, and the service method calls Doget, DoPost, DoPut, DoDelete, and so on when appropriate. Therefore, you do not need to do any action on the service () method, simply overload doget () or DoPost () based on the request type from the client.
The Doget () and DoPost () methods are the most commonly used methods in each service request: they are used to respond to get and post requests from the client, namely the browser side, respectively.
In the doget () and DoPost () methods, it is common to call
HttpServletRequest request,httpservletresponse Response
Use the methods in these two objects to achieve the desired functionality.
eg
public void doget (HttpServletRequest request, httpservletresponse response) throws Servletex Ception, IOException {//Set response content type Response.setcontenttype ("text/html"); The actual logic is here printwriter out = Response.getwriter (); Out.println ("
In many cases, it is necessary to pass some information, from the browser to the WEB server, eventually to the background program. The browser uses two methods to pass this information to the WEB server, respectively, the GET method and the POST method.
The GET method sends an encoded user information to a page request. Between the page and the encoded information? The characters are separated as follows:
Http://www.test.com/hello?key1=value1&key2=value2
The Post method packages information in the same way as the GET method, but the Post method does not use the information as a URL? The text string after the character is sent, but the information is used as a separate message. Messages are sent to the daemon in the form of standard output, which you can parse and use with these standard outputs. The Servlet uses the DoPost () method to handle this type of request.
reading form data using a ServletThe Servlet processes the form data, which is automatically parsed according to different scenarios using different methods:
GetParameter (): Call the Request.getparameter () method to get the value of the form parameter.
Getparametervalues (): If the parameter appears more than once, the method is called and multiple values, such as check boxes, are returned.
Getparameternames (): Call this method if you want to get a complete list of all the parameters in the current request.
Servlet Learning Notes Experience