getRequestURI,getRequestURL的区别

转自:http://www.cnblogs.com/JemBai/archive/2010/11/10/1873764.html

test1.jsp=======================

<a href ="test2.jsp?p=fuck">跳转到test2</a>

test2.jsp=======================

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

out.println("basePath:"+basePath);
out.println("<br/>");
out.println("getContextPath:"+request.getContextPath());
out.println("<br/>");
out.println("getServletPath:"+request.getServletPath());
out.println("<br/>");
out.println("getRequestURI:"+request.getRequestURI());
out.println("<br/>");
out.println("getRequestURL:"+request.getRequestURL());
out.println("<br/>");
out.println("getRealPath:"+request.getRealPath("/"));
out.println("<br/>");
out.println("getServletContext().getRealPath:"+getServletContext().getRealPath("/"));
out.println("<br/>");
out.println("getQueryString:"+request.getQueryString());

%>

 

显示结果:

basePath:http://localhost:8080/test/

getContextPath:/test 
getServletPath:/test.jsp 
getRequestURI:/test/test.jsp 
getRequestURL:http://localhost:8080/test/test.jsp 
getRealPath:D:\Tomcat 6.0\webapps\test\ 
getServletContext().getRealPath:D:\Tomcat 6.0\webapps\test\ 
getQueryString:p=fuck

在一些应用中,未登录用户请求了必须登录的资源时,提示用户登录,此时要记住用户访问的当前页面的URL,当他登录成功后根据记住的URL跳回用户最后访问的页面:

String lastAccessUrl = request.getRequestURL() + "?" + request.getQueryString();

 

这里有个问题:

如果请求路径改为

<a href ="test2.jsp;jsessionid=94C863C680E36898A21A2613D5A98C8E?p=fuck">跳转到test2</a>

则目前已知的在jdk1.8里的请求结果会带上jsessionid

getContextPath:/YSJH2 
getServletPath:/test2.jsp 
getRequestURI:/YSJH2/test2.jsp;jsessionid=94C863C680E36898A21A2613D5A98C8E 
getRequestURL:http://127.0.0.1:8080/YSJH2/test2.jsp;jsessionid=94C863C680E36898A21A2613D5A98C8E 
getRealPath:C:\Program Files (x86)\Apache Software Foundation\apache-tomcat-8.0.26\webapps\YSJH2\ 
getServletContext().getRealPath:C:\Program Files (x86)\Apache Software Foundation\apache-tomcat-8.0.26\webapps\YSJH2\ 
getQueryString:p=fuck

  

你可能感兴趣的:(getRequestURI,getRequestURL的区别)