Head First Servlets & JSP Learning Note Fifth--as a Web application

Source: Internet
Author: User

Initialization parameters: (Init-param)

Initialization parameters are written in the Web. Xml File: (written inside the <servlet> tag)

1 <servlet>2     <Servlet-name>Beeradvisorservlet</Servlet-name>3     <Servlet-class>Com.example.web.BeerServlet</Servlet-class>4 5     <Init-param>6         <Param-name>Email</Param-name>7         <Param-value>[Email protected]</Param-value>8     </Init-param>9 </servlet>

When a servlet executes a constructor, it simply instantiates an ordinary object and needs two things to become a true servlet:servletconfig, ServletContext.

When the Web container (Tomcat) Initializes a servlet, it constructs a unique servletconfigfor the servlet.

The Web container reads the initialization parameters (Init-param) of the servlet from the deployment description file ( Web. xml) and gives these parameters to ServletConfig, Then pass the servletconfig to the init () method .

The Web container reads the initialization parameters in the DD (XML), which is read only once in the lifetime of a servlet, just once, like a constructor, init () initialization function.

Each time you restart Tomcat, Tomcat looks at its WebApps directory and deploys all the apps found there.

<servlet> initialization parameters are valid only for one servlet, and the <web-app> context parameters are valid for the entire Web application. The context parameter is not written in a <servlet> because it is valid for the entire Web application, so it is written in <web-app>:

1 <Web-app>2     <Context-param>3         <Param-name>AdminEmail</Param-name>4         <Param-value>[Email protected]</Param-value>5     </Context-param>6 </Web-app>

Each servlet has a servletconfig, and a web app has only one servletcontext.

Compare ServletConfig objects and ServletContext objects
ServletConfig object (with initialization parameters preserved) ServletContext object (with context parameters preserved)
How to use Getservletconfig (). Getinitparameter ("parameter name"); Getservletcontext.getinitparameter ("parameter name");
Available Range

Available only for servlets configured with <init-param>, but can be

Get a greater range of availability by saving in one property

All Servlets, JSPs are available for the entire Web application

Number A servlet has a servletconfig One web App with only one servletcontext (except for distributed applications)

In Web. xml

The configuration in

<servlet>

<init-param>

<param-name> name of the parameter </param-name>

<param-value> parameter Values </param-value>

</init-param>

</servlet>

<web-app>

<context-param>

<param-name> name of the parameter </param-name>

<param-value> parameter Values </param-value>

<context-param>

</web-app>

Assigning values to Objects

Provided ServletConfig when the servlet was first initialized

Provided ServletContext when the servlet was first initialized

To initialize the parameters <init-param> and context parameters <context-param> think of the deployment constants, which is useless when the Web application is running, they are only read once at the time of deployment.

Servletcontextlistener (Context Listener)

Servletcontextlistener is able to listen to a context initialization event so that it can get context parameters and run some code before the app provides services to the customer.

Just as you tell the Web container about the rest of the Web application, you can discover and use Servletcontextlistener by configuring the Servletcontextlistener,web container with the deployment description file (XML) as well.

1 <Web-app>2     <Listener>3         <Listener-class>4 Com.example.listener.MyServletContextListener5         </Listener-class>6     </Listener>7 </Web-app>
Three attribute scopes:

Context, session (HttpSession), request (ServletRequest)

The context property is not thread safe: a servlet A sets a context property and assigns a value; another servlet B also assigns a value to the context property, and when servlet a gets the value of the context property, it is not the original assignment.

Session properties are also not thread-safe: A customer can open multiple browser windows to make a request, or a container with the same session.

Only request properties and local variables are thread-safe.

Head First Servlets & JSP Learning Note Fifth--as a Web application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.