使用url改写
在某些情形下,浏览器也许不接受cookies,这样就不能用cookies来进行会话跟踪。url改写就是这种情形下的一个解决办法,当weblogic服务器检测到浏览器不接受cookies时,就会自动替换url。url改写就会将编码的会话id写进servlet返回给浏览器的web页面的超级链接里。当用户随后点击这些链接时,weblogic服务器从url地址中取出id,并在servlet调用getsession()方法时找到合适的httpsession。
要在weblogic服务器中使用url改写,在weblogic特有部署描述符weblogic.xml中的<session-descriptor>下,将属性urlrewritingenabled设为true(这个属性的默认值是true)。
url改写的编码原则
要支持url改写,这里有一些代码应当如何处理urls的一般原则。
· 避免在输出流中直接写url,如:
out.println("<a href=\"/myshop/catalog.jsp\">catalog</a>");
而是使用httpservletresponse.encodeurl()方法,如:
out.println("<a href=\""
+ response.encodeurl("myshop/catalog.jsp")
+ "\">catalog</a>");
调用encodeurl()方法决定url是否需要改写,如要就用包含会话id的url改写。会话id被添加到url中,并以分号开头。
· 除了weblogic服务器响应返回的urls,也要编码重定向的urls。如
if (session.isnew())
response.sendredirect (response.encoderedirecturl(welcomeurl));
weblogic服务器在一个新会话开始时就会使用url改写,即使浏览器不接受cookies,因为服务器不能断定浏览器在会话的第一次访问时是否接受cookies。
· 通过检查httpservletrequest.isrequestedsessionidfromcookie()方法返回的布尔值,servlet能确定是否从cookie按收到给定的会话id。也许应用程序适当响应,或者简单地依赖weblogic服务器的url改写。
url改写和无线访问协议(wap)
如编写wap应用程序,由于wap协议不支持cookies,就必须使用url改写。另外,一些wap设备的url(包括参数)长度有128个字符的限制,这样就限制了用url改写传输的数据数量。要允许参数使用更多空间,用idlength属性指定字节数,可以限制weblogic服务器随机产生的会话id的长度。
使用字符集和post数据
you can set the character set that is used when processing data from a form that uses the post method. to inform the application that processes the form data that a particular character set is in use, you add specific "signal" characters to the url used to process the form data (specified with the action attribute of the <form> tag) and then map those characters to an encoding in the web application deployment descriptor, web.xml. post data is normally read as ascii unless specified using the following procedure.
在处理从用post方法提交的表单数据时可设定使用的字符集。要通知处理表单数据的应用程序在使用的特定字符集,给处理表单数据(在<form>标签的action属性里指定)的url添加指定的”信号”字符,然后将那些字符映射到一个web应用程序部署描述符web.xml的编码上。post数据数据正常以ascii编码读取,除非用以下过程指定编码。
要用非ascii字符集处理post数据:
1. 在web应用程度部署描述符web.xml中用<context-param>建立一个条目。这个条目应在web.xml 文件里的<distributable>元素后,<servlet>元素前。在这个条目中,<param-name>总是包含weblogic.httpd.inputcharset类名,后跟一句点,再后就是信号字符串。<param-value>包含http字符集的名称。在下例中,字符串/rus/jo*被映射到windows-1251字符集:
<context-param>
<param-name>weblogic.httpd.inputcharset./rus/jo*</param-name>
<param-value>windows-1251</param-value>
</context-param>
2. 在传递表单数据时使用信号字符串编码html表单。如:
<form action="http://some.host.com/mywebapp/rus/jo/index.html">
…
</form>
将信号字符串放在web应用程序名称(这种情况下也叫context路径-mywebapp-)后面和url剩余部分的前面。
有关更多的web应用程序描述符的信息,参考定义context参数。
