标签:定义 服务 案例 tom writer longest ref head value
servletContext对象是Servlet三大域对象之一,每个Web应用程序都拥有一个ServletContext对象,该对象是Web应用程序的全局对象或者上下文。Tomcat服务器在启动时,会自动创建一个ServletContext对象,在关闭时,会自动销毁这个ServletContext对象。每个Web应用程序只拥有一个ServletContext对象,ServletContext对象可以在整个Web应用中共享数据资源。
1 import java.io.IOException; 2 3 import javax.servlet.ServletConfig; 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 //演示获取servletContext对象 10 /** 11 * 有三种方式获取servletContext对象 12 * 1. 采用servletConfig对象获取 13 * 2. 采用servlet实例对象获取 14 * 3. 采用request对象获取 15 * @author Administrator 16 * 17 */ 18 public class ServletContext1 extends HttpServlet { 19 20 ServletContext sc ; 21 22 @Override 23 public void init(ServletConfig config) throws ServletException { 24 super.init(config) ; 25 sc = config.getServletContext() ;//1. 26 } 27 28 public void doGet(HttpServletRequest request, HttpServletResponse response) 29 throws ServletException, IOException { 30 //第二种方式 31 ServletContext sc1 = this.getServletContext() ; 32 System.out.println(sc); 33 System.out.println(sc1 == sc); 34 //第三种方式 35 ServletContext sc2 = request.getSession().getServletContext() ; 36 System.out.println(sc2 == sc); 37 } 38 39 public void doPost(HttpServletRequest request, HttpServletResponse response) 40 throws ServletException, IOException { 41 doGet(request, response); 42 } 43 }
1) 实现数据共享
2) 获取全局配置参数
3) 请求转发
a) 拿取请求转发器,然后转发
4) 获取资源文件
5) 用来获得文件的MIME的类型.
下面我将通过例子说明:登录成功后,5秒后跳转到某个页面,在页面中显示您是第x次登录成功
1 public class VisitServlet extends HttpServlet { 2 @Override 3 public void init() throws ServletException { 4 ServletContext context = getServletContext(); 5 context.setAttribute("times", 0); 6 } 7 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 8 ServletContext context = getServletContext(); 9 10 int times = (Integer)context.getAttribute("times"); 11 times ++; 12 context.setAttribute("times", times);
response.setHeader("Refresh", "5;url=/test/showTimeServlet");
13 } 14 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 doGet(request, response); 16 } 17 }
1 public class ShowTimeServlet extends HttpServlet { 2 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 ServletContext context = getServletContext(); 4 //获取访问的次数 5 int times = (Integer)context.getAttribute("times"); 6 7 response.setContentType("text/html;charset=utf-8"); 8 PrintWriter out = response.getWriter(); 9 out.println("<h1>VisitServlet共被访问了"+times+"次</h1>"); 10 } 11 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 12 doGet(request, response); 13 } 14 }
获得Servlet的web.xml配置信息
1 <context-param> 2 <param-name>username</param-name> 3 <param-value>苍老师</param-value> 4 </context-param> 5 <context-param> 6 <param-name>password</param-name> 7 <param-value>123</param-value> 8 </context-param>
获取web.xml配置信息中
import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 获取web应用的初始化信息 */ public class ServletContextDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得ServletContext域对象 ServletContext context = this.getServletContext(); //获得单个数据(username) String name = context.getInitParameter("username") ; // System.out.println(name); //获得所有数据 Enumeration enumeration = context.getInitParameterNames(); while(enumeration.hasMoreElements()){ String name = (String) enumeration.nextElement(); String value = context.getInitParameter(name); System.out.println(name+":"+value); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
在javax.servlet包中,定义了ServletConfig接口。Servlet容器使用ServletConfig对象在Servlet初始化时向其传递配置信息。
所谓的Serlvet配置信息,就是在Web应用程序中web.xml文件中配置有关Servlet的内容。
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ServletConfigTest</servlet-name> <servlet-class>app.java.servlet.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/servlet/ServletConfigTest</url-pattern> </servlet-mapping> <init-param> <param-name>name</param-name> <param-value>yl</param-value> </init-param> <init-param> <param-name>blog</param-name> <param-value>http://www.longestory.com</param-value> </init-param>
public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException { ServletConfig servletConfig = this.getServletConfig(); String servletName = servletConfig.getServletName(); System.out.println(servletName); }
运行Web应用程序,在控制台中打印“ConfigServletTest”。
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { ServletConfig servletConfig = getServletConfig(); String name = servletConfig.getInitParameter("name"); String blog = servletConfig.getInitParameter("blog"); System.out.println(name + "‘s blog is " + blog); }
运行Web应用程序,在控制台中打印“yl‘s blog is http://www.longestory.com”。
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { ServletConfig servletConfig = getServletConfig(); Enumeration enumeration = servletConfig.getInitParameterNames(); while (enumeration.hasMoreElements()) { String element = (String) enumeration.nextElement(); String value = servletConfig.getInitParameter(element); System.out.println(element + ": " + value); } }
<!-- Standard Action Servlet Configuration (with debugging) --> <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, /WEB-INF/struts-config-Wildcard.xml </param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
2017-05-12
小谈-—ServletConfig对象和servletContext对象
标签:定义 服务 案例 tom writer longest ref head value
原文地址:http://www.cnblogs.com/god-y-l/p/6847612.html