JSP-11-Servlet, jspservlet
1. Get to know what Servlet has done
 
Not doing business
 
Only receive requests and decide which JavaBean to call to process requests
 
Determine which page is used to display the returned data.
Ø what is Servlet? 
Servlet is a Java program that runs on the server side to process client requests and respond.
 
Server + Applet is a Server-side Java application.
 
Only when a server-side program uses the servlet API can the server-side program be called Servlet.
Ø ServletAPI introduction API: Application Interface 
Javax. servlet. Servlet
 
All Java Servlet basic interface classes. Specifies the set of methods that must be implemented by specific Servlet classes.
 
Javax. servlet. GenericServlet
 
It is a common Servlet version and is a protocol-independent Servlet.
 
Javax. servlet. http. HttpServlet
 
Http-based Servlet extended based on GenericServlet. This type is often inherited in programs. It should be noted that the subclass of Httpservlet should at least rewrite one doGet () and doPost () in the following method ().
 
 
Ø Servlet Lifecycle 
The Servlet container is responsible for loading and instantiating Servlet. Servlet containers are sometimes called Servlet engines. They are part of Web servers or application servers and are used to provide network services between requests and responses. Here we can understand the Servlet container as Tomcat.
Load and instantiate 
When the Servlet container is started or the container detects a client request, load and instantiate the request.
 
The Servlet container will check whether the Servlet instance exists in the memory. If it does not exist, it will be created. If it exists, it will be taken directly from the memory.
Initialize the init () method 
After instantiation, the container calls Servlet's init () to initialize the object.
Provide service and request processing service () 
After initialization, the Servlet is ready to respond to the request. In this case, the client request is obtained and processed. (Multi-threaded response ).
Destroy () method 
When the Servlet object in the program is no longer used or the Web service stops running
 
 
Main Methods of Servlet 
 
  
   
   | Init () | Servlet initialization method, only once | 
 
   
   | Service () | Process requests and generate responses | 
 
   
   | Destroy () | It is called only once when the server is stopped and the Servlet objects in the program are no longer used. | 
 
   
   | ServletRequest | Encapsulate customer request information | 
 
   
   | ServletResponse | Create a response and return the response to the client. | 
 
   
   | ServletConfig | Includes Servlet initialization parameter information | 
 
  
 
 
User who processes the Get/Post request HttpServletRequest request object by Servlet 
// Obtain parameters
 
Request. getParameter ("param ");
 
// Convert the input content to Chinese
 
Request. setCharacteEncoding ("GBK ");
 
Request. setAttribute ("loginname", user );
Common Methods for HttpServletResponse response objects 
// Solve Chinese garbled characters
 
Response. setContentType ("text/html; charset = GBK ");
 
Response. sendRedirect ("url ");
Forwarding 
RequestDispatcher dispatcher = request. getRequestDispatcher (url );
 
First, define the forwarder. The parameter url is the URL of the page or Servlet forwarded by the fish.
 
Dispatcher. forward (request, response );
 
Then the forwarding function is implemented. The parameters are request and response objects.
14.2 Servlet deployment and Configuration 
Web. xml of the current project
 
<Web-app>
 
<Servlet>
 
<Servlet-name> custom name </servlet-name>
 
<Servlet-class> actual servlet class location </servlet-class>
 
</Servlet>
 
<Servlet-mapping>
 
<Servlet-name> same as the custom name </servlet-name>
 
<Url-pattern> Servlet URL accessed by the user </url-pattern>
 
</Servlet-mapping>
 
</Web-app>