The first Servlet program code is as follows, followed by this small program to step through.
Importjava.io.IOException; ImportJava.io.PrintWriter; Importjavax.servlet.ServletException; ImportJavax.servlet.annotation.WebServlet; ImportJavax.servlet.http.HttpServlet; Importjavax.servlet.http.HttpServletRequest; ImportJavax.servlet.http.HttpServletResponse; @WebServlet ("/hello.view") Public classHelloServletextendsHttpServlet {protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html;charset=utf-8"); //Get response Output ObjectPrintWriter out=Response.getwriter (); //GET Request ParametersString name= Request.getparameter ("name"); Out.println ("); Out.println ("); Out.println ("<title>hello servlet</title>"); Out.println ("); Out.println ("<body>"); Out.println ("); Out.println ("</body>"); Out.println ("); Out.close (); }}
The example inherits the HttpServlet and redefined the Doget () method, which is called when the browser GET method sends the request.
On the Doget () method, you can see that the HttpServletRequest and httpservletresponse two parameters, when the container receives the HTTP request from the client, collects the information in the HTTP request and creates a Java object that represents the request and response, respectively. The two objects are then passed in as arguments when calling Doget (). Information about the HTTP request can be obtained from the HttpServletRequest object, in the example through HttpServletRequest's GetParameter () and specifying the request parameter name to obtain the request parameters sent by the user.
Because the HttpServletResponse object represents a response to the client, it is possible to set the correct content type through its setcontenttype (). The above example tells the browser that the returned response is to be parsed with text/html, while the character encoding taken is UTF-8. The Getwriter () method is then used to obtain the PrintWriter object that represents the output of the response, using the PrintWriter println () method to output the text of the response to the browser, in the example output HTML and according to the user voice hello!
The example inherits the HttpServlet and redefined the Doget () method, which is called when the browser GET method sends the request.
Think further: Why redefine doget () after inheriting HttpServlet? Why does the HTTP request automatically call Doget () when it is GET? Let's start with the relevant API frame composition seen in the example:
First, you see the servlet interface, which defines the basic behavior that the servlet should have. For example, the init (), Destroy () method associated with the Servlet life cycle, the service () method to be used to provide services, and so on.
The class that implements the Servlet interface is the Genericservlet class, which implements the ServletConfig interface, encapsulates the container call to the Init () method, which is the incoming ServletConfig instance, and the service () method is directly labeled Abstract without any implementation.
Genericservlet does not regulate any related methods of HTTP, but is defined by the HttpServlet that inherits it. When the Servlet was originally defined, it was not qualified to be used only for HTTP, so the HTTP-related service process was not defined in Genericservlet, but was defined in the service () method of HttpServlet.
The process in HttpServlet's service () method is as follows:
Protected void service (HTTPSERVLETREQUEST&NBSP;REQ,&NBSP;HTTPSERVLETRESPONSE&NBSP;RESP) throws ServletException, IOException { string method = req.getmethod (); if (Method.equals ( Method_get) { // slightly ... doget (REQ,&NBSP;RESP); // slightly ... } else if (Method.equals (method_head)) { // dohead (REQ,&NBSP;RESP); } else if (Method.equals (method_post)) { // dopost (REQ,&NBSP;RESP); } else if (Method.equals (method_put)) { // slightly ... }}
When the request arrives, the container invokes the Servlet's service () method. As you can see, the HttpServlet service () is basically the way to judge HTTP requests, and then call Doget (), DoPost (), and so on, so if you want to deal with GET, post, and so on, you only need to inherit After HttpServlet, redefine the corresponding doget (), DoPost () method.
Using Webservlet in Servlet 3.0, annotations (Annotation) can be used to inform the container which servlets provide services and additional information. For example:
@WebServlet ("/hello.view") public class HelloServlet extends HttpServlet {
As long as @WebServlet annotations are set on the Servlet, the container automatically reads the information in it. The above information tells the container that if the requested URL is "/hello.view", the service is provided by an instance of HttpServlet.
You can use @webservlet to provide more information.
@WebServlet {name= "Hello", urlpatterns={"/hello.view"}, Loadonstartup=1}public class HelloServlet extends Httpse Rvlet {
The @WebServlet above is to inform the container that the name of the Servlet is HttpServlet and is specified by the Name property. If the URL requested by the customer is/hello.view, it is handled by a Servlet with the Hello name, which is specified by the Urlpattern. If no property is set, the default value is the full name of the class for the Servlet.
After the application is started, all Servlet instances are not created. The container instantiates the corresponding servlet class, initializes the operation, and then processes the request when it first requests a servlet service. This means that the first request must wait for the Servlet class to instantiate, initialize the action.
If you want the application to start by loading, instantiating, and initializing the Servlet class first, you can use the loadonstartup setting. Setting a value greater than 0 (the default is-1) means that the servlet is initialized when the application is started (instead of instantiating several servlets). the smaller values are initialized first. If more than one servlet uses the same number when setting Loadonstartup , the container implements the manufacturer's own decision on which Servlet to load.
SERVLET&JSP Study notes: The first servlet program