JSP URI/URL - How to get the request URI, URL, and Context from a JSP

JSP URI/URL FAQ: How do I get a URI or URL from a JSP (the request URI or request URL)?

I was just working with a JSP, and trying to remember how to get information that can be very helpful inside of a JSP, specifically how to determine the Context, URI, and URL from within a JSP.

To that end, here‘s the Java source code for a JSP I wrote that will display the Context, URI, and URL for this JSP when it is accessed from a browser:

<h2>JSP URI, URL, Context</h2>

Request Context Path: <%= request.getContextPath() %>
Request URI:          <%= request.getRequestURI() %>
Request URL:          <%= request.getRequestURL() %>

  

When I save this in a file named uriTest.jsp, place it in a context named test that is already running on a Tomcat server on my local computer, and then hit the URL for this JSP (http://localhost:8080/test/uriTest.jsp), I see the following output:

JSP URI, URL, Context

Request Context Path: /test
Request URI:          /test/uriTest.jsp
Request URL:          http://localhost:8080/test/uriTest.jsp

Information like this can be very helpful when doing a variety of things inside a JSP. For instance, in looking at the blog here at devdaily.com/blog, I want to add some social bookmark links here, and when doing that, I can use the =request.getRequestURI() or request.getRequestURL()= methods to get the URL information that I need to send to Digg, StumbleUpon, and others.

While I mentioned that I ran this test from within Tomcat, you‘ll use these same Java/JSP methods from within any servlet container, including GlassfishJBoss, WebLogic, or WebSphere.

时间: 2024-10-03 13:27:18

JSP URI/URL - How to get the request URI, URL, and Context from a JSP的相关文章

jsp Request获取url信息的各种方法比较

从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+&q

request,response,URL编码,路径专题

一.Response 1.Resonse的继承结构: ServletResponse--HttpServletResponse 2.Response代表响应,于是响应消息中的 状态码.响应头.实体内容都可以由它进行操作,由此引伸出如下实验: 3.利用Response输出数据到客户端 response.getOutputStream().write("中文".getBytes())输出数据,这是一个字节流,是什么字节输出什么字节,而浏览器默认用平台字节码打开服务器发送的数据,如 果服务器

从request获取各种路径总结 request.getRealPath(&quot;url&quot;)

转载:http://blog.csdn.net/piaoxuan1987/article/details/8541839 equest.getRealPath() 这个方法已经不推荐使用了,代替方法是: request.getSession().getServletContext().getRealPath() 从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 S

java获取request的url方法区别

1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数.2.request.getRequestURI() 得到的是request URL的部分值,并且web容器没有decode过的 3.request.getContextPath() 返回 the context of the request. 4.request.getServletPath() 返回调用servlet的部分url. 5.reque

request 获取url

1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数.2.request.getRequestURI() 得到的是request URL的部分值,并且web容器没有decode过的 3.request.getContextPath() 返回 the context of the request. 4.request.getServletPath() 返回调用servlet的部分url. 5.reque

Request.ServerVariables[&quot;Url&quot;]

Request.ServerVariables["Url"] 返回服务器地址 Request.ServerVariables["Path_Info"] 客户端提供的路径信息 Request.ServerVariables["Appl_Physical_Path"] 与应用程序元数据库路径相应的物理路径 Request.ServerVariables["Path_Translated"] 通过由虚拟至物理的映射后得到的路径 Re

Unable to create request (bad url?) 解决方案

今天写一个测试demo,遇到如下问题: Error Domain=ASIHTTPRequestErrorDomain Code=5 "Unable to create request (bad url?)" UserInfo=0x69ba0f0 {NSLocalizedDescription=Unable to create request (bad url?)} 功能是为了测试分包下载,大概流程:请求URL,服务器返回一套URLList,自己分别去请求每个url最后把得到得数据进行拼

IIS问题解决:URL中制表符引起的Bad Request - Invalid URL

昨天处理好了Google网站管理员中的500错误,今天处理了一些400处理,比如下面的以制表符(tab)结尾的URL: http://www.cnblogs.com/me-sa/archive/2008/05/16/1200329.html%09 http://www.cnblogs.com/JimmyZhang/archive/2007/12/20/1006555.html%09 ... 访问这些URL时,IIS会返回400 bad request的错误: Bad Request - Inva

Request获取url各种信息的方法

1.Request获取url各种信息的方法 测试的url地址:http://www.test.com/testweb/default.aspx, 结果如下: Request.ApplicationPath: /testweb Request.CurrentExecutionFilePath: /testweb/default.aspx Request.FilePath: /testweb/default.aspx Request.Path: /testweb/default.aspx Reque