Home > php教程 > PHP开发 > servlet summary

servlet summary

高洛峰
Release: 2016-12-01 13:20:41
Original
1622 people have browsed it

ervlet summary

1. Servlet is responsible for processing user requests on the server side. When the client sends a request to the server, the server finds the corresponding Servlet through the web.xml configuration file to process the request. A servlet must be bound to a URL, and users access the servlet by accessing the URL.



2. Three ways to implement servlet

Java.servlet.Sertvlet class

Java.servlet.GenericServlet class

Java.servlet.HttpServlet class


3.serv in let interface Method

//Initialization

void init (ServletConfig config)

void service (ServletRequest request, ServletResponse response)

//Destruction

void destroy()


String getServletInfo()

ServletConfig getServletConfig ()

The client sends a request, and the server will find the corresponding Servlet through the URL. If the Servlet object does not exist, the server will create the Servlet object

After creating the object, the server immediately calls init (ServletConfig config) to complete the initialization work. The server calls the Servlet's service (ServletRequest req, ServletResponse res) method. At this time, it needs to pass the parameters request and response to it.

When the client accesses this Servlet again, because the Servlet object already exists, it will not be created. Servlet is a singleton.

(Note: The singleton pattern is a commonly used software design pattern. Its core structure only contains a special class called a singleton class. The singleton pattern can ensure that there is only one instance of a class in the system and This instance is easy to access from the outside, which facilitates control of the number of instances and saves system resources. If you want only one object of a certain class to exist in the system, the singleton mode is the best solution)

When the server needs it. When destroying the Servlet object, destroy() will first be called to complete the release of some resources, and then destroy the object

4. Configure the servlet in web.

cn.edu.aynu.rjxy.servlet.HelloWorldServlet

HelloWorldServlet


/helloWorld

< ;/servlet-mapping>

The configuration is to bind the URL and Servlet together. When the user accesses, the corresponding Servlet will be found to complete the request processing.

5.ServletConfig: The parameter of the init() method, which represents the Servlet configuration object and its corresponding Servlet configuration information.

In fact, the configuration information is the element in the web.xml file.

;/ servlet-class>

ServletConfig object is created by the server. When the server calls the init() method, it is passed to the method init() as an actual parameter.

We can use the init() method use it in.

(1) String getInitParameter(String name): Used to obtain the initialization parameters configured in web.xml and obtain the parameter value through the parameter name.

(2) Enumeration getInitParameterNames(): Used to obtain all initialization parameter names configured in web.xml.

(3) ServletContext getServletContext(): used to return the ServletContext object

(4) String getServletName(): used to return the name of the Servlet configured in web.xml, that is, the name specified by the element

                                                                           ;init -param>

                                                                                            ;

                                                                                                                                      ,,,,, Complete initialization immediately

school=aysfxy

major=java

class1=2

school=aysfxy

class1=2

major=java

school=aysfxy

major=java

class1=2

school=aysfxy

class1=2

major=java

6. GenericServlet: It is an abstract class that implements the javax.servlet.Servlet interface. Inheriting this class can also implement Servlet.

The getServletContext(), getInitParameter() and other methods in the class can be used directly. If you want to do some initialization parameter work,

Don’t override the method init (ServletConfig config), but override the method init()

HttpServlet: It is a Servlet class specially used to handle Http protocol requests. We can implement Servlet by inheriting HttpServlet

This is the simplest way. We will do this in the future, because the request sent by the client only has get and post. So we only need to rewrite these two methods.


7.HttpServlet: It is a Servlet class specially used to handle Http protocol requests. We can implement Servlet by inheriting HttpServlet

This is the simplest way, we will do this in the future, because the client There are only two methods of sending requests: get and post. So we only need to rewrite these two methods.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template