首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

解析Servlet/JSP对话跟踪机制

2013-01-26 
解析Servlet/JSP会话跟踪机制在上面这个 jsp:useBean标记中,class属性值是“包.类名字”形式。当然,对于不

解析Servlet/JSP会话跟踪机制

在上面这个< jsp:useBean>标记中,class属性值是“包.类名字”形式。当然,对于不同的包名字,class属性的值应该作相应的修改。注意Bean的scope属性是“application”,这是因为我们要在应用的所有页面中使用这个Bean。在这个应用中,把Bean的scope属性设置为“application”具有最好的效率,因为我们只需创建Bean对象一次就可以了。另外,正如前面所提到的,getSessionID方法必须在所有其他代码之前调用。

< % String sessionId = PseudoSessionId.getSessionID(request);%>

为了说明PseudoSessionBean的应用,下面我们来看两个JSP页面,它们是index.jsp和secondPage.jsp。index.jsp页面在伪会话变量中保存用户的名字,而secondPage.jsp则提取这个用户名字。

index.jsp页面的代码如下:

< %@ page session="false" contentType="text/html;charset=gb2312" %>< jsp:useBean id="PseudoSessionId" scope="application"/>< % String sessionId = PseudoSessionId.getSessionID(request);%>< html>< head>< title>伪会话< /title>< /head>< body>< h1>伪会话管理机制< /h1>< % String userName = "bulbul"; PseudoSessionId.setValue(sessionId, "userName", userName);%>< a href="/secondPage.jsp?sessionId=< ";%=sessionId%>>点击此处< /a>< form method="post" action=anotherPage.jsp?sessionId=< %=sessionId%>>输入数据:< input type="text" name="sample">< input type="submit" name="Submit" value="Submit">< /form>< /body>< /html>< % PseudoSessionId.deleteAllInvalidSessions();%>?

注意,包括< form>标记的action属性在内,所有的超级链接都已经改写,现在都包含了会话标识符。另外也请注意页面的最后调用了deleteAllInvalidSessions方法。

secondPage.jsp页面只简单地返回以前保存的用户名字。

< %@ contentType="text/html;charset=gb2312" page session="false" %>< jsp:useBean id="PseudoSessionId" scope="application"/>< % String sessionId = PseudoSessionId.getSessionID(request);%>< html>< head>< title>第2个页面< /title>< /head>< body>< % String userName = PseudoSessionId.getValue(sessionId,"userName");out.println("用户名字是 " + userName);%>< /body>< /html>?

热点排行