获得项目的绝对地址 getRequestURI,getRequestURL的区别
java获得tomcat项目的绝对地址
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
basePath : http://localhost:8080/test/
getRequestURI,getRequestURL的区别
test1.jsp=======================
<a href ="test.jsp?p=fuck">跳转到test2</a>
test2.jsp=======================
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% 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();
作 者:imsoft
Email:imsofter#163.com
出处:http://www.cnblogs.com/imsoft/
本文版权归作者和博客园共有,欢迎转载、交流,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。如果觉得本文对您有益,欢迎点赞、欢迎探讨。本博客来源于互联网的资源,若侵犯到您的权利,请联系博主予以删除。