标签:
1 Servlet 的创建
当Servlet容器启动web应用时,需要立即加载Servlet时:
Servlet容器启动web应用时,将按照指定的顺序初始化Servlet,需要设置<Servlet>元素的<load-on-startup>子元素。<load-on-startup>设置的值为数字,当值为0或者大于0时,表示容器在应用启动时就顺序加载并初始化这个servlet。
当Servlet容器启动时会启动所有的web应用。
2 ServletContex与web应用的关系。
当Servlet容器启动Web应用时,并为每个Web应用创建唯一的ServletCOntext对象。可以把ServletContext看成是一个Web应用的服务器端组件的共享内存。在ServletContext中可以存放共享数据,它提供了读取或设置共享数据的方法。
ServletContext中可以存放共享数据,它提供了读取或设置共享数据的方法:
1 Servlet容器启动web应用时,将按照指定的顺序初始化Servlet
LoadInitServlet.java
package com.test; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; public class LoadInitServlet extends HttpServlet { public void init() throws ServletException { super.init(); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>javaWeb1</display-name> <servlet> <servlet-name>LoadInitServlet</servlet-name> <servlet-class>com.test.LoadInitServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> </web-app>
2 ServletContex的计时器例子
CounterServlet.java
package com.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CounterServlet extends HttpServlet { private static final String CONTENT_TYPE = "text/html;charset=utf-8"; public void init(ServletConfig config) throws ServletException { super.init(config); System.out.println("init invoked"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获得ServletContext的引用 ServletContext context = getServletContext(); // 从ServletContext读取count属性 Integer count = (Integer) context.getAttribute("count"); // 如果count属性还没有设置, 那么创建count属性,初始值为0 // one and add it to the ServletContext if (count == null) { count = new Integer(0); context.setAttribute("count", new Integer(0)); } response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>WebCounter</title></head>"); out.println("<body>"); // 输出当前的count属性值 out.println("<p><h1>The current COUNT is : " + count + ".</h1></p>"); out.println("</body></html>"); // 创建新的count对象,其值增1 count = new Integer(count.intValue() + 1); // 将新的count属性存储到ServletContext中 context.setAttribute("count", count); } public void destroy() { } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>javaWeb1</display-name> <servlet> <servlet-name>CounterServlet</servlet-name> <servlet-class>com.test.CounterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CounterServlet</servlet-name> <url-pattern>/CounterServlet</url-pattern> </servlet-mapping> </web-app>
标签:
原文地址:http://www.cnblogs.com/wangshuo1/p/5790951.html