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

前端 未结 4 1590
你的背包
你的背包 2021-01-24 08:43

I am trying to use the encodeURL method in a jsp to encode a URL with \"%\" symbol.

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

4条回答
  •  有刺的猬
    2021-01-24 08:58

    The HttpServletResponse#encodeURL() method has actually a misleading name. Read the javadoc to learn what it really does (appending the jsessionid if necessary). See also In the context of Java Servlet what is the difference between URL Rewriting and Forwarding? to learn about ambiguties in JSP/Servlet world.

    In the servlet side, you need URLEncoder#encode() instead:

    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 tag instead (avoid Java code in JSP!):

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

提交回复
热议问题