URL太长时会被截断

时间:2012-03-01 05:29:27

标签: java jsp url java-ee

在JSP中,我传递了URL:

http://gmail.com/fs-bin/click?id=VFS63JhSk2k&offerid=99133.10000176&subid=0&u1=1945&type=4

到服务器。但是在服务器端,当从请求获取URL req.getParameter("url")时,我只得到:

http://gmail.com/fs-bin/click?id=VFS63JhSk2k

如何获取整个网址?

3 个答案:

答案 0 :(得分:0)

您是否看到getParameter首先分裂&?这是因为像http://myserver.com/test.jsp?param1=val1&url=http://gmail.com/fs-bin/click?id=VFS63JhSk2k&offerid=99133.10000176&subid=0&u1=1945&type=4&paramlast=valLast这样的网址将被假定为带有参数的网址

param1=val1
url=http://gmail.com/fs-bin/click?id=VFS63JhSk2k
offerid=99133.10000176
subid=0
u1=1945
type=4
paramlast=valLast

在将此URL作为参数传递之前,您需要在JSP中转义&。可能的方法之一是

 URLEncoder.encode("http://gmail.com/fs-bin/click?id=VFS63JhSk2k&offerid=99133.10000176&subid=0&u1=1945&type=4", "UTF-8")

答案 1 :(得分:0)

您需要在使用之前对网址进行编码。替换&和其他特殊的URL字符在作为参数传递出去之前

//在将其作为参数

传递之前
    String url = "http://a.com?a=1&b=2&c=3";
    String encodedUrl = URLEncoder.encode(url,"UTF-8"); 
    System.out.println(" http://anotherdomain.com/page.jsp?url=" + encodedUrl);

//在JSP上

    String decodedUrl = URLDecoder.decode(req.getParameter("url"),"UTF-8"); 
    System.out.println("Dncoded URL " + decodedUrl);

答案 2 :(得分:-1)

URL url = new URL(request.getRequestURL().toString());会为您提供整个网址。

您还可以获取网址的特定部分。检查java docs for java.net.URL。

相关问题