test1.jsp=======================html
<a href ="test.jsp?p=fuck">跳轉到test2</a>java
test2.jsp=======================web
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";app
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());webapp
%>jsp
顯示結果:spa
basePath:http://localhost:8080/test/ htm
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=fuckblog
在一些應用中,未登陸用戶請求了必須登陸的資源時,提示用戶登陸,此時要記住用戶訪問的當前頁面的URL,當他登陸成功後根據記住的URL跳回用戶最後訪問的頁面:資源
String lastAccessUrl = request.getRequestURL() + "?" + request.getQueryString();