Servlet 03_java Web

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
One, introduction to Servlet
Used to develop dynamic web resource technology of Sun company, nature is a java procedures, can be run in the servlet container (Tomcat).
1 write a class that implements the servlet interface,
1). write a FirstServlet class, the realization of GenericServlet interface
2)The Servlet jar package. The entry into Classpath
3)Compile the javac. FirstServlet -d.
2 will be written to the Tomcat (class registration is configured in the web.xml file)
4)The compiled files are saved to the Tomcat WEB-INF/classes/ directory
5)Web.xml. Configuration, deploy java to server Tomcat
<servlet>
<servlet-name>HelloServlet</servlet-name> --->Servlet name
<servlet-class>com.itheima.HelloServlet</servlet-class> --->The whole class specific Servlet, is not a folder
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name> --->The corresponding Servlet foreign access configuration name
<url-pattern>/servlet/HelloServlet</url-pattern> --->Foreign access path configuration Servlet
</servlet-mapping>
The life cycle of 3.servlet:
When Servlet was first visit there, the server creates a Servlet object, call the Servlet method of the init immediately execute initialization operation. From this Servlet
Always reside in memory, for all the Servlet access service, follow-up thereafter, each access to the Servlet will cause the service method in Servlet.
For, when the server is down or web application is removed out of the container, with the application of web Servlet in Servlet destruction destruction, destruction, server invokes the Servlet
Destroy method, do some remedial work
The inheritance structure of 4.servlet:
Servlet interface - defines the basic init methods servlet due, destroy, service
|
|--GenericServlet: The servlet sister have a default implementation class, method, we do not pay close attention to the general, this class to help us make the default implementation,
In addition to the service is an abstract method, need to realize the successor, and protocol independent, do not have method of protocol related
|
|--HttpServlet: On the basis of GenericServlet, based on Http protocol is enhanced, provides many and HTTP protocol
Related methods, the service method Abstract in GenericServlet. If it is an get request, provides the doGet method,
If it is an post request, implements the doPost method; the servlet is created, only need to override the doGet and corresponding doPost method can,
Generally not recommended to override the service method
5.servlet details:
1).servletmapping tag:
1'Direct write a fixed path, through the fixed path, access to the current Servlet
2'. use wildcards for configuration, but can only use the following two kinds of fixed way
Path to the beginning and ending with / * /
Path configuration in the mode of *.xx
2)The configuration load-on-startup.:
Is the Servlet with the web application server to start creating immediately after loading
3)Configure a default servlet.:
If the foreign access path configuration of a Servlet /, the Servlet is the default Servlet, other Servlet do not handle the request, the
The default Servlet browser, when accessing static resources, error pages, is " the parent web.xml" default configured in Servlet output, construction
You do not configure a default Servlet, otherwise it will lead to static resource, error page cannot be properly access
4)The thread safety.:
Because by default the Servlet in memory only a, multiple browser concurrency may produce thread safety issues
We can use the lock mechanism to solve the security problem, but easy to cause the program block, reducing efficiency
You can also use the SingleThreadModel interface, but the interface does not solve the thread safety issues, has been abandoned. That in the design of Servlet, should be
Try not to use the class member variables and static variables, if must use, or to use the lock mechanism to access the thread safety issues, but should pay attention to,
As far as possible as early as unlocking, reduce the blocking time
Two, ServletConfig and ServletContext
1.ServletConfig represents the current Servlet in web.xml configuration information
1). access to Servlet configuration in web.xml name
2)At initialization information acquisition in web.xml configuration Servlet
3)Access to ServletContext objects.
2.servletContext represents the web application, only corresponding to the current web application
1). used as a domain object, to transfer data between different Servlet
Scope: the entire web application
Life cycle: when the web application is loaded into the container, created on behalf of the entire web application of the ServletConfig object, created after the resident in memory,
When the server is down or web application is removed out of the container, the ServletContext object with the destruction
Domain: a domain can be understood as a frame, which can put data, a domain that is called a domain, it has a visible range, this range
Can operate on the field data, the object is called a domain object
2)The initialization parameter acquisition. Web application initialization parameter configuration: can the web applications in web.xml, use ServletContext to get
3)Realization of request forwarding.:
Request: the server internal circulation
this.getServletContext().getRequestDispatcher("/...").forward(request,response);
4)Read the resource file.:
1'Application of.Web, read the file, if you use a relative path / absolute path, to find the resources under the tomcat/bin directory; if the use of hard disk path, can
Find the file, but once you have a publishing environment, all paths are likely to be wrong
2'Using ServletContext.getRealPath (), will drive path splicing of current web application before the introduction of the path, so as to obtain the current resource of hard disk
Path, thereby reading resources
3'Through the class loader loading resources: Classloader.getResource (" ".GetPath) (); the current program from which directory to load a class, from which this method
A directory path resource loading



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ira at December 11, 2013 - 2:44 PM