JSP内置对象application
application对象类似于系统的“全局变量”,用于同一个应用内的所有用户之间的数据共享。
application对象的常用方法
方法名称 |
说 明 |
void setAttribute(String key, Object value) |
以key/value的形式保存对象值 |
Object getAttribute(String key) |
通过key获取对象值
扫描二维码关注公众号,回复:
1519149 查看本文章
|
String getRealPath(String path) |
返回相对路径的真实路径 |
例如:
//统计页 <% Integer count = (Integer) application.getAttribute("count"); if (count != null) { count = 1 + count; } else { count = 1; } application.setAttribute("count", count); %> 显示页 <% Integer i = (Integer) application.getAttribute("count"); out.println("您好,您是第 " + i + " 位访问本网站的用户"); %>
JSP常用内置对象
内置对象名称 |
说明 |
out |
用于向客户端输出数据 |
request |
主要用于处理客户端请求的数据信息 |
response |
用于响应客户端请求并向客户端输出信息 |
session |
用于记录会话状态的相关信息 |
application |
类似于系统的全局变量,用于实现Web应用中的资源共享 |
对象的作用域
page作用域
page作用域指单一JSP页面的范围,page作用域内的对象只能在创建该对象的页面中访问
page作用域在客户端每次请求JSP页面时创建,在服务器发送响应或请求转发到其他的页面或资源后失效。
pageContext.setAttribute(键,值)
例如:
//页面1 <% String name = "page"; pageContext.setAttribute("name",name); %> <strong> testOne:<%=pageContext.getAttribute("name") %> </strong> <br/> <% pageContext.include("testTwo.jsp"); %> //页面2 <strong> testTwo:<%=pageContext.getAttribute("name") %> </strong>
显示结果
testOne:page
testTwo:null
request作用域
request作用域内的对象则是与客户端的请求绑定在一起
request作用域对于每次客户端的请求都是不同的,对于任何一个新的请求,都会重新创建与本次请求相对应的request作用域。
例如:
页面1 <% String name = "request"; request.setAttribute("name",name); request.getRequestDispatcher("testTwo.jsp"). forward(request, response); %> 页面2 <strong> testTwo:<%=request.getAttribute("name") %> </strong>
显示结果
testTo:request
session作用域
JSP容器为每一次会话创建一个session作用域,在会话有效期内,只要将数据绑定到session作用域中,则该数据可以被本次会话中的其他页面所访问。
例如:
//页面1 <% String req = "request"; String ses = "session"; request.setAttribute("reqName ",req); session.setAttribute("sessionName ",ses); response.sendRedirect("testTwo.jsp"); %> //页面2 <strong> request:<%= request.getAttribute("reqName") %><br/> session:<%=session.getAttribute("sessionName") %> </strong>
显示结果
request:null
session:session
application作用域
相对于session作用域针对一个会话,application作用域则针对整个Web应用,被该应用下所有用户所共享。
例如:
//页面1 <% String app = "application"; String ses = "session"; session.setAttribute("sesName",ses); application.setAttribute("appName",app); response.sendRedirect("testTwo.jsp"); %> //页面2 <strong> session:<%=session.getAttribute("sesName") %><br/> application:<%=application.getAttribute("appName") %> </strong>
显示结果
session:session
application:application
对象的作用域
名称 |
说 明 |
page作用域 |
在一个页面范围内有效,通过pageContext对象访问 |
request作用域 |
在一个服务器请求范围内有效 |
session作用域 |
在一次会话范围内容有效 |
application作用域 |
在一个应用服务器范围内有效 |
cookie
cookie是Web服务器保存在客户端的一系列文本信息
cookie的作用
1、对特定对象的追踪
2、实现各种个性化服务
3、简化登录
安全性能:容易泄露信息
在JSP中使用cookie
cookie对象的常用方法
方法名称 |
说 明 |
void setMaxAge(int expiry) |
设置cookie的有效期,以秒为单位 |
void setValue(String value) |
在cookie创建后,对cookie进行赋值 |
String getName() |
获取cookie的名称 |
String getValue() |
获取cookie的值 |
int getMaxAge() |
获取cookie的有效时间,以秒为单位 |
语法:
1、创建cookie对象
Cookie newCookie = new Cookie(String key, String value);
2、写入cookie
response.addCookie(newCookie);
例如:
<% response.addCookie(new Cookie("username","Jack")); response.addCookie(new Cookie("password","123456")); response.sendRedirect("getCookies.jsp"); %>
3、读取cookie
Cookie[] cookies = request.getCookies();
cookie是通过name/value方式进行保存的,因而在遍历数组时,需要通过调用cookie对象的getName()方法对每个数组成员的名称进行检查,直至找到我们需要的cookie,然后调用cookie对象的getValue()方法取得与名称对应的值。
例如:
<% Cookie[] cookies = request.getCookies(); String user=""; String pwd = ""; if(cookies!=null){ for(int i=0;i<cookies.length;i++){ if(cookies[i].getName().equals("username")) user = cookies[i].getValue(); else if(cookies[i].getName().equals("password")) pwd = cookies[i].getValue(); } } out.print("用户名:"+user+" ,密码: "+pwd); %>
cookie的有效期
当用户访问网页时,cookie能够在客户端创建一个文件,用于记录用户的有关数据,但是一个cookie在客户端存在的事件并不是无限期的,也有其生命周期。
设置cookie有效期的方法是调用cookie对象的setMaxAge(int expiry)方法,其中参数expiry代表cookie的有效时间,以秒为单位。
例如:
//页面1 <% Cookie nc=new Cookie("info", "ok"); nc.setMaxAge(60); //设置Cookie失效前时间为60秒 response.addCookie(nc); response.sendRedirect("showCookie.jsp"); %> //页面2 <% Cookie[] cookies = request.getCookies(); boolean sign = false; //标识 if (cookies != null) { for (int i=0; i<cookies.length; i++) { if (cookies[i].getName().equals("info")) { sign = true; out.print("读取Cookie的值:" + cookies[i].getValue()); } } } if (!sign) { out.print("超过Cookie有效期,无法读取Cookie"); } %>
显示结果
读取cookie的值:ok
cookie与session的对比
session |
cookie |
在服务器端保存用户信息 |
在客户端保存用户信息 |
session中保存的是Object类型 |
cookie保存的是String类型 |
随会话的结束而将其存储的数据销毁 |
cookie可以长期保存在客户端 |
保存重要信息 |
保存不重要的信息 |
封装数据的JavaBean
从JavaBean的功能上可以分为
封装数据
封装业务
例如:
public class Comment { /* 将属性声明为私有属性 */ private String cid; // 用户名 private String cnid; // 邮编 private String ccontent; // 电话 /* 无参的共有构造方法 */ public Comment () { } /* 共有的设置属性值方法setXxx() 共有的获取属性值方法getXxx() */ public void setCid(String cid) { this. cid= cid; } public String getCid() { return cid; } }