javaweb three major components -servlet

1, what is the servlet
servlet is a program running on a web server or application server
2, what action servlet
servlet is used to process client requests, the servlet task has
(1) a data acquisition request
(2) processing request
( 3) completion response
3. implementation servlet manner
(1) to achieve the servlet interface (convenient)
(2) Inheritance of GenericServlet (convenient)
(3) inheritance class HttpServlet (easy)
. 4, life cycle servlet
lifecycle comprises servlet servlet birth, service, death.
A total of five Servlet interface methods, but only three life-cycle approach:
* void the init (the ServletConfig)
* void Service (ServletRequest, ServletResponse)
* void Destory ()
(1) server creates Servlet:
* When the Servlet is first when requested, or when the server starts, the server will create a Servlet examples.
* The default is to create the server Servlet servlet instance when the first request, creating implementation requires Servlet web.xml configuration if desired server is started
* server only creates an instance of an object type is a Servlet, it is a single Servlet Example of;

let the server when you start to create a Servlet
<the servlet>
<the servlet-name> hello1 </ the servlet-name>
<the servlet-class> cn.itcast.servlet.Hello1Servlet </ the servlet-class>
<Load-ON-Startup> 0 </ Load-ON-Startup>
</ the servlet>
<-Mapping the servlet>
<the servlet-name> hello1 </ the servlet-name>
<URL-pattern> / hello1 </ URL-pattern>
</ Mapping the servlet->
translation: this class is alias cn.itcast.servlet.Hello1Servlet hello1, to / hello1 this path request, are processed to give hello1

2) server initialization Servlet:.
* When the server creates Servlet instance will call immediately after the init Servlet (ServletConfig) method to complete the initialization of the Servlet;
* the init (ServletConfig) will only be called once
* server will call the init () method when passing parameters ServletConfig

3) Servlet to process the request using the server:
* when the Servlet is requested, the server calls the Servlet-Service (ServletRequest, ServletResponse) method
* service (ServletRequest, ServletResponse) a method of treatment for each request, will be called once so it may be called N times
* because Servlet is a singleton, it is possible the same time a Servlet object will be simultaneously access multiple requests, so this problem may occur threads cases
* Servlet is not thread cases, this will help and improve efficiency, but has not let Servlet state, in order to avoid multiple threads scramble data

4) The server Servlet destroy
* usually only when the server is shut down will destroy Servlet
* server calls destory Servlet before the destruction of Servlet () method
* Servlet resource release can be given possession in destory () method, but usually Servlet nothing will happen is released, this method is generally empty
5, servlet associated with the interface type
 ServletRequest: parameter service () method, which represents a request object that encapsulates all the data associated with the request, it is by the server created;
l the ServletResponse: parameter service () method, which represents a response object, complete response to the client needs to use the object in the service () method;
l the ServletConfig: the init () method parameter, which represents Servlet configuration object, it corresponds to the Servlet configuration information that corresponds to the file web.xml <servlet> element.
ServletConfig function categories are:
* getServletName String (): Get Servlet configuration name value, i.e. <servlet-name> of;
* ServletContext GetServletContext (): Get ServletContext object, which are described later
* String getInitParameter (String name): Get initialization parameters
* Enumeration getInitParameterNames (): Gets the name of all initialization parameters
6, HttpServlet
(. 1) HttpServlet covers the service () method, focus HTTP request
service (HttpServletRequest, HttpServletResponse);
the ServletRequest strong turn into HttpServletRequest
the ServletResponse intensity converted into the HttpServletResponse
(2) the doGet () and the doPost ()
-Service in the HttpServlet (HttpServletRequest, HttpServletResponse) being determined to be the current request is a GET or POST, GET request if it is, then the call will go to this class doGet () method, if POST requests to call doPost () method

7, servletContext
(1) Servlet ServletContext is one of the three domain objects, ServletContext is created when the server starts, destroyed when the server shuts down, only a JavaWeb application creates a ServletContext object.
(2) its functional classification:
* access data
* reading application initialization parameter in web.xml
* reading application resources
(3) obtaining ServletContext object
can be obtained by the following method in ServletContext object in HttpServlet
* ServletContext sc = this .getServletContext ()
* SC = ServletContext this.getServletConfig (). GetServletContext ()
(. 4) to access data
as JavaWeb in one application, only a ServletContext object, the stored data may be common in the ServletContext entire dynamic application JavaWeb resource sharing
ServletContext Servlet is one of the three domain objects inside the domain object has a Map, to save data
* void setAttribute (String name, Object value): used to add or replace ServletContext domain data
> servletContext.setAttribute ( "xxx", "XXX"), add the domain data
> ServletContext.setAttribute ( "xxx", " XXXX"), covering the field data because the data in the domain name is xxx already exists, so this is covered
* Object getAttribute (String name): to obtain the domain name through data
* void removeAttribute (String name): data field is removed name
* Enumeration <String> getAttributeNames () : Get the names of all data fields ServletContext
application initialization parameters in web.xml (5) read

<context-param>
<param-name>p1</param-name>
<param-value>v1</param-value>
</context-param>
<context-param>
<param-name>p2</param-name>
<param-value>v2</param-value>
</context-param>

* ServletContext.getInitParameter ( "p1"), returns V1
* servletContext.getInitParameter ( "p2"), returns V2
* servletContext.getInitParameterNames (), returns Enumeration <String>, comprising p1 and p2

. (6) to obtain project resources
* String getRealPath (String path): access to resources real name
String path = servletContext.getRealPath ( "/ WEB -INF / a.jpg");
return value /WEB-INF/a.jpg the true path, the disk path: C: /tomcat6/wabapps/hello/WEB-INF/a.jpg

* InputStream getResourceAsStream (String path): access to resources of the input stream
InputStream in = servletContext.getResourceAsStream ( "/ WEB -INF / a.jpg");
returns a.jpg input stream object can be obtained from a stream. jpg data

* Set <String> getResourcePaths (String path): Get all resources specified directory path under
the Set <String> = servletContext.getResourcePaths Paths ( "/ the WEB-INF");
the Set string returned contains the following:
> / WEB- INF / lib /
> / the WEB-INF / classes /
> /WEB-INF/web.xml
> /WEB-INF/a.jpg
8. The acquired resources classpath

Resource may be acquired through the object class path Class class corresponding to the application classpath JavaWeb resource classes directory is
for example:
the InputStream in cn.itcast.servlet.MyServlet.class.getResourceAsStream = ( "a.jpg");
the acquisition is: /WEB-INF/classes/cn/itcast/servlet/a.jpg, namely resources and at the same directory MyServlet.class

For example:
the InputStream in cn.itcast.servlet.MyServlet.class.getResourceAsStream = ( "/ a.jpg");
acquired is: /WEB-INF/classes/a.jpg, i.e., the root directory of the resource class path, is the root directory path the class / classes directory

What is servletConfig

HttpServlet principle

HttpServlet timing diagram

 

Guess you like

Origin www.cnblogs.com/beanjk/p/11248879.html