码迷,mamicode.com
首页 > Web开发 > 详细

getRequestURI,getRequestURL的区别

时间:2018-01-31 11:23:07      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:access   text   页面   执行   应用   post   浏览器   erp   serve   

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

 

假定你的web application 名称为news,你在浏览器中输入请求路径:

http://localhost:8080/news/main/list.jsp

则执行下面向行代码后打印出如下结果:

1、 System.out.println(request.getContextPath());

打印结果:/news

2、System.out.println(request.getServletPath());

 打印结果:/main/list.JSP

 3、 System.out.println(request.getRequestURI());

 打印结果:/news/main/list.JSp

 4、 System.out.println(request.getRealPath("/"));

 打印结果:F:\tomcat 6.0\webapps\news\test

 

getRequestURI,getRequestURL的区别

标签:access   text   页面   执行   应用   post   浏览器   erp   serve   

原文地址:https://www.cnblogs.com/xiarongjin/p/8390701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!