JAVAWEB四大域对象总结

四大域对象均含有的方法

1,pubic void setAttribute(String name,*Object value*):向域中存储数据,指定名称
2,public *Object* getAttribute(String name):获取域对象中指定name的值
3,public void removeAttribute(String name):删除域对象中指定name的值

Servletcontext域对象

生命周期:
  • 创建:当服务器启动时,服务器会为服务器上的每一个web项目创建一个ServletContext域对象
  • 销毁:服务器关闭的时候或者项目从服务器上移除的时候
  • 作用范围:只要项目运行着都可以使用,是最大的域对象,所有的servlet都可以访问

servletContext的作用:

  1. 应用范围域对象:
    分别创建三个servlet,分别进行存储,获取,删除数据,测试servletcontext域对象对当前所有servlet有效
    代码展示:
    step1:
    创建链接:
<body>
  <a href="${pageContext.request.contextPath}/ContextServlet">存储数据</a>
  <a href="${pageContext.request.contextPath}/Context1Servlet">获取数据</a>
  <a href="${pageContext.request.contextPath}/Context2Servlet">删除数据</a>
  </body>
step2:
	创建ContextServlet:
@WebServlet("/ContextServlet")
public class ContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //告诉浏览器响应的数据类型
        response.setContentType("text/html;charset=utf-8");
        //获取servletContext域对象,方法是从父类中继承过来的
        ServletContext sc = getServletContext();
        //存放数据到域对象中
        sc.setAttribute("wp","666");
        response.getWriter().write("存储成功!");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
step3:
	创建Context1Servlet:
@WebServlet("/Context1Servlet")
public class Context1Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //告诉浏览器响应的数据类型
        response.setContentType("text/html;charset=utf-8");
        //获取servletContext域对象,方法是从父类中继承过来的
        ServletContext sc = getServletContext();
        //获取servletContext域对象中的数据
        String wp = (String) sc.getAttribute("wp");
        response.getWriter().write("获取到的数据是:"+wp);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

step4:
	创建Context2Servlet:
@WebServlet("/Context2Servlet")
public class Context2Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //告诉浏览器响应的数据类型
        response.setContentType("text/html;charset=utf-8");
        //获取servletContext域对象,方法是从父类中继承过来的
        ServletContext sc = getServletContext();
        //删除域对象中name为“wp”的数据
        sc.removeAttribute("wp");
        response.getWriter().write("删除成功!");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

结果:当点击存储数据时将数据存入servletContext域对象中,此时点击获取数据,显示数据666,当点击删除数据之后,再点击获取数据显示null。

  1. 获取应用级的参数配置(全局配置参数,只有xml方式)
    常用方法:
    1,public String getInitParamter(String name):获取指定name的配置参数信息
    2,public Enumeration<String> getInitParamterNames():获取所有的应用级参数
    代码展示:
    step1:
    在web.xml配置应用级别的参数:
<context-param>
        <param-name>wp</param-name>
        <param-value>66</param-value>
    </context-param>
    <context-param>
        <param-name>wpp</param-name>
        <param-value>666</param-value>
    </context-param>
step2:
	创建Deno1Servlet:
@WebServlet("/Demo1Servlet")
public class Demo1Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取servletContext域对象
        ServletContext sc = getServletContext();
        //获取wp的取值
        String wp = sc.getInitParameter("wp");
        System.out.println(wp);
        //获取所有的配置参数
        Enumeration<String> ipn = sc.getInitParameterNames();
        //遍历
        while (ipn.hasMoreElements()){
            String s = ipn.nextElement();
            System.out.println(sc.getInitParameter(s));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

输出结果:
66
666
66

  • 获取web资源的真实路径
    常用方法:
    1,public string getRealPath(“/路径”):获取真实路径(servlet3.0之前必须/开头)
    2,public inputstream getresourceAsStream(“/路径”):获取到此的输入流对象
    代码展示:
@WebServlet("/Demo2Servlet")
public class Demo2Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取servletContext域对象
        ServletContext sc = getServletContext();
        //获取真实路径
        String realPath = sc.getRealPath("/images/5.jsp");
        System.out.println("真实路径:"+realPath);
        //输出结果:真实路径:E:\idea工作区间\javaweb129\out\artifacts\MyTest_war_exploded\images\5.jpg
        //getrealpath()方法不管路径存不存在,都可以获取到真实路径
        //需要注意的是,此图片的路径在web下的images文件夹下
        InputStream is = sc.getResourceAsStream("/images/5.jpg");
        ServletOutputStream os = response.getOutputStream();
        int len;
        byte[] b=new byte[1024];
        while ((len=is.read(b))!=-1){
            os.write(b,0,len);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

HttpSession域对象

生命周期:

  • 创建:java中,在一次会话中第一次调用getSession()时由服务器创建;jsp中,在一次会话中第一次访问任意一个jsp界面时就会创建
  • 销毁:
    调用invalidate()方法时
    服务器超时时:web服务器指定了默认超时时间,tomcat是30分钟
//此代码在tomcat的conf目录的web.xml中
<session-config>
        <session-timeout>30</session-timeout>
    </session-config>
服务器关闭时:服务器关闭时,我们可以通过钝化(将session数据保存到硬盘中)和活化(将session数据回复到session中)
  • 作用范围:一次会话中,涉及的servlet和jsp都可以使用

HttpServletRequest域对象

生命周期:

  • 创建:浏览器请求时由服务器创建
  • 销毁:响应生成时由服务器销毁
  • 作用范围:只在一次请求中
  • 注意!request域对象中的数据在转发时可以在两个servlet中共享数据,但是在重定向中不可以
    代码演示:
    step1:
    创建Demo3Servlet向域对象中存储数据
@WebServlet("/Demo3Servlet")
public class Demo3Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //向httpservletrequest域对象中存储数据
        request.setAttribute("data","一对数据");
        //转发到Demo4Servlet
        request.getRequestDispatcher("Demo4Servlet").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
step2:
	在此域对象中获取到了转发来的数据
@WebServlet("/Demo4Servlet")
public class Demo4Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取域中的数据
        String data = (String) request.getAttribute("data");
        //输出:一对数据
        System.out.println(data);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

PageContext域对象

生命周期:

  • 创建:当对jsp的请求开始时创建

  • 销毁:当响应结束后销毁

  • 作用范围:整个页面(是四大域对象中作用范围最小的)
    作用:

  • 获取其他八大内置对象
    getException()返回Exception。
    getPage()返回Page。
    getRequest()返回request。
    getResponse()返回response。
    getServletConfig()返回config。
    getServletContext()返回application。
    getSession()返回session。
    getOut()返回out

  • 作为域对象

    扫描二维码关注公众号,回复: 10670171 查看本文章
  • 可以在当前界面操作其他三大域对象
    setAttribute(String key,String value,int 哪个域) :
    getAttribute(String key,int 哪个域)
    removeAttribute(String key,int 哪个域)
    findAttribute(String key) :依次从jsp的四个域中从小到大(域的范围)挨个查找指定的key.若找到直接返回,终止查找,若没 有返回null

发布了1 篇原创文章 · 获赞 0 · 访问量 1

猜你喜欢

转载自blog.csdn.net/SUPER__W/article/details/105440298