JSP学习--常用作用域
page:当前页面,也就是只要跳到别的页面就失效了
request:一次会话,简单的理解就是一次请求范围内有效
session:浏览器进程,只要当前页面没有被关闭(没有被程序强制清除),不管怎么跳转都是有效的
request:一次会话,简单的理解就是一次请求范围内有效
session:浏览器进程,只要当前页面没有被关闭(没有被程序强制清除),不管怎么跳转都是有效的
application:服务器,只要服务器没有重启(没有被程序强制清除),数据就有效
PageContext作用域:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/lose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Hello</title> </head> <body> <% pageContext.setAttribute("name", "fuckyou"); pageContext.setAttribute("fuckyou", 101010); %> <% String name = (String)pageContext.getAttribute("name"); int fuckyou = (Integer)pageContext.getAttribute("fuckyou"); %> <font> name:<%=name %> fuckyou:<%=fuckyou %> </font> <br> <font> name:<%=name %> fuckyou:<%=fuckyou %> </font> </body> </html>
Request作用域:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% // 设置两个request范围的数据 key-> value request.setAttribute("fuckyou","this fuckyou is coming from here"); request.setAttribute("fuckfuckyou",01010); %> <jsp:forward page="requestTarget.jsp"></jsp:forward> </body> </html>
//接收:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% <span style="white-space:pre"> </span>// 取值 <span style="white-space:pre"> </span>String fuck=(String)request.getAttribute("fuckyou"); <span style="white-space:pre"> </span>int you=(Integer)request.getAttribute("fuckfuckyou"); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>// 获取头信息 <span style="white-space:pre"> </span>Enumeration enu=request.getHeaderNames(); <span style="white-space:pre"> </span>while(enu.hasMoreElements()){ <span style="white-space:pre"> </span>String headerName=(String)enu.nextElement(); <span style="white-space:pre"> </span>String headerValue=request.getHeader(headerName); %> <span style="white-space:pre"> </span><h4><%=headerName %> <%=headerValue %></h4> <% <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> %> <font>姓名:<%=fuck %></font> <font>年龄:<%=you %></font> </body> </html>
session作用域:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% // 设置两个session范围的数据 key-> value session.setAttribute("fuck","you"); session.setAttribute("fuckyou",01010); %> <h1>session作用域!</h1> </body> </html>
//接收
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% <span style="white-space:pre"> </span>// 取值 <span style="white-space:pre"> </span>String fuck=(String)session.getAttribute("fuck"); <span style="white-space:pre"> </span>int fuckyou=(Integer)session.getAttribute("fuckyou"); %> <font>hello<%=fuck %></font> <font>world<%=fuckyou %></font> </body> </html>
application作用域:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% // 设置两个application范围的数据 key-> value application.setAttribute("name","fuck"); application.setAttribute("fuckyou",01010); %> <h1>application作用域</h1> </body> </html>
//接收
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% <span style="white-space:pre"> </span>// 取值 <span style="white-space:pre"> </span>String fuck=(String)application.getAttribute("name"); <span style="white-space:pre"> </span>int you=(Integer)application.getAttribute("fuckyou"); %> <font>fuck<%=fuck %></font> <font>you<%=you %></font> </body> </html>