Servlet——ServletContext

一、ServletConfigjavax.servlet.ServletContext包;

  //对应jsp中的application

  //其中定义了很多方法使得Servlet可以和tomcat进行数据交互和罗基交换;

  //ServletContext在当前web程序和jvm中唯一,允许在多个Servlet之间数据的共享;

二、获取ServletContext对象:

  (1)通过GenericServlet提供的 getServletContext();

    //this.getServletContext();

  (2)通过ServletConfig提供的getServletContext();

    //this.getServletConfig().getServletContext();

  (3)通过HttpServletRequest获取;

    //req.getServletContext();

  (4)通过HttpSession获取;

    //req.getSession().getServletContext();

 四、操作数据方法://域方法都包含属性方法;

  1.设置属性:void setAttribute(String name, Object object);//多次调用同个name属性会覆盖;

  2.获取属性:Object getAttribute(String name);//获取没设置过的属性名返回null;

  3.删除属性:void removeAttribute(String name);

  4.获取所有本域的属性名:Enumeration getAttributeNames();

@WebServlet("/test")
public class Test extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext servletContext = getServletContext();
        //设置属性
        servletContext.setAttribute("name", "zs");
        servletContext.setAttribute("name", "ww");
        servletContext.setAttribute("age", 14);
        //获取指定属性
        System.out.println(servletContext.getAttribute("name"));
        //删除属性
        servletContext.removeAttribute("name");
        //获取所有属性
        Enumeration<String> attributeNames = servletContext.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String name = attributeNames.nextElement();
            Object value = servletContext.getAttribute(name);
            System.out.println(name+":"+value);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

五、获取资源方法 

  (1)获取项目名:String getContextPath();

    //返回的项目名开头包含/斜杠;

  (2)获取真实路径:String getRealPath(String path);

    //传入的path前必须包含斜杠;“/a.txt”

  (3)获取路径下所有资源路径:Set getResourcePaths("/WEB-INF");

  (4)获取资源流:InputStream getResourceAsStream("/WEB-INF/b.txt");

  (5)获取参数值:String getInitParameter(String name);

    //根据名字返回当前程序所包含的初始化参数的值;<context-param>

  (6)获取参数值:Enumeration<String>        getInitParameterNames()  

    //返回当前程序中所有的初始化参数的名字的枚举的引用(集合);

  (7)获取编码:String getRequestCharacterEncoding();

  (8)设置编码:void setRequestCharacterEncoding(String encoding);

@WebServlet("test")
public class Test extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象;
        ServletContext servletContext = getServletContext();
        //返回项目名,带/斜杠;/review
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
        //返回文件的真实路径;E:\JavaProject\review2\web\index.jsp
        String realPath = servletContext.getRealPath("/index.jsp");
        System.out.println(realPath);
        //返回路径下所有资源路径;
        Set<String> resourcePaths = servletContext.getResourcePaths("/WEB-INF");
        for (Iterator<String> iterator = resourcePaths.iterator(); iterator.hasNext(); ) {
            String next = iterator.next();
            System.out.println(next);
        }
        //获取资源流
        InputStream stream = servletContext.getResourceAsStream("/WEB-INF/b.txt");
        System.out.println(stream);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

猜你喜欢

转载自www.cnblogs.com/Tractors/p/11263454.html