Java中的URL编码? [英] url encoding in java?
问题描述
我想知道什么是 url编码. 我有2个jsp页面和1个servlet. 当我运行该应用程序时,显示的网址是:
I want to know that what is url encoding. I have 2 jsp pages and one servlet. When I run the application the url displayed is :
http://localhost:8080/myproject/index.jsp
其中
index.jsp:
<form action="Myservlet" method="post">
<input type="text" name="mytext" id="mytext"/>
<input type="submit" value="submit"/>
</form>
点击提交按钮后,显示的URL为:
after the submit button is clicked the URL displayed is :
http://localhost:8080/myproject/Myservlet
URL编码是什么意思?如何编码网址?
从index.jsp
转到Myservlet
,然后到result.jsp
Myservet#doPost //我需要在此处进行URL编码吗?如果是,怎么办?
Myservet#doPost // Do I need to do URL encoding here? If yes how ?
fetching data from db.......
....................
String nextJSP = "/result.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
result.jsp
displays data here
推荐答案
采用表单编码,URL字符串被转换为可用于Internet的有效ASCII格式.来自 URLEncoder.encode(String,String)文档:
In form encoding, the URL string is converted into a valid ASCII format that's Internet-ready. From the URLEncoder.encode(String, String) docs:
将字符串转换为application/x-www-form-urlencoded格式 使用特定的编码方案.此方法使用提供的 编码方案以获取不安全字符的字节.
Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.
第二种是URL重写.如果客户端浏览器不支持(或禁用)cookie或会话跟踪,则URL字符串将使用会话ID进行编码.来自 HttpServletResponse .encodeURL(String)文档:
The second kind is URL rewriting. The URL string is encoded with a session id in case the client browser doesn't support (or has disabled) cookies or session tracking. From the HttpServletResponse.encodeURL(String) docs:
通过在其中包含会话ID来编码指定的URL,或者 不需要编码,返回不变的URL.实施 此方法的逻辑包括确定会话ID是否为逻辑 需要在URL中进行编码.例如,如果浏览器支持 Cookie,或会话跟踪已关闭,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.
这篇关于Java中的URL编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!