2017-05-04 117 views
0

的URL在HTML:HttpServletResponse的使URL正斜杠(/)变成两个正斜杠(//)

<a href=""////jrdc.xxx.com/dh/nc?camp=19&mid=19&mat=121&unit=-&uuid=386931bea19dbba0e8f8c3291743d004a71669b5807d3eb49e150e08fcd93c83&aid=12&day=1493864666856&to=https://sale.xxx.com/act/UuzWBLwPKX.html" target="_blank"> 

的contronller:

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
public void clickLog(HttpServletRequest request, HttpServletResponse response) { 
    try { 
     // 
     String targetUrl = request.getParameter("to");   
     if(targetUrl != null && !targetUrl.contains("http")){ 
      targetUrl = "http://" + targetUrl; 
     } 
     response.sendRedirect(targetUrl); 
    }catch (Exception e){ 

    }finally { 

    } 

} 

的targetUrl这个以response.sendRedirect是()是:

https://sale.jd.com/act/UuzWBLwPKX.html

的问题是,当重定向: 的Chrome浏览器的URL变为:

https://sale.xxx.com//act//UuzWBLwPKX.html 

其“行为”与“/”前成为“//”,我不希望这样的结果,为什么变成这个问题以及如何成为https://sale.xxx.com/act/UuzWBLwPKX.html

回答

-1

使用URI Class来解决这一问题

URI uri = new URI(targetUrl).normalize(); 
System.out.println("Target URL ----> "+uri.toString()); 

结果:

Target URL ----> https://sale.xxx.com/act/UuzWBLwPKX.html 
+0

感谢关注 – Fanl

0

我自己解决了这个问题;遗憾的离开了信息,该项目具有过滤处理保卫XSS攻击

public class FHttpServletRequest extends HttpServletRequestWrapper{ 

    public FHttpServletRequest(HttpServletRequest request) { 
      super(request); 
     } 
    @Override 
    public String getParameter(String name) { 
     return escapeXss(super.getParameter(escapeXss(name))); 
    } 
    protected String escapeXss(String param) { 
     if (StringUtils.isNotBlank(param)) { 
      return 
    StringEscapeUtils.escapeHtml(StringEscapeUtils.escapeJavaScript(param)); 
    } 
    return param; 
} 
.. 
} 

因此字符串“为= https://sale.xxx.com/act/UuzWBLwPKX.html”的要求, 在Java中:“https://sale.xxx.com/ ACT/UuzWBLwPKX.html” 这样的解决方案是:

String targetUrl = request.getParameter("to"); 
targetUrl = StringEscapeUtils.unescapeJava(targetUrl); 
if(targetUrl != null && !targetUrl.contains("http")){ 
.... 
} 
+0

错误我还是却不知道为什么变成了‘//行为’ – Fanl