Java servlet key points in a detailed

Source: Internet
Author: User
Tags http post

Java servlet key points in detail Chszs, reprint to be noted. Blog home: http://blog.csdn.net/chszs1, understanding the life cycle of a servlet

The servlet engine controls the life cycle of the servlet

The life cycle of a servlet is described by the following three methods (five life cycle stages)
1) Initialize init (ServletConfig obj)
2) service services (ServletRequest, Servletresponse)
3) Destroy Destroy ()

When something happens in the servlet's life, the servlet engine invokes these methods on the servlet instance to process it.

2. What is ServletConfig

The servlet uses the request parameters to accept data from the client in order to process the request.
Some specific data must be raised to the servlet when the servlet is initialized, specifically for some clients, but this data is not provided by the client through the request parameters.
During servlet initialization, the initialization parameters are used to provide data to the servlet, and the initialization parameters are set in the Web Deployment descriptor (XML).
These parameters are accessible to the servlet during run time.
Request parameters can be changed from one request to another request.
From one servlet to another, the initialization parameters can also be changed.

3. What is ServletContext

You can deploy multiple Web apps in one servlet container.
Each Web application contains its own resources in a separate environment, which is called the context of the Web application or called ServletContext.
A resource that belongs to a web app context is not valid for the context of another web app.
A servlet context contains 0 to more servlets, and for each Servlet object, the servlet container creates a separate ServletConfig object for it.

4. Can I define a construction method in a servlet?

Yes. In the servlet this can define the constructor method, but we cannot explicitly call this constructor because this is the work of the servlet container. The servlet container creates a Servlet object, and the construction method is, of course, called by the servlet container.

5. Can I call the Destroy () method within the initialization init () method of the servlet? What's going to happen?

Yes, you can call the Destroy () method within the initialization init () method of the servlet.
In fact, if there is nothing to do in the Init () method, the container automatically calls the Destroy () method.
If we rewrite the Destroy () method and call it in the Init () method, then 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; supports the public void service (ServletRequest req,servletresponse res) method.

2) HttpServlet
It is also an abstract class, it relies on the HTTP protocol, is a subclass of the Genericservlet class, supports the public void service () method, and the protected Void Service () method, the Doget () method, the DoPost () method, DoPut () method, DoDelete () method, Dohead () method, Dotrace () method, Dooptions () method, and so on.

7. What is the difference between the Doget () method and the Dopost () method

1) doget () method
The complete method protected void doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, Java.io.IOException;
The HTTP GET request is processed;
After the request parameter is added to the URL, the header information is sent;
Request parameters cannot be encrypted;
The maximum data length sent using the Doget () method cannot exceed 240 bytes.

2) DoPost () method
The complete method protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException;
The HTTP POST request is processed;
Request parameters are submitted as forms;
Request parameters can be encrypted;
There is no limit to the length of data sent using the Dopost () method.

8. Can I call another servlet in one servlet?

Yes, you can call another servlet in one servlet, also known as a communication between servlets.
Communication between servlets can be done by using the RequestDispatcher object.

RequestDispatcher rd=request.getRequestDispatcher("other servlet url name");rd.forward(req, res);
9. What is the difference between the forward () method and the Sendredirect () method?

1) Forward () method
Example code:

request.getRequestDispathcer("example.jsp").forward(request, response);

Used to forward requests to the available resources within the server domain.

2) Sendredirect () method
Example code:

response.sendRedirect("http://www.instanceofjava.com");

Used to redirect requests to other resources or domains.

10. How can I get information about another servlet in the context of a servlet?

Use the SetAttribute () method to obtain information for another servlet:

Context.setAttribute("name", "value")Context.getAttribute("name")

Java servlet key points in a detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.