• ServletContext对象,,,,,


    ServletContext对象

    1.什么是ServletContext对象(代表整个项目的对象)

    ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象     内部封装是该web应用的信息,ServletContext对象一个web应用只有一个

    问题:一个web应用有几个servlet对象?----多个

    ServletContext对象的生命周期?

    创建:该web应用被加载(服务器启动或发布web应用(前提,服务器启动状            态))

    销毁:web应用被卸载(服务器关闭,移除该web应用)

    开启服务器时创建

    销毁服务器时销毁

    2.怎样获得ServletContext对象

    1)ServletContext servletContext = config.getServletContext();

    2)ServletContext servletContext = this.getServletContext();

     

    //获取ServletContext对象
            ServletContext context=getServletContext();

    3.ServletContext的作用

    (1)获得web应用中任何资源的绝对路径(重要 重要 重要)

    在服务器上的绝对路径

    方法:String path = context.getRealPath(相对于该web应用的相对地址);

    写一个相对,得到一个绝对的path

    通过相对路径获取绝对路径,相对路径相也对于服务器

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取ServletContext对象
            ServletContext context=getServletContext();
            //获取通过相对路径获取绝对路径
            String patha=context.getRealPath("a.txt");
            String pathb=context.getRealPath("WEB-INF/b.txt");
            String pathc=context.getRealPath("WEB-INF/classes/c.txt");
            System.out.println(patha);
            System.out.println(pathb);
    
            System.out.println(pathc);
    
        }

    (2)ServletContext是一个域对象(重要 重要 重要)

    什么是域对象?什么是域?

    存储数据的区域就是域对象

     

    ServletContext域对象的作用范围:整个web应用(所有的web资源都可以随意向      servletcontext域中存取数据,数据可以共享)

    Servlet01封存消息,
    Servlet02获取消息
    public class Servlet01 extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext context=getServletContext();
    //存值 context.setAttribute(
    "name", "小红帽"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
    public class Servlet02 extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext context=getServletContext();
    //取值 String name
    =(String)context.getAttribute("name"); System.out.println(name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

    域对象的通用的方法:

    setAtrribute(String name,Object obj);

    getAttribute(String name);

    removeAttribute(String name);

    登录,显示第几个登录的

    public class LoginServlet extends HttpServlet {
    private UserService userService=new UserService();
    //对自定义数据进行初始化
        public void init() throws ServletException {
            int count=0;
            //获取ServletContext 对象
            ServletContext context=getServletContext();
            context.setAttribute("count", count);
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String uname=request.getParameter("uname");
            String pwd=request.getParameter("pwd");
            //调Service层方法
            int count=userService.login(uname, pwd);
            if(count>0){
                //获取ServletContext对象
                ServletContext context=getServletContext();
                //取值
                int c=(int)context.getAttribute("count");
                int num=++c;
                //存值
                context.setAttribute("count", num);
                response.getWriter().write("you are "+num+" success");
            }else{
                response.getWriter().write("fail");
            }
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }

     

     

  • 相关阅读:
    四层架构设计实践
    看看node.js chat程序如何实现Ajax longpolling长链接刷新模式
    模仿igoogle【定制化、拖动排序,最大化、分屏】
    安装和配置Apache
    好书推荐《Pro ASP.NET MVC 3 Framework 3rd Edition》
    GAC和VS引用的程序集不一致?
    不要在 ASP.NET 4.5 Beta 的 Page 类事件上直接使用 async 与 await
    使用事务自动回滚来实现单元测试
    C# 如何异步查询数据库
    Linq + Jquery + Ajax 实现异步分页,批量删除,单个删除,全选,反选 ……
  • 原文地址:https://www.cnblogs.com/111wdh/p/13475468.html
Copyright © 2020-2023  润新知