java java中的url编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10276473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
url encoding in java?
提问by sujit
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 :
我想知道什么是url encoding。我有 2 个 jsp 页面和一个 servlet。当我运行应用程序时,显示的 url 是:
http://localhost:8080/myproject/index.jsp
http://localhost:8080/myproject/index.jsp
where
在哪里
index.jsp :
索引.jsp:
<form action="Myservlet" method="post">
<input type="text" name="mytext" id="mytext"/>
<input type="submit" value="submit"/>
</form>
after the submit button is clicked the URL displayed is :
单击提交按钮后,显示的 URL 为:
http://localhost:8080/myproject/Myservlet
http://localhost:8080/myproject/Myservlet
What is the meaning of URL encoding? How can I encode url?
URL编码的含义是什么?我如何编码网址?
From index.jsp
goes to Myservlet
then to result.jsp
从index.jsp
去到Myservlet
然后到result.jsp
Myservet#doPost// Do I need to do URL encoding here? If yes how ?
Myservet#doPost// 这里需要做 URL 编码吗?如果是怎么办?
fetching data from db.......
....................
String nextJSP = "/result.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
result.jsp
结果.jsp
displays data here
displays data here
回答by blackcompe
There are two types of encoding: HTML form encodingand URL re-writing.
In form encoding, the URL string is converted into a valid ASCII format that's Internet-ready. From the URLEncoder.encode(String, String)docs:
在表单编码中,URL 字符串被转换为可在 Internet 上使用的有效 ASCII 格式。来自URLEncoder.encode(String, String)文档:
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.
使用特定的编码方案将字符串转换为 application/x-www-form-urlencoded 格式。此方法使用提供的编码方案来获取不安全字符的字节。
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:
第二种是 URL 重写。URL 字符串使用会话 ID 进行编码,以防客户端浏览器不支持(或已禁用)cookie 或会话跟踪。来自HttpServletResponse.encodeURL(String)文档:
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.
通过在其中包含会话 ID 对指定的 URL 进行编码,或者,如果不需要编码,则返回未更改的 URL。该方法的实现包括判断会话ID是否需要在URL中编码的逻辑。例如,如果浏览器支持 cookie,或者关闭了会话跟踪,则不需要 URL 编码。
回答by Hardik Mishra
I think you have misundersting here. Neither HTML Form Encoding
nor URL Re-writing
is for what you want to achieve.
我想你在这里有误解。既不是HTML Form Encoding
也不URL Re-writing
是为了你想要达到的目标。
If you want to achieve like .
如果你想实现像 .
For example: instead of typing http://localhost:8080/search.jsp?xxxthe user would see http:/localhost:8080/search?xxx
例如:用户将看到 http://localhost:8080/search?xxx而不是键入http://localhost:8080/search.jsp ?xxx
You can create a servlet mapping like this:
您可以像这样创建一个 servlet 映射:
<servlet-mapping>
<servlet-name>MappingServlet</servlet-name>
<url-pattern>path/*</url-pattern>
</servlet-mapping>
The url-pattern must be edited to suit your needs. You need of course to create the servlet in order to map the url to the actual jsp. This technique is used by most of the MVC frameworks.
必须编辑 url-pattern 以满足您的需要。您当然需要创建 servlet 以便将 url 映射到实际的 jsp。大多数 MVC 框架都使用这种技术。
Read More on How to develop JSP/Servlets Web App using MVC pattern?
回答by Jose Manuel Gomez Alvarez
Use java.net.URLEncoder.encode(s, "UTF-8")
where s
is the string to encode.
使用java.net.URLEncoder.encode(s, "UTF-8")
wheres
是要编码的字符串。
This is required whenever we send text as path segments, query string arguments, etc...
每当我们将文本作为路径段、查询字符串参数等发送时,这是必需的...
Example: see the documentation
示例:查看文档