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中共享数据,但是在重定向中不可以

HttpServletRequest的作用:

  1. 请求范围域对象:
    分别创建两个servlet,在Demo3Servlet中向域对象中存储数据,在Demo4Servlet域对象中取出数据
    代码演示:
    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);
    }
}
  1. 获取请求内容
    -获取请求行内容
    常用方法:
    1,public string getMethod():获取请求方式
    2,public string getRequestURI():获取浏览器端的请求uri
    3,public stringbuffer getRequestURL():获取浏览器端的请求url(协议,主机端口,资源地址)
    4,public string getProtocol():获取浏览器端使用的协议和版本号
    5,public string getContextPath():获取虚拟路径
    6,public string getRemoteAttr():获取来访者的ip地址
    代码展示:
@WebServlet("/Demo5Servlet")
public class Demo5Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getMethod();
        System.out.println("浏览器端的请求方式是:"+method);
        String requestURI = request.getRequestURI();
        System.out.println("浏览器端的uri地址是:"+requestURI);
        StringBuffer requestURL = request.getRequestURL();
        System.out.println("浏览器端的url地址是:"+requestURL);
        String protocol = request.getProtocol();
        System.out.println("浏览器端的协议及其版本号是:"+protocol);
        String contextPath = request.getContextPath();
        System.out.println("虚拟地址是:"+contextPath);
        String remoteAddr = request.getRemoteAddr();
        System.out.println("浏览器端的ip地址是:"+remoteAddr);

    }

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

结果:
浏览器端的请求方式是:GET
浏览器端的uri地址是:/mytest/Demo5Servlet
浏览器端的url地址是:http://localhost:8080/mytest/Demo5Servlet
浏览器端的协议及其版本号是:HTTP/1.1
虚拟地址是:/mytest
浏览器端的ip地址是:0:0:0:0:0:0:0:1

-获取请求消息头内容
常用方法:
1,public string getHeader(String name):获取指定name的消息头
2,public long getDateHeader(String name):获取取值为毫秒值的消息头
3,public int getIntHeader(String name):获取取值为整数类型的消息头
4,public enumeration getHeaderNames():获取所有的请求消息头
5,public enumeration getHeaders(String name):获取指定name的有多个值的消息头

-获取请求正文内容
注意!只有post方式才有请求正文,请求数据类型必须是application/x-www-form-urlencoded
常用方法:
1,public servletinputstream getInputstream():以字节输入流的形式获取正文
2,public bufferedreader getreader():以字符流的形式获取正文
3,public String getparameter(String name):获取指定name的参数值
4,public String[] getparametervalues(String name):获取指定name的参数值(多个值)
5,public enumeration<String> getparameterNames():获取所有的参数名
6,public map<String,String[]> getparameterMap():获取所有的请求参数,键为string,值为数 组类型

PageContext域对象

生命周期:

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

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

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

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

  • 作为域对象

  • 可以在当前界面操作其他三大域对象
    setAttribute(String key,String value,int 哪个域) :
    getAttribute(String key,int 哪个域)
    removeAttribute(String key,int 哪个域)
    findAttribute(String key) :依次从jsp的四个域中从小到大(域的范围)挨个查找指定的key.若找到直接返回,终止查找,若没 有返回null

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值