zoukankan      html  css  js  c++  java
  • jsp课程笔记(三)四种范围对象

    客户端在第一次请求服务端时,如果服务端发现 此请求没有 JSESSIONID,则会创建一个 name=JSESIONID的cookie 并返回给客户端

    Cookie:
    a.不是内对对象,要使用必须new
    b.但是,服务端会 自动生成一个(服务端自动new一个cookie) name=JSESIONID的cookie 并返回给客户端

    JSP9大内置对象

    • pageContext JSP页面容器
    • request 请求对象
    • session 会话对象
    • appliation 全局对象
    <%="当前项目的虚拟路径:" +application.getContextPath() +"<br/>"	%>
    <%="虚拟路径对应的绝对路径:" +application.getRealPath("/MyJspProject") +"<br/>"	%>
    
    • response 响应对象

    • config 配置对象(服务器配置信息)

    • out 输出对象

    • page 当前JSP页面对象(相当于java中的this)

    • exception 异常对象

    四种范围对象(小->大)

    • pageContext JSP页面容器 (page对象); 当前页面有效
    • request 请求对象 同一次请求有效
    • session 会话对象 同一次会话有效
    • appliation 全局对象 全局有效(整个项目有效)

    以上4个对象共有的方法:

    • Object getAttribute(String name):根据属性名,或者属性值
    • void setAttribute(String name,Object obj) :设置属性值(新增,修改)
    • setAttribute(“a”,“b”) ;//如果a对象之前不存在,则新建一个a对象 ;
      如果a之前已经存在,则将a的值改为b
    • void removeAttribute(String name):根据属性名,删除对象

    a.
    pageContext 当前页面有效 (页面跳转后无效)
    案例:
    pageContext.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    		<%
    			pageContext.setAttribute("hello", "world");
    			request.getRequestDispatcher("pc1.jsp").forward(request, response);
    		%>
    		
    		
    </body>
    </html>
    

    pc1.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	pc1.jsp<br>
    		<%=pageContext.getAttribute("hello") %>
    
    </body>
    </html>
    

    在这里插入图片描述
    b.
    request 同一次请求有效;其他请求无效 (请求转发后有效;重定向后无效)
    在这里插入图片描述
    案例(一):request请求转发取值
    request.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    		<%
    			request.setAttribute("hello", "world");
    			request.getRequestDispatcher("rq1.jsp").forward(request, response);
    		%>
    </body>
    </html>
    

    rq1.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    		pq1.jsp<br>
    		<%=request.getAttribute("hello") %>
    </body>
    </html>
    

    在这里插入图片描述
    案例(二):request重定向取值
    把上面案例(一)的请求转发改为重定向

    <%
    			request.setAttribute("hello", "world");
    			//request.getRequestDispatcher("rq1.jsp").forward(request, response);
    			response.sendRedirect("rq1.jsp");
    		%>
    

    再次访问:
    在这里插入图片描述

    重定向为两次请求,所以request获取不到值。
    

    c.
    session 同一次会话有效 (无论怎么跳转,都有效;关闭/切换浏览器后无效 ; 从 登陆->退出 之间 全部有效)
    session案例:
    session.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<%
    			session.setAttribute("hello", "world");
    			//request.getRequestDispatcher("rq1.jsp").forward(request, response);
    			response.sendRedirect("ss1.jsp");
    		%>
    </body>
    </html>
    

    ss1.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    		ss1.jsp<br>
    		<%=session.getAttribute("hello") %>
    </body>
    </html>
    

    在这里插入图片描述
    切换浏览器后直接访问ss1.jsp无效:
    在这里插入图片描述

    d.
    application

    • 全局变量;整个项目运行期间 都有效 (切换浏览器 仍然有效);
    • 关闭服务、其他项目 无效
      案例:
      application.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<%
    			application.setAttribute("hello", "world");
    			//request.getRequestDispatcher("rq1.jsp").forward(request, response);
    			response.sendRedirect("ap1.jsp");
    		%>
    </body>
    </html>
    

    ap1.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    		ap1.jsp<br>
    		<%=application.getAttribute("hello") %>
    </body>
    </html>
    

    在这里插入图片描述
    切换浏览器访问也有效
    在这里插入图片描述
    重启Tomcat后无效:
    在这里插入图片描述
    ->多个项目共享、重启后仍然有效 :JNDI

    1.以上的4个范围对象,通过 setAttribute()赋值,通过getAttribute()取值;
    2.以上范围对象,尽量使用最小的范围。因为 对象的范围越大,造成的性能损耗越大。

  • 相关阅读:
    2017.04.05-2017.07.14封闭开发总结
    Android读取Manifest文件下Application等节点下的metadata自定义数据
    MyEclipse Hibernate Reverse Engineering 找不到项目错误
    web服务器决定支持多少人同时在线的因素
    配置servers时,错误:Setting property 'source' to 'org.eclipse.jst.jee.server:hczm' did not find a matching property
    查看端口被占用
    高德开发 android 出现 key 鉴权失败
    Android EventBus
    javascript 中的数据驱动页面模式
    读书笔记之
  • 原文地址:https://www.cnblogs.com/xdr630/p/15255106.html
Copyright © 2011-2022 走看看