Session解决的相同用户在不同请求间的数据共享问题,那么不同用户的数据共享问题该如何解决?这时候就要使用ServletContext。帖子的浏览次数就是个经典的不同用户需要共享的数据。从Session的机制可以联想,如果服务器端有一个可以被所有用户访问的对象,并将这类数据存入该对象,就可以实现不同用户的数据共享了
Servlet:每个JVM上的Web程序都有一个与之对应的ServletContext对象,ServletContext对象可以被该Web程序中的所有Servlet访问。该对象由服务器创建,切在整个项目中是单例的,可以被任何对象获取。
/**
* ServletContext: 用来解决不同用户的数据共享问题,由服务器创建,单例,由所有用户共享这个对象
*
* @author Song X.
* @date 2020/02/13
*/
public class ServetContextServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//以下是三种获取ServletContext的方式,无论哪种获取的ServletContext都一样,因为ServletContext是单例的
ServletContext servletContext = this.getServletContext();
//ServletContext servletContext1 = this.getServletConfig().getServletContext();
//ServletContext servletContext2 = request.getSession().getServletContext();
//与Session一样,使用setAttribute和getAttribute实现数据共享,这里不多做演示
servletContext.setAttribute("111","hello world");
servletContext.getAttribute("111");
//除了设置数据,还可以移除数据
servletContext.removeAttribute("111");
//获取web.xml中配置的公共属性
String beijing = servletContext.getInitParameter("beijing");
System.out.println(beijing);
//返回资源在服务器上的真是路径
String realPath = servletContext.getRealPath("SessionServlet");
System.out.println(realPath);
//返回web程序的上下文路径
String contextPath = servletContext.getContextPath();
System.out.println(contextPath);
}
}
<!--*************公共属性**************-->
<context-param>
<param-name>beijing</param-name>
<param-value>capital of China</param-value>
</context-param>
ServletConfig用来获取Servlet自己单独的配置配置项
/**
* ServletConfig用于每一个Servlet获取自己单独的属性配置
* 每一个Servlet单独拥有一个ServletConfig对象
* @author Song X.
* @date 2020/02/14
*/
public class ServletConfigServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletConfig servletConfig = this.getServletConfig();
Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
while(initParameterNames.hasMoreElements()){
String key = initParameterNames.nextElement();
String value = servletConfig.getInitParameter(key);
System.out.println(key + " : " + value);
}
}
}
```xml
<servlet>
<servlet-name>ServletConfigServlet</servlet-name>
<servlet-class>home.frank.servletConfigProject.ServletConfigServlet</servlet-class>
<init-param>
<param-name>beijing</param-name>
<param-value>sanlitun</param-value>
</init-param>
<init-param>
<param-name>shanghai</param-name>
<param-value>waitan</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletConfigServlet</servlet-name>
<url-pattern>/config</url-pattern>
</servlet-mapping>
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/20599.html