ServletContext

1.为什么需要servletContext

   需求1
需求2
---------------》解决之道servletContext
 
 
servletContext
1.ServletContext是在服务器
2.ServletContext是被所有客户端共享
3.ServletContext是当web应用启动的时候,自动创建
4.ServletContext当web应用关闭  ,tomcat关闭,对web应用reload造成ServletContext的消亡
 
 
ServletContext小结
 
获取属性
//1.通过this直接获取。
 ServletContext servletcontext=this.getServletContext();
   //2.通过ServletConfig获取
ServletContext servletcontext2=this.getServletConfig().getServletContext();
 
添加属性 
servletcontext.setAttribute("属性名name", "val韩顺平");  
 
取出属性
String val=(String)servletcontext.getAttribute("属性名name");  
 
删除
servletcontext.removeAttribute("属性名name");  
 
ServletContext应用
(1)获取WEB应用的初始化参数
  <!--如果希望所有的servlet都可以访问该配置-->
   <context-param>
          <param-name>name</param-name>
          <param-value>nscott</param-value>
  </context-param>
如何获取
String val=(String)this.getServletContext().getInitParameter("name");  
 
(2)转发
  response.sendRedirect("/web应用名/资源");
  request.getRequestDispatcher("/资源名").forward(request,response);
区别1.getRequestDispatcher发生在web服务器,sendRedirect发生在浏览器
       2.如果  request.setAttribute("name","shunping")希望下一个页面可以使用该值使用  request.getRequestDispatcher("/资源名").forward(request,response);
       3.如果  session.setAttribute("name","shunping")希望下一个页面,两个方法都能使用
               建议使用  request.getRequestDispatcher("/资源名").forward(request,response);
      4.如果希望跳转到本web应用以外的url ,只能使用 response.sendRedirect
this.getServletContext().getRequestDispatcher("/资源名").forward(request,response);
(3)读取文件和获取文件的全路径
1.为什么需要servletContext
   需求1
需求2
---------------》解决之道servletContext
 
 
servletContext
1.ServletContext是在服务器
2.ServletContext是被所有客户端共享
3.ServletContext是当web应用启动的时候,自动创建
4.ServletContext当web应用关闭  ,tomcat关闭,对web应用reload造成ServletContext的消亡
 
 
ServletContext小结
 
获取属性
//1.通过this直接获取。
 ServletContext servletcontext=this.getServletContext();
   //2.通过ServletConfig获取
ServletContext servletcontext2=this.getServletConfig().getServletContext();
 
添加属性 
servletcontext.setAttribute("属性名name", "val韩顺平");  
 
取出属性
String val=(String)servletcontext.getAttribute("属性名name");  
 
删除
servletcontext.removeAttribute("属性名name");  
 
ServletContext应用
(1)获取WEB应用的初始化参数
  <!--如果希望所有的servlet都可以访问该配置-->
   <context-param>
          <param-name>name</param-name>
          <param-value>nscott</param-value>
  </context-param>
如何获取
String val=(String)this.getServletContext().getInitParameter("name");  
 
(2)转发
  response.sendRedirect("/web应用名/资源");
  request.getRequestDispatcher("/资源名").forward(request,response);
区别1.getRequestDispatcher发生在web服务器,sendRedirect发生在浏览器
       2.如果  request.setAttribute("name","shunping")希望下一个页面可以使用该值使用  request.getRequestDispatcher("/资源名").forward(request,response);
       3.如果  session.setAttribute("name","shunping")希望下一个页面,两个方法都能使用
               建议使用  request.getRequestDispatcher("/资源名").forward(request,response);
      4.如果希望跳转到本web应用以外的url ,只能使用 response.sendRedirect
this.getServletContext().getRequestDispatcher("/资源名").forward(request,response);
(3)读取文件和获取文件的全路径

 
 
 
 
 
 
原文地址:https://www.cnblogs.com/sunli0205/p/5893793.html