Java Servlet key points
Detailed explanation of the key points of Java Servlet: chszs, reprinted must be noted. Blog homepage: http://blog.csdn.net/chszs1?#servlet's Life Cycle
The Servlet engine controls the Servlet lifecycle.
The Servlet lifecycle is described by the following three methods (five lifecycle stages)
1) initialize init (ServletConfig obj)
2) service (servletRequest, servletResponse)
3) destroy ()
If something happens during the Servlet lifecycle, the Servlet engine will call these methods on the Servlet instance for processing.
2. What is ServletConfig?
Servlet uses request parameters to accept data from the client for processing requests.
Some specific data must be raised to the Servlet during Servlet initialization. The data is specially targeted at some clients, but the data is not provided by the client through request parameters.
During Servlet initialization, data is provided to the Servlet using initialization parameters. The initialization parameters are set in the Web deployment descriptor (web. xml.
Servlet can access these parameters during running.
From one request to another, the request parameters can be changed.
From a Servlet to another Servlet, the initialization parameters can also be changed.
3. What is ServletContext?
You can deploy multiple Web applications in a Servlet container.
Each Web application contains its own resources in a separate environment, which is called the context of a Web application or ServletContext.
Resources belonging to a Web application context are invalid for the context of another Web application.
A Servlet context contains zero to multiple servlets. For each Servlet object, the Servlet container creates a separate ServletConfig object for it.
4. Can I define the constructor in Servlet?
Yes. Servlet can define the constructor, but we cannot explicitly call this constructor, because this is the work of the Servlet container. The Servlet container will create a Servlet object. The construction method is also called by the Servlet container.
5. Can I call the destroy () method in the initialization init () method of Servlet? What will happen?
Yes, you can call the destroy () method in the initialization init () method of Servlet.
In fact, if there is nothing to do in the init () method, the container will automatically call the destroy () method.
If we override the destroy () method and call it in the init () method, the destroy () method will be executed.
6. What is the difference between GenericServlet and HttpServlet?
1) GenericServlet
Is an abstract class. It is independent of the Protocol. It is a subclass of the Servlet class. It supports the public void service (ServletRequest req, ServletResponse res) method.
2) HttpServlet
It is also an abstract class. It depends on the Http protocol. It is a subclass of the GenericServlet class. It supports the public void service () method, the protected void service () method, the doGet () method, and the doPost () method () method, doPut () method, doDelete () method, doHead () method, doTrace () method, doOptions () method, and so on.
7. What are the differences between the doGet () method and the doPost () method?
1) doGet () method
Complete method protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, java. io. IOException;
Http get requests are processed;
After the request parameters are added to the URL, they are sent using the Header information;
Request Parameters cannot be encrypted;
The maximum data length sent using the doGet () method cannot exceed 240 bytes.
2) doPost () method
Complete method Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
It processes http post requests;
Request Parameters are submitted as forms;
Request parameters can be encrypted;
The length of data sent using the doPost () method is unlimited.
8. Can I call another Servlet in one Servlet?
Yes, you can call another Servlet in one Servlet, that is, the communication between Servlet.
You can use the RequestDispatcher object to implement inter-Servlet communication.
RequestDispatcher rd=request.getRequestDispatcher("other servlet url name");rd.forward(req, res);
9. What are the differences between the forward () method and the sendRedirect () method?
1) forward () method
Sample Code:
request.getRequestDispathcer("example.jsp").forward(request, response);
It is used to forward requests to available resources in the server domain.
2) sendRedirect () method
Sample Code:
response.sendRedirect("http://www.instanceofjava.com");
Redirects requests to other resources or domains.
10. How can I obtain the information of another Servlet in the context of a Servlet?
You can use setAttribute () to obtain the information of another Servlet:
Context.setAttribute("name", "value")Context.getAttribute("name")