• ServletContext的使用


    ServletContext:

    ServletContext表示Servlet应用程序。每个Web应用程序只有一个上下文。在将一个应用程序同时部署到多个容器的分布式环境中,每台Java虚拟机上的Web应用都会有一个ServletContext对象。

    通过在ServletConfig中调用getServletContext方法,可以获得ServletContext。

    有了ServletContext,就可以共享从应用程序中的所有资料处访问到的信息,并且可以动态注册Web对象。前者将对象保存到ServletContext中的一个内部的Map中。保存在ServletContext中的对象被称作属性。

    1:实现数据共享 setAttribute(key,value)  getAttribute(key) ,获取域对象

    2:获取全局配置信息: getInitParameter() 

    3:获得应用下任何资源的路径

    例一:通过调用GenericServlet的 getServletContext对象得到ServletContext对象,获取域对象

    先运行myservlet1.java,在运行myservlet2.java

    myservlet1.java

    public class myservlet extends HttpServlet {
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		//通过调用GenericServlet的 getServletContext对象得到ServletContext对象
    		//先访问demo1,在访问demo2
    		ServletContext application=this.getServletContext();
    		application.setAttribute("name", "tom");
    		System.out.println(application.getClass().getName());
    	}
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		doGet(request,response);
    	}
    }
    

    myservlet2.java

    public class myservlet2 extends HttpServlet {
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		String name=(String) this.getServletContext().getAttribute("name");
    		
    		if(name==null){
    			System.out.println("你不能直接访问这个类");
    		}
    		System.out.println(name);
    	}
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		doGet(request,response);
    	}
    }
    

      运行效果图:

    例二:获取全局配置信息

    获取encoding的配置信息

    myservlet3.java

    public class myservlet3 extends HttpServlet {
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		String encoding=this.getServletContext().getInitParameter("encoding");
    		System.out.println(encoding);
    	}
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		doGet(request,response);
    	}
    }
    

     web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <display-name></display-name>
      <context-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
      </context-param>
      <servlet>
        <servlet-name>myservlet</servlet-name>
        <servlet-class>com.zk.myservlet.myservlet</servlet-class>
      </servlet>
      <servlet>
        <servlet-name>myservlet2</servlet-name>
        <servlet-class>com.zk.myservlet.myservlet2</servlet-class>
      </servlet>
      <servlet>
        <servlet-name>myservlet3</servlet-name>
        <servlet-class>com.zk.myservlet.myservlet3</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>myservlet</servlet-name>
        <url-pattern>/demo1</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>myservlet2</servlet-name>
        <url-pattern>/demo2</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>myservlet3</servlet-name>
        <url-pattern>/demo3</url-pattern>
      </servlet-mapping>	
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    例三:获取资源路径

    使用this.getServletContext().getRealPath获取文件的资源路径

    public class myservlet1 extends HttpServlet {
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		String apath=this.getServletContext().getRealPath("/WEB-INF/a.properties");//参数一定要以斜杠开头
    		System.out.println(apath);
    		//创建一个properties
    		Properties pro=new Properties();
    		pro.load(new FileInputStream(apath));
    		System.out.println(pro.getProperty("akey"));
    		
    		String bpath=this.getServletContext().getRealPath("/WEB-INF/classes/b.properties");
    		System.out.println(bpath);
    		Properties pro2=new Properties();
    		pro2.load(new FileInputStream(bpath));
    		System.out.println(pro2.getProperty("key"));
    		
    		String cpath=this.getServletContext().getRealPath("/WEB-INF/classes/com/zk/myservlet/c.properties");
    		System.out.println(cpath);
    		Properties pro3=new Properties();
    		pro3.load(new FileInputStream(cpath));
    		System.out.println(pro3.get("ckey"));
    	}
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		doGet(request,response);
    	}
    }
    

      运行结果图:

  • 相关阅读:
    MessageBoxButtons.OKCancel的选择事件
    Markdown 学习
    【Python】tesserocr的Path错误
    【Python套接字】socket编程
    【Python数据】懒人修仙传数值
    【Python画画】失败案例总结
    【Python画画】.ui文件转.py文件
    【Python截图】截图处理
    【想法】想做一个辅助工具
    【Python爬虫】从html里爬取中国大学排名
  • 原文地址:https://www.cnblogs.com/longlyseul/p/9943030.html
Copyright © 2020-2023  润新知