JSP用户会话跟踪

先介绍下会话跟踪的概念吧!
我们知道,HTTP是一种无状态的协议,也就是说,客户端在浏览服务器上的不同的页面时,每次请求获得响应完成后,服务器和客户端的Socket连接会关闭。但是在不同页面之间,我们往往需要交换信息。交换信息的方式如下:
1.HTTP信息
它将需要交换的信息保存在HTTP头部。随着代理服务器的出现和保密性的问题,这种技术就过时了。
2.隐藏字段
通过HTML的HIDDEN标记来传递信息:
[quote]
<input type="hidden" name="somename"value="somevalue">
[/quote]
这种方式存在保密性问题,我们可以通过浏览器菜单中的【view】查看—>【Source】命令来查看浏览器中的源文件
3.URL重写
把身份认证的信息加到页面链接的尾部,如:
[quote]
<a href="/user.jsp">Next</a>
改为
<a href="user.jsp?name=hellking&item=item&quantify=3">Next</a>
[/quote]
其实这样方式也存在安全的问题,同时,这个链接的长度受URL长度的限制(一般浏览器URL长度最大为2KB)
4.Cookie
浏览器把一些小的片断信息保存在客户端,在以后的使用过程中,服务器端可以使用客户端的Cookie。Cookie虽然是一个有效的解决方式,但同样存在一些问题:客户端可以选择不接受Cookie,如果这样,有些程序很可能就不能正常运行。
5.Session
Session就是会话。简单地讲,会话可以在单一的用户和服务器之间定义一些列的交互,这些交互可以保持一段时间。一旦服务器上建立一个会话,一个SessionID就和这个会话关联。

实例:一个简单的购物程序

//Item 类
public class Item{
//属性
private String itemId;//目录中Items的Id
private double price;// Item的价格
private String description;//Items的描述
private boolean available;//是否有Item
private String producter;//Items的生产商

//构造方法
public Item(String itemId,double price,String description,
boolean available,String producter){

this.itemId=itemId;
this.price=price;
this.description=description;
this.available;
this.producter=producter;
}

//属性getter和setter方法

public String getItemId(){
return itemId;
}

public void setItemId(String itemId){
this.itemId=itemId;
}

public double getPrice(){
return price;
}
publi void setPrice(double price){
this.price=price;
}

public String getDescription(){
return description;
}
public void setDescription(String description){
this.description=description;
}

public boolean getAvailable(
return available;
)
public void setAvailable(boolean available){
this.available=available;
}

public String getProducter(){
return producter;
}
public void setProducter(String producter){
this.producter=producter;
}
}


Products类。它表示当前可以获得的Item,在通常情况下从数据库中获得

import java.util.Vector;
public class Products{
private Vector items=new Vector();//表示Item的目录

synchronized public Vector getItems(){
return items;
}
synchronized public Item getItem(String itemId){
int index=Integer.parseInt(itemId);
return (Item)items.elementAt(index);
}
synchronized public void setItem(Item item,String itemId){
int index=Integer.parseInt(itemId);
items.set(index,item);
}
public Products(){
//添加一些资料
items.addElement(new Item("0","JSP技术",(double)45.90,true,"电子工业出版社"))
.........................
}
//Item数量
public int getSize(){
return items.size();
}
}

Cart类,表示购物车,它使用HashMap保存用户添加的物品。

public class Cart{
private String userId;//用户ID
private HashMap items;// 购物车中的物品

public Cart(){
items=new HashMap();
}

//新增一个Item
public void addItem(String itemId,int quantity){
items.put(itemId,Integer(quantity));
}
//删除一种Item
public void removeItem(String itemId){
items.remove(itemId);
}
//更新某个Item
public void updateItem(String itemId,int quantity){
if(items.containsKey(itemId))
items.remove(itemId);
items.put(itemId,new Integer(quantity));
}
//返回购物车中的所有Item
public HashMap getItems(){
return this.items
}

public void setUserId(String userId){
this.userId=userId;
}
public String getUserId(){
return this.userId;
}

//清空购物车
public void clear(){
items.clear();
}
}

以上就是JavaBean组件
现在要结合JSP页面了。
先看看checklogin.jsp
[quote]
<jsp:useBean id="cart" class="com.Cart" scope="session">
<jsp:setProperty name="cart" property="*"/>
</jsp:useBean>
<% session.setMaxInactiveInterval(900); //设置Session超时为30分钟%>
<%
String nextpage;
if(cart.getUserId().equals("jack"))nextpage="shopping.jsp";
else nextpage="login.jsp"
%>
<jsp:forward page="<%=nextpage%>"/>
[/quote]

接下来就是登陆成功进入的shopping.jsp代码
[quote]
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="com.*"%>
<jsp:useBean id="products" clss="com.Products" scope="session"/>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="test/html;charset=utf8">
</head>
<Link href="x.css" type=text/css rel=stylesheet>
<body>
<%@ include file="header.jsp"%>
<center>
<form action="cart.jsp" method=get>
<table width="75%"border="1" bordercolor="#006633">
<tr bgcolor="#999999">
<td>id</td>
<td>名称</td>
<td>价格</td>
<td>是否有库存</td>
<td>出版社</td>
</tr>
<%
java.util.Vector=products.getItems();
java.util.Enumeration e=v.elements();
while(e.hasMoreElements())
{
Item item=(Item)e.nextElement();
%>
<tr>
<td><input type="checkbox" name="itemId"> value="<%=item.getItemId()%>"</td>
<td><%=items.getDescription()%></td>
<td><%=items.getPrice()%></td>
<td><%=items.getAvailable%></td>
<td><%=item.getProducer()%></td>
</tr>
<%}%>
<tr align=left>
<td colspan=5><input type=submit value="add" name="action"></td></tr>
<td colspan=5><a href="cart.jsp">购物车</a>[]<a href="logout.jsp">注销</a></td></tr>
</table>
</form>
</center>
<%@include file="tail.jsp"%>
</body>
</html>
[/quote]
上面的代码实现个Add功能。现在看看cart.jsp文件吧
[quote]
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="com.*,java.util.*"%>
<jsp:useBean id="products" clss="com.Products" scope="session"/>
<jsp:useBean id="cart" clss="com.Cart" scope="session"/>
</jsp:useBean>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="test/html;charset=utf8">
</head>
<Link href="x.css" type=text/css rel=stylesheet>
<%
//action为执行操作,add表示添加此Item,remove表示删除此Item
String action=request.getParameter("action");
//获得所有添加、删除的Item的ItemId
String items[]=request.getParamterValues("itemId");
if(items!=null)
for(int i=0;i<items.length;i++){
if(action.equals("add"))
cart.addItem(items[i],1)
else if(action.equals("remove"))cart.removeItem(items[i])
}
%>
<body>
<%@include file="header.jsp"%>
<center>
<form action="cart.jsp" method=get>
<table width="75%" border="1" bordercolor="#006633">
<tr bgcolor="#999999">
<td>id</td>
<td>名称</td>
<td>数量</td>
</tr>
<%
java.util.HsahMap cart_item=cart.getItems();
Iterator it=cart_item.keySet().iterator();
while(it.hasNext()){
String itemId=(String)it.next();
Item item=products.getItem(itemId);
%>
<tr>
<td><input type="checkbox" name="itemId" value="<%=item.getItemId()%>"></td>
<td><%=item.getDescription()%></td>
<td><%=car_item.get(itemId)%></td>
</tr>
<%}%>
<tr align=left>
<td colspan=5><input type=submit value="remove" name="action"></td>
</tr>
<tr align=left>
<td colspan=5><a href="shopping.jsp">购物</a>[]<a href="logout.jsp">注销</a></td>
</tr>
</table>
</center>
<%@ include file="tail.jsp"%>
</body>
</html>
[/quote]
最后看下注销logout.jsp的相关源码
[quote]
<jsp:useBean id="cart" class="com.Cart" scope="session"/>
<%
cart.clear();
session.invalidate();
response.sendRedirect("login.jsp");
%>
[/quote]
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值