HttpServletResponse encodeURL 作用

If your application uses session objects, you must ensure that session tracking is enabled by having the application rewrite URLs whenever the client turns off cookies. You do this by calling the response’s encodeURL(URL) method on all URLs returned by a servlet. This method includes the session ID in the URL only if cookies are disabled; otherwise, it returns the URL unchanged.

The doGet method of ShowCartServlet encodes the three URLs at the bottom of the shopping cart display page as follows:

 1 out.println("<p> &nbsp; <p><strong><a href=\"" +
2 response.encodeURL(request.getContextPath() +
3 "/bookcatalog") +
4 "\">" + messages.getString("ContinueShopping") +
5 "</a> &nbsp; &nbsp; &nbsp;" +
6 "<a href=\"" +
7 response.encodeURL(request.getContextPath() +
8 "/bookcashier") +
9 "\">" + messages.getString("Checkout") +
10 "</a> &nbsp; &nbsp; &nbsp;" +
11 "<a href=\"" +
12 response.encodeURL(request.getContextPath() +
13 "/bookshowcart?Clear=clear") +
14 "\">" + messages.getString("ClearCart") +
15 "</a></strong>");

 

If cookies are turned off, the session is encoded in the Check Out URL as follows:

http://localhost:8080/bookstore1/cashier;jsessionid=c0o7fszeb1

If cookies are turned on, the URL is simply

http://localhost:8080/bookstore1/cashier
 
在客户端禁止cookie的情况下,将session信息追加到url后,保持session同步。

转载于:https://www.cnblogs.com/lostyue/archive/2012/03/15/2398783.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值