JSP中四种属性保存范围(1)
一、四种属性范围
在JSP中提供了四种属性保存范围
page:在一个页面内保存属性,跳转之后无效
request:在一次服务请求范围内,服务器跳转后依然有效
session:-在一次会话范围内,无论何种跳转都可以使用,但是新开浏览器无法使用
application:在整个服务器上保存,所有用户都可以使用
名称 |
作用域 |
application |
在所有应用程序中有效 |
session |
在当前会话中有效 |
request |
在当前请求中有效 |
page |
在当前页面有效 |
置统一的请求编码:request.setCharacterEncoding("GBK");
- 请求方式:<%=request.getMethod()%><br>
- 请求的资源:<%=request.getRequestURI()%><br>
- 请求用的协议:<%=request.getProtocol()%><br>
- 请求的文件名:<%=request.getServletPath()%><br>
- 请求的服务器的IP:<%=request.getServerName()%><br>
- 请求服务器的端口:<%=request.getServerPort()%><br>
- 客户端IP地址:<%=request.getRemoteAddr()%><br>
- 客户端主机名:<%=request.getRemoteHost()%><br>
四种属性范围:
page(pageContext):只在一个页面中保存属性。 跳转之后无效。
request:只在一次请求中有效,服务器跳转之后有效。 客户端跳无效
session:再一次会话中有效。服务器跳转、客户端跳转都有效。 网页关闭重新打开无效
application:在整个服务器上保存,所有用户都可使用。 重启服务器后无效
注意:如果设置过多的application属性范围会影响服务器性能 page(pageContext)对象:
pageContext.PAGE_SCOPE
pageContext.REQUEST_SCOPE
pageContext.SESSION_SCOPE
pageContext.APPLICATION_SCOPE
pageContext.setAttribute("name", "liuyang", pageContext.APPLICATION_SCOPE); request对象:
通过地址重写的方式进行传值:
要访问的网页地址?参数1=值1&参数2=值2&...
Request常用的方法:
1.01 getParameter(String strTextName) 获取表单提交的信息。
String strName=request.getParameter("name");
1.02 getProtocol() 获取客户使用的协议。
String strProtocol=request.getProtocol();
1.03 getServletPath() 获取客户提交信息的页面。
String strServlet=request.getServletPath();
1.04 getMethod() 获取客户提交信息的方式,get|post。
String strMethod = request.getMethod();
1.05 getHeade() 获取HTTP头文件中的accept、accept-encoding和Host的值。
String strHeader = request.getHeader("accept");
1.06 getRermoteAddr() 获取客户的IP地址。
String strIP = request.getRemoteAddr();
1.07 getRemoteHost() 获取客户机的名称。
String clientName = request.getRemoteHost();
1.08 getServerName() 获取服务器名称。
String serverName = request.getServerName();
1.09 getServerPort() 获取服务器的端口号。
int serverPort = request.getServerPort();
1.10 getParameterNames() 获取客户端提交的所有参数的名字。
Enumeration enum = request.getParameterNames();
while(enum.hasMoreElements()){
String s=(String)enum.nextElement();
out.println(s);
}
1.11 getParameterValues()获取客户端提交的所有参数的名字对应的值。
String[] inst = request.getParameterValues(paramName);
for(String ss:inst){
System.out.println(ss);
} response对象:
设置头信息setHeader():
response.setHeader("refresh","5; url=hello.jsp"); 5秒后自动跳转(jsp)
<meta http-equiv="refresh" content="5;url=hello.jsp" > 5秒后自动跳转(html)
<jsp:forward/> 与 sendRedirect 区别:
<jsp:forward page="hello.jsp"></jsp:forward> 执行到该语句立即跳转,后面的语句不执行
response.sendRedirect("hello.jsp"); 重定向。 执行完此页面再进行跳转
cookie: 设置在客户端,安全性比较低
一个客户端上最多只能保存300个cookie。
addCookie();
setHeader("Set-Cookie","名字=值");
response.setHeader("refresh","1"); //每隔1秒刷新一次
response.addCookie();
Cookie [] c = request.getCookies();
session对象:每一个session代表了一个用户。
获取session ID:
String id = session.getId();
String id = session.getId(); 获取session ID Manager 可以将session保存在本地上
long start = session.getCreationTime(); //创建此会话的时间
long end = session.getLastAccessedTime();//客户端上一次发送与此会话关联的请求的时间,最后一次操作此会话时间
1.page属性范围:
1.设置page无跳转 属性范围,在当前jsp页面有效
1 <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>page属性范围</title>
</head>
<body>
<%
//设置page 属性范围,在当前jsp页面有效
pageContext.setAttribute("name", "liuyang");
pageContext.setAttribute("age", "23");
pageContext.setAttribute("birthday", new Date());
%>
<%
String name=(String)pageContext.getAttribute("name");
int age=Integer.parseInt((String)pageContext.getAttribute("age"));
Date birthday = (Date)pageContext.getAttribute("birthday");
%>
<h1>姓名:<%=name %></h1>
<h1>年龄:<%=age %></h1>
<h1>生日:<%=birthday %></h1>
</body>
</html>
2.设置page 属性范围-服务器跳转,服务器跳转后无效
// page02.jsp
1 <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>page属性范围-服务器跳转</title>
</head>
<body>
<%
//设置page(pageContext) 属性范围,在当前jsp页面有效,只在一个页面中保存属性。 跳转之后无效。
pageContext.setAttribute("name", "liuyang");
pageContext.setAttribute("birthday", new Date());
%>
<jsp:forward page="page_03.jsp"></jsp:forward>
</body>
</html>
//page03.jsp
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>page属性范围-服务器跳转</title>
</head>
<body>
<%
String name=(String)pageContext.getAttribute("name");
Date birthday = (Date)pageContext.getAttribute("birthday");
%>
<h1>姓名:<%=name %></h1>
<h1>生日:<%=birthday %></h1>
</body>
</html>
3.设置page 属性范围-客户端跳转,客户端跳转后无效
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>page属性范围-客户端跳转</title>
</head>
<body>
<%
//设置page(pageContext) 属性范围,在当前jsp页面有效,只在一个页面中保存属性。 跳转之后无效。
pageContext.setAttribute("name", "liuyang");
pageContext.setAttribute("birthday", new Date());
%>
<a href="page_03.jsp">跳转</a> </body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>page属性范围-客户端跳转</title>
</head>
<body>
<%
String name=(String)pageContext.getAttribute("name");
Date birthday = (Date)pageContext.getAttribute("birthday");
%>
<h1>姓名:<%=name %></h1>
<h1>生日:<%=birthday %></h1>
</body>
</html>
2.request属性范围:
1.设置request 属性范围 - 服务器跳转,服务器跳转后有效, 客户端跳转后无效
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>request属性范围—服务器跳转</title>
</head>
<body>
<%
//request:只在一次请求中有效,服务器跳转之后有效。
request.setAttribute("name", "liuyang");
request.setAttribute("birthday", new Date());
%>
<jsp:forward page="request_02.jsp"></jsp:forward> </body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>request属性范围—服务器跳转</title>
</head>
<body>
<%
//request:只在一次请求中有效,服务器跳转之后有效。 客户端跳转后无效
String name=(String)request.getAttribute("name");
Date birthday = (Date)request.getAttribute("birthday");
%>
<h1>姓名:<%=name %></h1>
<h1>生日:<%=birthday %></h1>
</body>
</html>
2.设置request 属性范围 客户端跳转,客户端跳转后无效
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>request属性范围—客户端跳转</title>
</head>
<body>
<%
//request:只在一次请求中有效,服务器跳转之后有效。 客户端跳无效
request.setAttribute("name", "liuyang");
request.setAttribute("birthday", new Date());
%>
<a href="request_02.jsp">跳转</a>
</body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>request属性范围—客户端跳转</title>
</head>
<body>
<%
//request:只在一次请求中有效,服务器跳转之后有效。 客户端跳转后无效
String name=(String)request.getAttribute("name");
Date birthday = (Date)request.getAttribute("birthday");
%>
<h1>姓名:<%=name %></h1>
<h1>生日:<%=birthday %></h1>
</body>
</html>
3.session属性范围:
1.session:服务器跳转。 再一次会话中有效。服务器跳转、客户端跳转都有效。 浏览器关闭重新打开无效
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>session 服务器跳转</title>
</head>
<body>
<%
//session:再一次会话中有效。服务器跳转、客户端跳转都有效。 网页关闭重新打开无效
session.setAttribute("name", "liuyang");
session.setAttribute("birthday", new Date());
%>
<jsp:forward page="session_02.jsp"></jsp:forward>
</body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>session 服务器跳转</title>
</head>
<body>
<%
//session:再一次会话中有效。服务器跳转、客户端跳转都有效。 网页关闭重新打开无效
String name=(String)session.getAttribute("name");
Date birthday = (Date)session.getAttribute("birthday");
%>
<h1>姓名:<%=name %></h1>
<h1>生日:<%=birthday %></h1>
</body>
</html>
2.session 客户端跳转
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>session 客户端跳转</title>
</head>
<body>
<%
//session:再一次会话中有效。服务器跳转、客户端跳转都有效。 网页关闭重新打开无效
session.setAttribute("name", "liuyang");
session.setAttribute("birthday", new Date());
%>
<a href="session_02.jsp">跳转</a>
</body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>session 客户端跳转</title>
</head>
<body>
<%
//session:再一次会话中有效。服务器跳转、客户端跳转都有效。 网页关闭重新打开无效
String name=(String)session.getAttribute("name");
Date birthday = (Date)session.getAttribute("birthday");
%>
<h1>姓名:<%=name %></h1>
<h1>生日:<%=birthday %></h1>
</body>
</html>
3.关闭浏览器重新打开进入session_02.jsp无效
4.application属性范围:
1.application:服务器跳转 在整个服务器上保存,所有用户都可使用。 重启服务器后无效
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>application 服务器跳转</title>
</head>
<body>
<%
//application:在整个服务器上保存,所有用户都可使用。 重启服务器后无效
//application.setAttribute("name", "liuyang");
pageContext.setAttribute("name", "liuyang", pageContext.APPLICATION_SCOPE);
application.setAttribute("birthday", new Date());
%>
<jsp:forward page="application_02.jsp"></jsp:forward>
</body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>application 服务器跳转</title>
</head>
<body> <%
//application:在整个服务器上保存,所有用户都可使用。 重启服务器后无效
String name=(String)application.getAttribute("name");
Date birthday = (Date)application.getAttribute("birthday");
%>
<h1>姓名:<%=name %></h1>
<h1>生日:<%=birthday %></h1>
</body>
</html>
2.application:客户端跳转 在整个服务器上保存,所有用户都可使用。 重启服务器后无效
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>application 客户端跳转</title>
</head>
<body>
<%
//application:在整个服务器上保存,所有用户都可使用。 重启服务器后无效
//application.setAttribute("name", "liuyang");
pageContext.setAttribute("name", "liuyang", pageContext.APPLICATION_SCOPE);
application.setAttribute("birthday", new Date());
%>
<a href="application_02.jsp">跳转</a>
</body>
</html>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>application 客户端跳转</title>
</head>
<body> <%
//application:在整个服务器上保存,所有用户都可使用。 重启服务器后无效
String name=(String)application.getAttribute("name");
Date birthday = (Date)application.getAttribute("birthday");
%>
<h1>姓名:<%=name %></h1>
<h1>生日:<%=birthday %></h1>
</body>
</html>
3.application:关闭浏览器重新进入application_02.jsp 在整个服务器上保存,所有用户都可使用。 重启服务器后无效
4.application:重启服务器后 重新进入application_02.jsp 在整个服务器上保存,所有用户都可使用。 重启服务器后无效
四种属性范围:page(pageContext):只在一个页面中保存属性。 跳转之后无效。
request:只在一次请求中有效,服务器跳转之后有效。 客户端跳无效
session:再一次会话中有效。服务器跳转、客户端跳转都有效。网页关闭重新打开无效
application:在整个服务器上保存,所有用户都可使用。重启服务器后无效
注意:如果设置过多的application属性范围会影响服务器性能
page(pageContext): 对象
pageContext.REQUEST_SCOPE
pageContext.SESSION_SCOPE
pageContext.APPLICATION_SCOPE
pageContext.setAttribute("name", "liuyang", pageContext.APPLICATION_SCOPE);
request对象:
通过地址重写的方式进行传值:要访问的网页地址?参数1=值1&参数2=值2&...
Request常用的方法:
1.01 getParameter(String strTextName) 获取表单提交的信息。
String strName=request.getParameter("name");
1.02 getProtocol() 获取客户使用的协议。
String strProtocol=request.getProtocol()
;1.03 getServletPath() 获取客户提交信息的页面。
String strServlet=request.getServletPath();
1.04 getMethod() 获取客户提交信息的方式,get|post。
String strMethod = request.getMethod();
1.05 getHeade() 获取HTTP头文件中的accept、accept-encoding和Host的值。
String strHeader = request.getHeader("accept");
1.06 getRermoteAddr() 获取客户的IP地址。
String strIP = request.getRemoteAddr();1.07 getRemoteHost() 获取客户机的名称。
String clientName = request.getRemoteHost();1.08 getServerName() 获取服务器名称。
String serverName = request.getServerName();1.09 getServerPort() 获取服务器的端口号。
int serverPort = request.getServerPort();1.10 getParameterNames() 获取客户端提交的所有参数的名字。
Enumeration enum = request.getParameterNames();
while(enum.hasMoreElements()){
String s=(String)enum.nextElement();
out.println(s); }
1.11 getParameterValues()获取客户端提交的所有参数的名字对应的值。
String[] inst = request.getParameterValues(paramName);
for(String ss:inst){
System.out.println(ss);
}
response对象:
设置头信息setHeader():
response.setHeader("refresh","5; url=hello.jsp");5秒后自动跳转(jsp)
<meta http-equiv="refresh" content="5;url=hello.jsp" > 5秒后自动跳转(html)
<jsp:forward/> 与 sendRedirect 区别:
<jsp:forward page="hello.jsp"></jsp:forward> 执行到该语句立即跳转,后面的语句不执行
response.sendRedirect("hello.jsp");重定向。 执行完此页面再进行跳转
cookie: 设置在客户端,安全性比较低一个客户端上最多只能保存300个cookie。
addCookie();
setHeader("Set-Cookie","名字=值");
session对象:每一个session代表了一个用户。
获取session ID:String id = session.getId();
JSP中四种属性保存范围(1)的更多相关文章
- JSP中四种属性保存范围(2)
1.session <%@ page language="java" contentType="text/html" pageEncoding=" ...
- jsp中四种传递参数的方法
jsp中四种传递参数的方法如下: 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="i ...
- css样式表中四种属性选择器
学习此连接的总结http://developer.51cto.com/art/201009/226158.htmcss样式表中四种属性选择器1> 简易属性 tag[class]{ font-we ...
- (转)JSP中四种传递参数的方法:
1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...
- JSP中四种传递参数中文乱码问题
查看来源:http://blog.csdn.net/hackerain/article/details/6776083
- JSP九大内置对象和四种属性范围解读
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文首先主要解说了JSP中四种属性范围的概念.用法与实例. 然后在这个基础之上又引入了九 ...
- Jsp学习总结(1)——JSP九大内置对象和四种属性范围解读
一.四种属性范围 1.1.在JSP中提供了四种属性保存范围 page:在一个页面内保存属性,跳转之后无效 request:在一次服务请求范围内,服务器跳转后依然有效 session:-在一次会话范围内 ...
- jsp四种属性范围
在JSP提供了四种属性的保存范围.所谓的属性保存范围,指的就是一个设置的对象,可以在多个页面中保存并可以继续使用.它们分别是:page.request.session.appliction. 1.pa ...
- JavaScript中四种不同的属性检测方式比较
JavaScript中四种不同的属性检测方式比较 1. 用in方法 var o = {x:1}; "x" in o; //true "y" in o; //fa ...
随机推荐
- 中标麒麟系统安装rpm文件
打开终端,获得su权限. cd到rpm所在文件夹,输入指令,rpm -ivh rpm的名称
- P3379 【模板】最近公共祖先(LCA)(欧拉序+rmq)
P3379 [模板]最近公共祖先(LCA) 用欧拉序$+rmq$维护的$lca$可以做到$O(nlogn)$预处理,$O(1)$查询 从这里剻个图 #include<iostream> # ...
- ComboBox TextUpdate事件
winfrom ComboBox TextUpdate事件 首次输入词组(广州)会触发2次,最后text= "州",有人知道原因么?怎么解决! 大哥帮帮忙!输入法 换了 也一样,第 ...
- Simple Vedio Intercom System
I. Deployment / Architecture Block Diagram II. Resources Used sip proxy server + sip user agent 1. ...
- TensorFlow——MNIST手写数字识别
MNIST手写数字识别 MNIST数据集介绍和下载:http://yann.lecun.com/exdb/mnist/ 一.数据集介绍: MNIST是一个入门级的计算机视觉数据集 下载下来的数据集 ...
- How to compile and install Linux Kernel 5.1.2 from source code
How to compile and install Linux Kernel 5.1.2 from source code Compiling a custom kernel has its adv ...
- 【转】SIP协议 会话发起协议
转自:https://www.cnblogs.com/gardenofhu/p/7299963.html 会话发起协议(SIP)是VoIP技术中最常用的协议之一.它是一种应用层协议,与其他应用层协议协 ...
- Centos7 tomcat 启动权限
Cannot find bin/catalina.sh The file is absent or does not have execute permission This file is ne ...
- 2018-09-20-weekly
Algorithm 最长有效括号 What 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. How 这里可以用栈来求解,需要定义个start变量来记录合法括号串的起 ...
- AOP技术介绍--(引言)
软件设计因为引入面向对象思想而逐渐变得丰富起来.“一切皆为对象”的精义,使得程序世界所要处理的逻辑简化,开发者可以用一组对象以及这些对象之间的关系将软件系统形象地表示出来.而从对象的定义,进而到模块, ...