Question fourth: What is the life cycle of a servlet?
The life cycle of the servlet is briefly summed up in four steps: Service--------Instantiation of the servlet class loading.
Steps for servlet instantiation:
1. When the servlet container starts: reads the information in the Web. XML configuration file, constructs the specified Servlet object, creates a ServletConfig object, and invokes the ServletConfig object as a parameter to invoke the Init method of the Servlet object.
2. After the servlet container is started: The client makes a request to the servlet for the first time, the servlet container will determine if there is a specified Servlet object in memory, create it if not, and then create a httprequest based on the customer's request. A HttpResponse object that invokes the service method of the Servlet object.
3. After the servlet class file is updated, recreate the servlet
The servlet container automatically creates the servlet at startup, which is determined by the <load-on-startup> properties set for the servlet in the Web. xml file. And from there we can see that the same type of servlet object exists as a singleton in the servlet container.
If Loadonstartup is not set, it is instantiated the first time it is requested. If you set Loadonstartup, there are three cases:
Loadonstartup < 0
In the case of negative numbers, the Web container does not instantiate when it is started, and is instantiated when the servlet is called for the first time. This situation is the same as not setting loadonstartup.
Loadonstartup > 0
When the Web container is started, it is instantiated, and the order is small to large, and the positive integers are instantiated first.
Loadonstartup = 0
When the Web container is started, it is instantiated, which is equivalent to the largest integer, so when the Web container starts, it is instantiated at the end of the process.
For example, the following example:
<servlet>
< servlet-name>action<servlet-name>
< servlet-class>org.apache.struts.action.actionservlet</servlet-class>
< init-param>
< param-name>config</param-name>
< param-value>/web-inf/struts-config.xml</param-value>
</init-param>
< init-param>
< param-name>name</param-name>
< param-value>wangsan</param-value>
</init-param>
< init-param>
< param-name>address</param-name>
< param-value>beijing</param-value>
</init-param>
< load-on-startup>2</load-on-startup>
</servlet>
< servlet-mapping>
< servlet-name>action</servlet-name>
< url-pattern>*.do</url-pattern>
</servlet-mapping>
The following configuration section information is parsed
The name of the Servlet-name:servlet object
Servlet-class: The class to invoke to create the Servlet object
Param-name: Parameter name
Param-value: Parameter values
Load-on-startup:servlet the order in which the Servlet objects are loaded when the container starts
Servlet-mapping/servlet-name: To correspond to the contents of the Servlet-name configuration section in the servlet
Url-pattern: The relative URL path of the servlet that the customer accesses
When the servlet container starts reading <servlet> configuration section information, the Servlet object is created according to the <servlet-class> configuration section information, and according to the <init-param> The configuration section information creates the Httpservletconfig object, then executes the Init method of the Servlet object, and determines the order in which the Servlet objects are created, based on the <load-on-startup> configuration section information. If this configuration section information is negative or not configured, the Servlet object will not be loaded when the servlet container is started. When a client accesses a servlet container, the servlet container finds the specified Servlet object by using the <url-pattern> configuration section information in the <servlet-mapping> configuration section, based on the URL address that the customer accesses. and calls the service method of this Servlet object.
Question Fifth: PageContext relationships with other JSP built-in objects?
The PageContext object is a very important built-in object in JSP, but it is seldom used in a generic JSP program. It is an instance object of the Javax.servlet.jsp.PageContext class, and you can use the methods of the PageContext class. In fact, the PageContext object provides access to all the objects and namespaces of the JSP page. PageContext objects can access other hidden objects.
Method |
return value |
Method description |
GetException () |
Exception |
Gets the current exception built-in object, but this page is error page |
Getout () |
JspWriter |
Get the output stream of a Web page |
GetPage () |
Object |
Get page servlet entity |
Getrequest () |
Servletrequset |
Get a request for a webpage |
GetResponse () |
Servletresponse |
Get Responses to Web pages |
Getservletconfig () |
ServletConfig |
Get the ServletConfig object for a Web page |
Getserveltcontext () |
ServletContext |
Get the execution environment for your web page |
GetSession () |
HttpSession |
Get a conversation that's connected to a webpage |
GetAttribute (String name,int scope) |
Object |
Gets the Name property value for the specified range |
Getattributenamesinscope (int scope) |
Enumeration |
Gets all property names for the specified range |
Getattributesscope (String name) |
Int |
Get property range with Name |
RemoveAttribute (String name) |
void |
Remove property with Name property |
RemoveAttribute (String name,int scope) |
void |
Removes the attribute with the name of the specified range |
SetAttribute (String name,object value,int scope) |
void |
Sets the name property of the specified range |
Findattribute (String name) |
Object |
Look for properties with name for all scope property names |
Question sixth: What are the characteristics of the request attribute?
Request: The attribute is valid within the scope of a request. If the page jumps from one page to another, the property is invalidated. The jump referred to here refers to a client jump, such as a customer clicking a hyperlink to jump to another page or browsing another page through the browser's address bar. If the server-side jump <jsp:forward> is used, the property remains in effect. Similarly, use the SetAttribute () and getattribute () of the Request object.
Reference content:
Articles explaining the servlet life cycle:
Http://www.cnblogs.com/AaronLi/archive/2011/09/02/2164355.html
Details of the JSP built-in object of the article, this article layout is good, especially the table is used to explain the knowledge is very reasonable, but the content of more need to mercilessly thoroughly understand.
Http://www.blogjava.net/RoyPayne/archive/2012/01/05/367930.htm
Reprinted from:Struts Tutorial Network (netease) ? JSP built-in object details (bottom)