jsp获取到用户请求的链接

JSP页面中相关代码:

<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
String  url  =  "http://"  +  request.getServerName()  +  ":"  +  request.getServerPort()  +  request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1);  

if(request.getQueryString()!=null) 
{   
    url+="?"+request.getQueryString();           
} 
System.out.println("path:"+path); 
System.out.println("basePath:"+basePath);    
System.out.println("URL:"+url);    
System.out.println("URL参数:"+request.getQueryString());  
%>

访问之后输出的结果:
1.这是不带参数

path:/CS
basePath:http://localhost:8080/CS/
URL:http://localhost:8080/CS/
URL参数:null

2.带参数时

path:/CS
basePath:http://localhost:8080/CS/
URL:http://localhost:8080/CS/?userid=3
URL参数:userid=3

为了在servlet中,让异常时跳到带有参数的URL,找了很久才找到。原来这么简单。把url存到session里,异常的时候再取出来加上去就好啦。

 String url = session.getAttribute("url");
    out.println("<script>alert('出错啦。');window.location.href='"+url+"';</script>");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值