一、四种属性范围

在JSP中提供了四种属性保存范围

page:在一个页面内保存属性,跳转之后无效
request:在一次服务请求范围内,服务器跳转后依然有效
session:-在一次会话范围内,无论何种跳转都可以使用,但是新开浏览器无法使用
application:在整个服务器上保存,所有用户都可以使用

名称

作用域

application

在所有应用程序中有效

session

在当前会话中有效

request

在当前请求中有效

page

在当前页面有效

  

  置统一的请求编码:request.setCharacterEncoding("GBK");

  1. 请求方式:<%=request.getMethod()%><br>
  2. 请求的资源:<%=request.getRequestURI()%><br>
  3. 请求用的协议:<%=request.getProtocol()%><br>
  4. 请求的文件名:<%=request.getServletPath()%><br>
  5. 请求的服务器的IP:<%=request.getServerName()%><br>
  6. 请求服务器的端口:<%=request.getServerPort()%><br>
  7. 客户端IP地址:<%=request.getRemoteAddr()%><br>
  8. 客户端主机名:<%=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)的更多相关文章

  1. JSP中四种属性保存范围(2)

    1.session <%@ page language="java" contentType="text/html" pageEncoding=" ...

  2. jsp中四种传递参数的方法

    jsp中四种传递参数的方法如下: 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="i ...

  3. css样式表中四种属性选择器

    学习此连接的总结http://developer.51cto.com/art/201009/226158.htmcss样式表中四种属性选择器1> 简易属性 tag[class]{ font-we ...

  4. (转)JSP中四种传递参数的方法:

    1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...

  5. JSP中四种传递参数中文乱码问题

    查看来源:http://blog.csdn.net/hackerain/article/details/6776083

  6. JSP九大内置对象和四种属性范围解读

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文首先主要解说了JSP中四种属性范围的概念.用法与实例. 然后在这个基础之上又引入了九 ...

  7. Jsp学习总结(1)——JSP九大内置对象和四种属性范围解读

    一.四种属性范围 1.1.在JSP中提供了四种属性保存范围 page:在一个页面内保存属性,跳转之后无效 request:在一次服务请求范围内,服务器跳转后依然有效 session:-在一次会话范围内 ...

  8. jsp四种属性范围

    在JSP提供了四种属性的保存范围.所谓的属性保存范围,指的就是一个设置的对象,可以在多个页面中保存并可以继续使用.它们分别是:page.request.session.appliction. 1.pa ...

  9. JavaScript中四种不同的属性检测方式比较

    JavaScript中四种不同的属性检测方式比较 1. 用in方法 var o = {x:1}; "x" in o; //true "y" in o; //fa ...

随机推荐

  1. 《jmeter:菜鸟入门到进阶系列》

    jmeter是我从事软件测试工作以来接触的第一个性能测试工具,也是耗费时间精力最多的一个工具,当然,学习jmeter过程中,由于知识储备不够,也顺带学习了很多其他相关的一些知识. 一直有个想法,就是把 ...

  2. 关于java的数组

    一定要写成 int[] arr = new int[30] 这样每个元素默认为0; 介样子的 如果写成 int[] arr = {1,2,3,4}; 那么他的长度就是4

  3. 【题解】Oh My Holy FFF

    题目大意   有\(n\)个士兵(\(1 \leq n \leq 10^5\)),第\(i\)个士兵的身高为\(h_{i}\),现在要求把士兵按照原来的顺序分成连续的若干组,要求每组的士兵数量不超过\ ...

  4. 在react中用装饰器来装饰connect

    假设我们在react中有如下header组件: import React, { PureComponent } from 'react'; import { connect } from 'react ...

  5. Vue组件通信方式(一)

    组件与组件的关系,通常有父子关系,兄弟关系以及隔代关系. 针对不同的场景,如何选用适合的通信方式呢? (一) props/$emit parentComponent ==> childCompo ...

  6. .NET平台 C# ASP.NET

    .NET 平台 根据微软的定义: .NET is a“ revolutionary new platform, built on open Internet protocols and standar ...

  7. Html5移动端页面自适应布局详解(rem布局)

    在移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport,通读网上的各种对于viewport的解释之后 大概viewport可以理解为三种 1.layout viewport  ...

  8. vue传值(小demo)

    vue+element ui实现的.解释大多在代码中(代码臭且长,有错误请指正)-- 代码如下: <template>  <div class="userList" ...

  9. 使用myBase Desktop来管理电脑上的资料

    下载链接:下载链接:http://www.wjjsoft.com/download.html 选择自己的操作系统下的myBase Desktop 这里是下载的是安装包,有解压的版本的. 这里就简单介绍 ...

  10. python 安装 pip ,并使用pip 安装 filetype

    闲话少说,直接上操作. python版本为2.7.6 可以直接到官网下载,我也提供一个百度云的下载地址 https://pan.baidu.com/s/1kWPXG8Z 这个是window版本,lin ...