简体   繁体   English

如何使用特殊字符“百分比”对URL进行编码?

[英]How to encode a URL with the special character “percentage”?

I am trying to use the encodeURL method in a jsp to encode a URL with "%" symbol. 我试图在jsp中使用encodeURL方法来编码带有“%”符号的URL。

response.encodeURL(/page1/page2/view.jsp?name=Population of 91% in this place)

Whenever the button is clicked, "The website cannot display the page" error is shown. 只要单击该按钮, "The website cannot display the page"错误。

But when you manually change the "%" symbol to "%25" like this " Population of 91%25 in this place ",then the correct page is displayed. 但是,当您将"%"符号手动更改为"%25"例如“ Population of 91%25 in this place ”时,将显示正确的页面。

Also whenever the "%" symbol is placed at last like this " In this place Population of 91% ", then the page is correctly displayed but i noticed that in the address bar its still shown as "%" and not as "%25" and still its working. 同样,每当这样最后放置"%"符号时,例如“ In this place Population of 91% ”时,页面就会正确显示,但是我注意到在地址栏中它仍显示为"%"而不是"%25"而且还在努力。

When I searched around, its mentioned only to use the other methods like encodeURI() & encodeURIComponent(). 当我四处搜索时,它仅提及使用其他方法,例如encodeURI() & encodeURIComponent().

Can you suggest me a solution while still using the encodeURL method to display the pages correctly even if there is a "%" symbol. 您能否为我提出一个解决方案,同时仍然使用encodeURL方法正确显示页面,即使有"%"符号也是如此。 Should i use replace() or why isnt the encodeURL() method working correctly? 我应该使用replace()还是为什么encodeURL()方法不能正常工作?

考虑使用URLEncoderURLDecoder

The HttpServletResponse#encodeURL() method has actually a misleading name. HttpServletResponse#encodeURL()方法实际上有一个误导性的名称。 Read the javadoc to learn what it really does (appending the jsessionid if necessary). 阅读javadoc以了解它的真正作用(必要时附加jsessionid )。 See also In the context of Java Servlet what is the difference between URL Rewriting and Forwarding? 另请参阅在Java Servlet的上下文中,URL重写和转发有什么区别? to learn about ambiguties in JSP/Servlet world. 了解JSP / Servlet世界中的歧义。

In the servlet side, you need URLEncoder#encode() instead: 在Servlet端,您需要URLEncoder#encode()代替:

String url = "/page1/page2/view.jsp?name=" + URLEncoder.encode("Population of 91% in this place", "UTF-8");
// ...

In the JSP side, however, you need the JSTL <c:url> tag instead ( avoid Java code in JSP! ): 但是,在JSP端,您需要JSTL <c:url>标记( 避免在JSP中使用Java代码! ):

<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...

<c:url var="url" value="/page1/page2/view.jsp">
  <c:param name="name" value="Population of 91% in this place" />
</c:url>

<a href="${url}">link</a>

<form action="${url}">
  <input type="submit" value="button" />
</form>

The result of your code is : 您的代码的结果是:

%2Fpage1%2Fpage2%2Fview.jsp%3Fname%3DPopulation%20of%2091%25%20in%20this%20place

You should encode only the query string value. 您应该仅对查询字符串值进行编码。

... = "/page1/page2/view.jsp?name=" + URLEncoder.encode('Population of 91% in this place');

?

In your example, you can use the c:url and c:param tags: 在您的示例中,您可以使用c:urlc:param标签:

<c:url value="/page1/page2/view.jsp">
    <c:param name="name" value="Population of 91% in this place" />
</c:url>

In particular, the c:param tag will url-encode the value attribute. 特别是, c:param标记将对value属性进行url编码。 I just ran into a situation where I needed to generate a URL with a querystring containing a value that started with a pound sign. 我刚遇到一种情况,我需要生成一个带有查询字符串的URL,该查询字符串包含以井号开头的值。 Without url-encoding, the pound sign was interpreted by the browser as the start of the anchor portion. 如果没有url编码,浏览器会将井号解释为锚点部分的开始。 I added the c:param tag, and the pound sign was encoded, allowing expected behavior when following the link. 我添加了c:param标记,并且对井号进行了编码,从而可以在链接时实现预期的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM