日期:2014-05-16  浏览次数:20297 次

Jsp/Servlet复习笔记-----第4章 会话跟踪
4.1会话跟踪的需求
4.2 创建会话     
4.3 会话跟踪技术
4.3.1隐藏表单域   
4.3.2 URL 重写   
4.3.3 Cookie
4.4 Servlet 通信方法
   4.4.1 Forward 方法
   4.4.2 include 方法
4.5 Servlet 上下文
4.6 用MyEclipse开发会话跟踪程序
   4.6.1 显示客户访问计数
   4.6.2 购物车
4.7 本章小结   

隐藏域:<input type=”hidden” name=”” value=””>
将内容隐藏起来,使用户不能修改,之后随表单一起提交到服务器上显示
地址重写:
通过地址重写,可以将内容传递到服务器端——服务器端只认HTTP协议,而不管接受的内容是以何种形式传送过来的
如果要传递多个参数呢?
*.jsp?name=darkness&password=111

Java代码
1.<html>  
2.<body>  
3.    <form action="demo06.jsp" method="post">  
4.        <input type="hidden" name="uname" value="darkness">  
5.        <input type="submit" value="提交">  
6.    </form>  
7.    <a href="demo06.jsp?uname=darkness&upass=wind">demo06.jsp</a>  
8.</body>  
9.</html>  
10. 
11.<%@ page contentType="text/html;charset=gbk"%>  
12.<html>  
13.<body>  
14.<%  
15.    // 接收内容  
16.    request.setCharacterEncoding("GBK") ;  
17.    String name = request.getParameter("uname") ;  
18.    String pass = request.getParameter("upass") ;  
19.%>  
20.<h1>内容为:<%=name%></h1>  
21.<h1>内容为:<%=pass%></h1>  
22.</body>  
23.</html> 
<html>
<body>
<form action="demo06.jsp" method="post">
<input type="hidden" name="uname" value="darkness">
<input type="submit" value="提交">
</form>
<a href="demo06.jsp?uname=darkness&upass=wind">demo06.jsp</a>
</body>
</html>

<%@ page contentType="text/html;charset=gbk"%>
<html>
<body>
<%
// 接收内容
request.setCharacterEncoding("GBK") ;
String name = request.getParameter("uname") ;
String pass = request.getParameter("upass") ;
%>
<h1>内容为:<%=name%></h1>
<h1>内容为:<%=pass%></h1>
</body>
</html>


? 设置 Cookie(默认只对当前浏览器有效)
Cookie c1 = new Cookie(“name”,”value”);
c1.setMaxAge(60);//如果要Cookie长留在本机,设置cookie最大保存时间
response.addCookie(c1);
Cookie是通过服务器端设置到客户端上去的——response
如果要在服务器端取得cookie?request.getCookie()

Java代码
1.Cookie[] c = request .getCookies();  
2.for(i<c.length)?<%=c[i].getName()?c[i].getValue()%>  
3.<%  
4.    Cookie c1 = new Cookie("name","darkness") ;  
5.    Cookie c2 = new Cookie("password","sky") ;  
6. 
7.    // 保存时间为60秒  
8.    c1.setMaxAge(60) ;  
9.    c2.setMaxAge(60) ;  
10.%>  
11.<%  
12.    // 通过response对象将Cookie设置到客户端  
13.    response.addCookie(c1) ;  
14.    response.addCookie(c2) ;  
15.%>  
16.-----------------------------------  
17. 
18.<%  
19.    // 通过request对象,取得客户端设置的全部Cookie