`

response的 encodeUrl方法的使用[转]

阅读更多

转自 :http://edu.codepub.com/2009/1213/18697.php

 

public java.lang.String encodeURL(java.lang.String url)Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary.
For robust session tracking, all URLs emitted by a servlet should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.

Parameters:
url - the url to be encoded.
Returns:
the encoded URL if encoding is needed; the unchanged URL otherwise.

通常,会话管理是通过服务器将 Session ID 作为一个 cookie 存储在用户的 Web 浏览器中来唯一标识每个用户会话。如果浏览器不支持 cookies,或者将浏览器设置为不接受 cookies,我们可以通过 URL 重写来实现会话管理。

  实质上 URL 重写是通过向 URL 连接添加参数,并把 session ID 作为值包含在连接中。然而,为使这生效,你需要为你的 servlet 响应部分的每个连接添加 session ID 。

   把 session ID 加到一个连接可以使用一对方法来简化:response.encodeURL() 使 URL 包含 session ID,如果你需要使用重定向,可以使用 response.encodeRedirectURL () 来对 URL 进行编码。

  encodeURL () 及 encodeRedirectedURL () 方法首先判断 cookies 是否被浏览器支持;如果支持,则参数 URL 被原样返回,session ID 将通过 cookies 来维持。

  来看下面的例子,两个 JSP 文件:hello1.jsp 和 hello2.jsp,及它们之间的影响。我们在 hello1.jsp 中简单的创建一个会话,并在 session 中存储一个对象实例。接着用户可以点击页面的连接到达 hello2.jsp。在 hello2.jsp 中,我们从 session 中获取原先放置的对象并显示它的内容。注意,我们在 hello1.jsp 中调用了 encodeURL() 方法来获得 hello2.jsp 的链接,使得在浏览器停用 cookies 的情况下,session ID 自动添加到 URL,hello2.jsp 仍能得到 session 对象。

  首先在启用 cookies 的情况下运行。然后关闭对 cookie 的支持,重启浏览器,再运行一次。每次你都可以看到会话管理在起作用,并能在页之间传递信息。

注意,如果你想让这个例子能在关闭了 cookies 的浏览器中工作,你的 JSP 引擎必须支持 URL 重写。

hello1.jsp

<%@ page session="true" %>
<%
Integer num = new Integer(100);
session.putValue("num",num);
String url =response.encodeURL("hello2.jsp");
%>
<a href='<%=url%>'>hello2.jsp</a>

hello2.jsp

<%@ page session="true" %>
<%
Integer i= (Integer)session.getValue("num");
out.println("Num value in session is "+i.intValue());
%>

使用时,若你的浏览器支持cookie的话,要设置为不支持cookie,才会有效果,你将看到链接后的页面地址,追加了sessionID的值。

分享到:
评论

相关推荐

    用JSP做的网上交友页面

    &lt;a href="&lt;%=response.encodeURL("showMember.jsp")%&gt;"&gt;浏览会员| &lt;a href="&lt;%=response.encodeURL("register.jsp")%&gt;"&gt;会员注册| &lt;a href="&lt;%=response.encodeURL("login.jsp")%&gt;"&gt;会员登录| &lt;a href="&lt;%=...

    字符串转EnCode_c#

    字符串转EnCode_c# 字符串转EnCode_c# 字符串转EnCode_c# 字符串转EnCode_c# 字符串转EnCode_c# 字符串转EnCode_c# 字符串转EnCode_c# 字符串转EnCode_c#

    javascript中encodeURI和decodeURI方法使用介绍

    一、基本概念 encodeURI和decodeURI是成对来使用的,因为浏览器的地址栏有中文字符的话,可以会出现不可预期的错误,所以可以encodeURI把非英文字符转化为英文编码,decodeURI可以用来把字符还原回来。encodeURI方法...

    EncodeDecode经典加密解密+VB源码.rar

    EncodeDecode经典加密解密+VB源码.rarEncodeDecode经典加密解密+VB源码.rarEncodeDecode经典加密解密+VB源码.rarEncodeDecode经典加密解密+VB源码.rar

    java-servlet-api.doc

    这份文档以及刚才提及的Javadoc格式的文档都描述了这两个软件包,Javadoc格式的文档还描述了你应该如何使用这两个软件包中的所有方法。 有关规范 你也许对下面的这些Internet规范感兴趣,这些规范将直接影响到...

    jsp java 购物车

    &lt;%String str=response.encodeURL("buybook.jsp");%&gt; &lt;FORM action="&lt;%=str%&gt;" Method="post" &gt; &lt;P&gt;&lt;BR&gt;输入要订购的书的序列号: 提交"&gt; 查询到如下记录: (); %&gt; 如果准备订购该书,请填写订单,点击"添加...

    java中Cookie被禁用后Session追踪问题

    一.服务器端获取Session对象依赖于客户端携带的Cookie中的JSESSIONID数据。...在 Session1Servlet中,使用response.encodeURL(url) 对超链接路径拼接 session的唯一标识 // 当点击 的时候跳转到 sess

    ASP中只有UrlEncode,没有Urldecode问题的解决方法?

    在ASP中传递参数时有一个很有用的系统函数Server.UrlEncode,可以将一些非字母数字的特殊符号转换成标准URL编码(其实就是16进制ASC码),这样就解决了参数传递问题,然后我以为也提供了Server.UrlDecode,但使用后...

    在asp中使用js的encodeURIComponent方法

    encodeURIComponent 方法返回一个已编码的 URI。如果您将编码结果传递给 decodeURIComponent,那么将返回初始的字符串

    servlet学习笔记

    String newURL=response.encodeURL(oldURL); 目的:保证session在cookie禁用时仍然有效 http://localhost:8080/XServlet;jsessionid=fdfsdddde45234435235 GET session id 不再通过请求头中传送...

    servlet2.4doc

    The default behavior of this method is to call encodeUrl(String url) on the wrapped response object. encodeUrl(String) - Method in interface javax.servlet.http.HttpServletResponse Deprecated. As of ...

    Excel公式与函数大辞典.宋翔(带书签高清文字版).pdf

    本书从函数功能、函数格式、参数说明、注意事项、Excel 版本提醒、案例应用、交叉参考7 个方面,全面、细致地介绍了Excel 2016/2013/2010/2007/2003 中公式和函数的使用方法、实际应用和操作技巧。最后3 章还将公式...

    SimpleServlet.zip

    out.println(response.encodeURL("cart")); out.println("\"method=\"POST\"&gt;"); out.println("&lt;table cellspacing=\"5\" cellpadding=\"5\"&gt;&lt;tr&gt;"); out.println("&lt;td align=\"center\"&gt;&lt;b&gt;种类&lt;/b&gt;&lt;/td&gt;")...

    ios获取数据之encodeURI和decodeURI的实例

    下面小编就为大家带来一篇ios获取数据之encodeURI和decodeURI的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    PHP rawurlencode与urlencode函数的深入分析

    结论:rawurlencode遵守是94年国际标准备忘录RFC 1738,urlencode实现的是传统做法,和上者的主要区别是对空格的转义是’+’而不是’ ′javascript的encodeURL也是94年标准, 而javascript的escape是另一种用”%xxx”...

    portal开发portlet的一些知识

    图片路径的写法 &lt;IMG &gt;/testportlet/images/mark.gif"&gt; &lt;IMG src="%=renderResponse.encodeURL(renderRequest.getContextPath()+"&gt;"&gt; 其中renderResponse.encodeURL表示编码。目录结构截图: 。。。 。。。

    php二维码扩展库exqrcode.zip

    使用方法:   $instance = new ExQrcode(); //ExQrcode::QRLEVEL_L //ExQrcode::QRLEVEL_M //ExQrcode::QRLEVEL_Q //ExQrcode::QRLEVEL_H $instance-&gt;setLevel(ExQrcode::QRLEVEL_H); $instance-&gt;setSize(7); //$...

    JS对字符串编码的几种方式使用指南

    在客户端中,要对字符串编码,可以采用:escape();encodeurl();encodeURIComponent();下面我们就来具体讲解下这几个方法。

    UrlRewriting Filter-开源

    UrlRewriting Filter 是一个 servlet 过滤器,它自动将 request.encodeURL() 应用于 servlet 响应正文中的 url。 这允许用户不必担心手动将 request.encodeURL() 应用于这些 url 以进行会话传播。

    Excelize文档类库-其他

    CONCATENATE, COUNT, COUNTBLANK, CUMIPMT, CUMPRINC, DATE, DATEDIF, DB, DDB, DEC2BIN, DEC2HEX, DEC2OCT, DOLLARDE, DOLLARFR, EFFECT, ENCODEURL, EXACT, FALSE, FIND, FINDB, FISHER, FISHERINV, FIXED, FV, ...

Global site tag (gtag.js) - Google Analytics