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>");

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐