On the technical basis of servlet development

1、 Servlet overview

1. Dynamic web resource development technology provided by Sun company. It is essentially a java applet. It is required that the applet must implement the servlet interface so that the server can call it.

2. Two steps of servlet development

*Experiment: a quick start to Servlet

(1) Step 1: write a java program to implement the servlet interface (here directly inherits the default implementation class genericservlet)

(2) In addition to putting the compiled. Class with package under WEB-INF / classes, configure the web.xml registration servlet of the web application

3. Develop servlet with MyEclipse

2、 Servlet details

1. Life cycle: when a thing will live and die, and what it is bound to do during its existence. All these together are the declaration cycle of the thing.

2. Servlet life cycle: usually, when a servlet is accessed for the first time, it creates an object in memory and immediately calls the init () method for initialization after creation. For each request, the service (req, resp) method is used to process the request. At this time, the request object will be used to encapsulate the request information, And use the response object (initially empty) represents a response message, which is passed into the service method for use. After the service method completes processing, it returns to the server. The server organizes the response message according to the information in the response and returns it to the browser. After the response, the servlet does not destroy it and stays in memory waiting for the next request. Until the server is shut down or the web application is removed from the virtual host The servlet object is destroyed and the destroy () method is called to do some aftermath before destruction.

3. Inheritance structure of servlet interface

Servlet interface: defines the methods that a servlet should have. All servlets should implement this interface directly or indirectly

|

|----Genericservlet: the default implementation of servlet interface, general servlet, is an abstract class. Most of the methods are implemented by default. Only the service method is an abstract method that needs to be implemented by the inheritor

|

|----Httpservlet: a servlet that optimizes the HTTP protocol. It inherits from the genericservlet class and implements the service abstract method. The default implementation determines the request mode of the request, and calls different doxxx () methods according to different request modes. Usually, we can directly inherit httpservlet

4.web. Considerations for registering servlets with XML

4.1 register a servlet with the < servlet > < servlet mapping > tag

Note: here is the full class name of a servlet, not including Java or Class extended file path

4.2 a < servlet > can correspond to multiple < servlet mapping >

4.3 < Serlvet mapping > can be configured with * matching character, but note that it must be * The path starting with do or / and ending with / *.

~Due to the introduction of matching characters, it is possible that a virtual path will correspond to multiple servlet mapping. At this time, which servlet is most like which servlet to find, and * Do level is the lowest.

4.4 the < load on startup > sub tag can be configured for < servlet >, which specifies that the servlet will be loaded with the startup of the server, and the configured value specifies the startup order

4.5 default servlet: if the external access path of a servlet is set to /, the servlet is a default servlet, and requests not processed by other servlets are processed by it

~In conf / Web The default servlet is configured in XML, and the access to static resources and the output of error pages are handled by this default servlet. If we write a default servlet to put dad on the web If the default servlet in XML is overridden, static web resources will be inaccessible. Therefore, configuration is not recommended.

4.6 thread safety of Servlet

4.6. 1. Generally, a servlet has only one instance in memory to process requests. When multiple requests are sent, multiple threads will operate the servlet object, which may lead to thread safety problems.

(1) The member variables of Serlvet may have thread safety problems

*Experiment: define a member variable inti = 0; Perform I + + operation in doxxx () method and output the I value to the client. At this time, thread safety problems may be caused due to delay

(2) When Serlvet operates a resource file, multiple threads operate on the same file, causing thread safety problems

*Experiment: the request comes with a parameter. The servlet writes the request parameter to a file, reads the file, and prints the read value to the client. There may be a thread safety problem

4.6. 2 solution

(1) Use synchronized code blocks to solve the problem. The defect is that the synchronization code block can only process one request at a time, which is very inefficient. Therefore, the synchronization code block should only contain the core code that leads to thread safety problems.

(2) Implement the singlethreadmodel interface for the servlet, which is a marked interface. The marked servlet will save a servlet pool in memory. If a thread comes and there is no servlet object processing in the pool, a new one will be created. If there are free servlets in the pool, use them directly. This does not really solve the thread safety problem. This interface has been deprecated.

(3) Neither solution is perfect, so try not to have member variables in servlets.

3、 ServletConfig

1. Objects representing servlet configuration can be displayed on the web Configuration in < servlet > in XML

Then use this in the servlet Getservletconfig() gets the ServletConfig object, which provides getinitparameter() and getinitparameternames() methods to traverse the configuration items in the configuration.

The content that you don't want to write dead in the servlet can be configured here.

4、 ServletContext

1. Object representing the current web application.

2. It is used as a domain object to transfer data between different servlets. Its scope is the whole web application

Life cycle: when the web application is loaded into the container, a ServletContext object representing the whole web application is created. When the server is shut down or the web application is removed from the container, the ServletContext object is destroyed.

~Domain: a domain is understood as a box in which data can be placed. Since a domain is called a domain, it has a visible range in which data in the domain can be operated. Such an object is called a domain object.

3. On the web XML can configure the initialization parameters of the whole web application and use ServletContext to obtain

4. Forward between different servlets

After the method is executed, the service will return to the server, and then the server will call the target servlet. The request will be recreated and the data of the previous request will be copied in.

5. Read resource file

5.1 by default, the relative path is relative to the directory started by the Java virtual machine, so we can't get resources because we write the relative path directly relative to the Tomcat / bin directory. If it is written as an absolute path, the absolute path will be wrong when the project is published to other environments.

5.2 to solve this problem, ServletContext provides this getServletContext(). Getrealpath ("/ 1. Properties"), give the virtual path of a resource and return the real path of the resource in the current environment. this. getServletContext(). Getresourceasstream ("/ 1. Properties"), a stream that returns the virtual path of a resource to the real path of the resource.

5.3 when a resource file is obtained under a non servlet, there is no ServletContext object. At this time, only the class loader can be used

classLoader. Getresourceasstream ("... /.. / 1. Properties"), this method uses the class loader to directly load resources into memory. There is a problem of update delay and too much memory if the file is too large.

classLoader. getResource("../1.properties"). Getpath(), which directly returns the real path of the resource. There is no problem of update delay.

summary

The above is all about the technical basis of servlet development in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Basic analysis of servlet session Technology

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>