JSP中常见的几个路径区别

当前jsp访问地址为:http://localhost:8080/app/test/contextPath.jsp 

其中/app

String path = request.getContextPath();

http://localhost:8080/app/

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
/test/contextPath.jsp   

String servletPath = request.getServletPath();
/app/test/contextPath.jsp

如果在js中,要加载本jsp页面同级的jsp文件,你就可能需要这个    

String jspPath = path +  servletPath.substring(0, servletPath.lastIndexOf("/"));
/app/test/contextPath.jsp

request.getRequestURI()
http://localhost:8080/app/test/contextPath.jsp           
request.getRequestURL()
http:/localhost/123/jsp/test.jsp:
 
   1:  <%@ page language="java" pageEncoding="UTF-8"%>
   2:   
   3:  <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
   4:  <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
   5:  <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
   6:  <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
   7:   
   8:   
   9:  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10:  <html:html lang="true">
  11:    <head>
  12:      <html:base />
  13:     
  14:      <title>test.jsp</title>
  15:   
  16:      <meta http-equiv="pragma" content="no-cache">
  17:      <meta http-equiv="cache-control" content="no-cache">
  18:      <meta http-equiv="expires" content="0">   
  19:      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  20:      <meta http-equiv="description" content="This is my page">
  21:      <!--
  22:      <link rel="stylesheet" type="text/css" href="styles.css">
  23:      -->
  24:   
  25:    </head>
  26:   
  27:    <body>
  28:  <p>
  29:      <h2>application - javax.servlet.ServletContext</h2>
  30:      application.getRealPath("/")<%=application.getRealPath("/") %><br>
  31:      application.getMajorVersion() - <%=application.getMajorVersion() %><br>
  32:      application.getMimeType("txt") - <%=application.getMimeType("txt") %><br>
  33:      application.getServerInfo() - <%=application.getServerInfo() %><br>
  34:      application.getServletContextName() - <%=application.getServletContextName() %><br>
  35:      application.getContext()
  36:  </p>
  37:   
  38:  <p>
  39:      <h2>config - javax.servlet.ServletConfig</h2>
  40:      config.getServletName() - <%=config.getServletName() %><br>
  41:      config.getServletContext()<br>
  42:  </p>
  43:   
  44:  <p>
  45:      <h2>pageContext - javax.servlet.jsp.PageContext</h2>
  46:      pageContext.getRequest()(JSP中的requset隐式对象)<br>
  47:      pageContext.getResponse()(JSP中的response隐式对象)<br>
  48:      pageContext.getServletConfig()(JSP中的config隐式对象)<br>
  49:      pageContext.getServletContext()(JSP中的application隐式对象)<br>
  50:      pageContext.getException()(JSP中的exception隐式对象)<br>
  51:      pageContext.getSession()(JSP中的session隐式对象)<br>
  52:      pageContext.getOut()(JSP中的out隐式对象)
  53:  </p>
  54:   
  55:  <p>
  56:      <h2>request - javax.servlet.http.HttpServletRequest</h2>
  57:      request.getLocalAddr() - <%=request.getLocalAddr() %><br>
  58:      request.getServletPath() - <%=request.getServletPath() %><br>
  59:      request.getContextPath() - <%=request.getContextPath() %><br>
  60:      request.getLocalName() - <%=request.getLocalName() %><br>
  61:      request.getLocalPort() - <%=request.getLocalPort() %><br>
  62:      request.getPathInfo() - <%=request.getPathInfo() %><br>
  63:      request.getProtocol() - <%=request.getProtocol() %><br>
  64:      request.getQueryString() - <%=request.getQueryString() %><br>
  65:      request.getRemoteAddr() - <%=request.getRemoteAddr() %><br>
  66:      request.getRemoteHost() - <%=request.getRemoteHost() %><br>
  67:      request.getRemotePort() - <%=request.getRemotePort() %><br>
  68:      request.getRemoteUser() - <%=request.getRemoteUser() %><br>
  69:      request.getRequestedSessionId():<%=request.getRequestedSessionId() %><br>
  70:      request.getRequestURI() - <%=request.getRequestURI() %><br>
  71:      request.getRequestURL() - <%=request.getRequestURL() %>
  72:  </p>
  73:   
  74:  <p>
  75:      <h2>session - javax.servlet.http.HttpSession</h2>
  76:      session.getServletContext()
  77:  </p>
  78:   
  79:    </body>
  80:  </html:html>

客户端显示如下:

application – javax.servlet.ServletContext

application.getRealPath("/")F:\workspace\tomcat\123\

application.getMajorVersion() – 2

application.getMimeType("txt") – null

application.getServerInfo() – Apache Tomcat/5.0.30

application.getServletContextName() – null

application.getContext()

config – javax.servlet.ServletConfig

config.getServletName() – jsp

config.getServletContext()

pageContext – javax.servlet.jsp.PageContext

pageContext.getRequest()

pageContext.getResponse()

pageContext.getServletConfig()

pageContext.getServletContext()

pageContext.getException()

pageContext.getSession()

pageContext.getOut()

request – javax.servlet.http.HttpServletRequest

request.getLocalAddr() – 127.0.0.1

request.getServletPath() – /jsp/test.jsp

request.getContextPath() – /123

request.getLocalName() – 127.0.0.1

request.getLocalPort() – 80

request.getPathInfo() – null

request.getProtocol() – HTTP/1.1

request.getQueryString() – null

request.getRemoteAddr() – 127.0.0.1

request.getRemoteHost() – 127.0.0.1

request.getRemotePort() – 1211

request.getRemoteUser() – null

request.getRequestedSessionId():5AFAE9C0A164621D4F8E0DAF2F253C92

request.getRequestURI() – /123/jsp/test.jsp

request.getRequestURL() – http://localhost/123/jsp/test.jsp

session – javax.servlet.http.HttpSession

session.getServletContext()

注:

http://localhost/123/taglib/html/result.jsp中使用以下方法

request.getServletPath() :返回request的请求路径的相对于应用程序的相对路径

request.getContextPath()返回应用程序上下文相对路径

this.getRealPath(String str)返回UIL中str对应于本地磁盘的绝对路径

request.getLocalAddr() 返回服务器IP地址

                              

你可能感兴趣的:(JSP中常见的几个路径区别)