• servlet的ServletContext接口


    ServletContext

      Servlet 上下文

    每个web工程都只有一个ServletContext对象,也就是不管在哪个servlet里面,获取到的这个ServletContext对象都是同一个

    web.xml

      <servlet>
      	<servlet-name>hello2</servlet-name>
      	<servlet-class>com.qf.servlet.HelloServlet2</servlet-class>
      	
      </servlet>
      <servlet-mapping>
      	<servlet-name>hello2</servlet-name>
      	<url-pattern>/h2</url-pattern>
      </servlet-mapping>
      <servlet>
      	<servlet-name>hello</servlet-name>
      	<servlet-class>com.qf.servlet.HelloServlet3</servlet-class>
      </servlet>
      <servlet-mapping>
      	<servlet-name>hello</servlet-name>
      	<url-pattern>/h3</url-pattern>
      </servlet-mapping>

    servlet类

    public class HelloServlet2 extends HttpServlet {
    
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		System.out.println("helloServlet2:"+getServletContext());
    	}
    }
    -------------------------------------------------------------
    public class HelloServlet3 extends HttpServlet {
    
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		System.out.println("helloServlet3:"+getServletContext());
    		//testServletConfig();
    	}
    }

    浏览器先后访问url(http://localhost:8080/HelloServlet/h2、http://localhost:8080/HelloServlet/h3),查看console输出

    helloServlet2:org.apache.catalina.core.ApplicationContextFacade@4b020c18
    helloServlet3:org.apache.catalina.core.ApplicationContextFacade@4b020c18
    

      所以两个servlet获取的是同一个ServletContext对象

    ServletContext对象的获取

    ServletContext context = getServletContext();

    ServletContext的作用

    获取全局配置参数

    web.xml配置

      <context-param>
    	<param-name>name</param-name>
    	<param-value>wxf</param-value>
      </context-param> 
      <context-param>
    	<param-name>age</param-name>
    	<param-value>21</param-value>
      </context-param>

    servlet代码

     1 public class HelloServlet2 extends HttpServlet {
     2 
     3     @Override
     4     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     5         ServletContext context = getServletContext();
     6         Enumeration<String> names = context.getInitParameterNames();
     7         while (names.hasMoreElements()) {
     8             String name = (String) names.nextElement();
     9             System.out.println(name+"==="+context.getInitParameter(name));
    10         }
    11     }
    12 }

    console输出

    name===wxf
    age===21

    获取web工程中的资源(webContent目录下的资源)

     

    config.properties

    name=zhangsan
    age=123
    

    servlet类

     1 public class HelloServlet2 extends HttpServlet {
     2 
     3     @Override
     4     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     5         ServletContext context = getServletContext();
     6         
     7         //1.获取资源所在的绝对路径位置
     8         //这里得到的是项目在tomcat里面的根目录
     9         String path = context.getRealPath("");
    10         System.out.println("path==="+path);
    11         //这里得到的是项目在tomcat里面的根目录
    12         String path2 = context.getRealPath("config.properties");
    13         System.out.println("path2==="+path2);
    14         System.out.println("-------------------------");
    15         
    16         //2.getResourceAsStream 获取资源 流对象 相对路径
    17         InputStream stream = context.getResourceAsStream("config.properties");
    18         Properties prop = new Properties();
    19         prop.load(stream);
    20         String name = prop.getProperty("name");
    21         System.out.println("name==="+name);
    22     }
    23 }

    console输出

    path===F:eclipseworkspacedevelop_study.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsHelloServlet
    path2===F:eclipseworkspacedevelop_study.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsHelloServletconfig.properties
    -------------------------
    name===zhangsan

    注:

    1. 通过classloader去获取web工程下的资源的方式:this.getClass().getClassLoader().getResource("../../config.properties")
    2. src对应的是/WEB-INF/classes,如果想要使用ServletContext获取src目录下的文件可以使用context.getResourceAsStream("/WEB-INF/classes/config.properties")

    存取数据,servlet间共享数据 域对象

            ServletContext context = getServletContext();
            context.setAttribute("name", "qf");
            System.out.println("name==="+context.getAttribute("name"));                

    console输出

    name===qf
  • 相关阅读:
    ccc pool
    ccc 模拟重力 正太分布
    ccc 正态分布
    ccc this 指针
    基本语法HelloWorld
    css选择器
    css基本语法
    表单
    表格
    超链接
  • 原文地址:https://www.cnblogs.com/qf123/p/10045327.html
Copyright © 2020-2023  润新知