Servlet-another common component in Java EE applications

Source: Internet
Author: User

1. servlet is another common component in Java EE applications. It is the second important invention of Java EE technology after JDBC.

2. Differences between JSP and Servlet:

JSP is an HTML page embedded in Java code, and the JSP page is converted to servlet for execution.

Servlet is a Java class embedded in HTML.

3. Processing of servlet execution requests and responses.

4. In Web applications, the <servlet> element is the basic element used to describe the servlet. <Servlet> you can have the following child elements:

· <Icon> · <servlet-Name>: defines a user-friendly name to indicate the resources used. · <Display-Name> · <description> · <servlet-class>: defines the complete path name of the servlet class. · <JSP-File>: defines the complete path name of the JSP file class. · <Init-Paran>: defines servlet parameters. <load-on-startup>: an integer is used to indicate the relative sequence of servlets that should be called during server startup: a servlet with a small value is placed before a servlet with a small value. The servlet with the same value is called in any order. · <Run-as> · <security-role-ref>

Note: You must have one of the <servlet-Name> and <servlet-class> or <JSP-File> elements.

5. <servlet-mapping>: used for ing between a specified servlet name and the corresponding request URI.

6. Because the servlet container can only call the non-parameter constructor of the servlet class, when your servlet class does not mention any constructor, java automatically mentions a non-parameter constructor, so you do not have to write any constructor for the servlet class.

7. servlet lifecycle:

1) Loading and instantiation: servlet containers are angry with each deployment description file web. xml file, and then load the specified servlet class in the deployment description file. Then, call a constructor without parameters to instantiate each servlet class.

2) initialization: The servlet container calls the servlet class's Init (servletconfig) method to implement initialization. This method is provided by the parent class genericservlet.

·The init () method reads servlet initialization parameters or configuration data, initializes external resources (such as database connections), and performs other one-time actions.

·The init () of the genericservlet class has two forms:

Publicvoid Init () throws servletexception

Publicvoid Init (servletconfig) throws servletexception

·In the second method, the servletconfig object comes from: for example, the <servlet> sub-element <init-Paran> element mentioned at point 4th defines the servlet parameter, and the servlet container starts from the web. read these parameters in the XML file and save them to the servletconfig object by "name/value pairs.

·Init (servletconfig): First save the reference of servletconfig, and then call the init () method. The init () method can access all the initialization parameters referenced by servletconfig.

·If you implement Init (servletconfig) in a custom servlet, you must call the super-class Init (servletconfig) method. If super is not called when Init (servletconfig) is implemented. when the init (servletconfig) method is implemented, the servletconfig reference will not be saved, and the servlet or its parent class cannot access the servletconfig object in the subsequent lifecycle.

·If a problem occurs during Init (), the servletexception class or its subclass unavailableexcption should be thrown. This will tell the container that there is a problem during initialization, and this servlet should not be used to process the request. You can use unavailableexcption to set the Servlet's invalid duration. After this duration expires, the container will re-call Init ().

·Unavailableexcption has the following constructor:

Public unavailableexcption (stringmsg, int seconds)

The Int parameter is an integer. Negative and zero values indicate that the servlet cannot be reused. The positive integer indicates that the server should reinitialize the servlet several seconds later.

3) Request Processing: The servlet container calls the Service () method. Like the init () method, the servletexception class or its subclass unavailableexcption exception will be thrown. The difference is that the unavailableexcption created here has no specified invalid duration, therefore, once an exception is thrown, the servlet container must terminate the servlet lifecycle.

4) End of declaration cycle: the servlet container calls the destroy () method. However, the destroy () method is called only when the request processing thread that is still running is finished or the server has defined an expiration time. This method is also implemented by the genericservlet parent class. If your servlet instance does not need to perform any final work, you do not need to implement this method. After the destroy () method is executed, the container releases a reference to the servlet instance.

Note: The destroy () method does not destroy the servlet or trigger garbage collection on it. It simply clears the resources used and opened by the servlet. Obviously, after calling this method, the container will no longer forward any requests to this servlet.

8. How to Ensure servlet thread security:

1) Use method variables to save the private data in the request.

Data is stored in method variables, that is, local variables. At this time, each thread entering this method has its own copy of the method variable. Therefore, no thread will modify the data members of other threads. If you want to save a copy of data between different requests, you should use sessions to store this type of data.

2) only use servlet member variables to save data that will not change.

This type of data storage queries the database connection name, path of other resources, path of other web components, and so on.

3) synchronize access to your member variables that may request modification.

4) if the servlet accesses external resources, you need to access these resources synchronously.

For example, file access is not thread-safe, so you must write code to access this resource synchronously.

Note:

1) implementing the singlethreadmodel interface does not guarantee servlet thread security.

2) do not achieve servlet thread security by synchronizing dopost (), doget (), or service. As the number of requests increases, the larger the number of clients will remain in the waiting state.

3) synchronize as much code as possible within the minimum range of code blocks. The fewer synchronization code, the better the servlet execution.

9. servlet response process:


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.