您的位置:首页 > Web前端 > JavaScript

[转]JSP中与路径相关的常用的几个方法

2010-10-01 10:21 363 查看
虽然很简单,但是很全,有时候很久没用好了还真是记不住,所以转载过来了。

http:/localhost/123/jsp/test.jsp:

<%@pagelanguage="java"pageEncoding="UTF-8"%>

[code]
<%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%>

<%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>

<%@tagliburi="http://struts.apache.org/tags-logic"prefix="logic"%>

<%@tagliburi="http://struts.apache.org/tags-tiles"prefix="tiles"%>



<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html:htmllang="true">

<head>

<html:base/>


<title>test.jsp</title>


<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="Thisismypage">

<!--

<linkrel="stylesheet"type="text/css"href="styles.css">

-->


</head>


<body>

<p>

<h2>application-javax.servlet.ServletContext</h2>

application.getRealPath("/")<%=application.getRealPath("/")%><br>

application.getMajorVersion()-<%=application.getMajorVersion()%><br>

application.getMimeType("txt")-<%=application.getMimeType("txt")%><br>

application.getServerInfo()-<%=application.getServerInfo()%><br>

application.getServletContextName()-<%=application.getServletContextName()%><br>

application.getContext()

</p>


<p>

<h2>config-javax.servlet.ServletConfig</h2>

config.getServletName()-<%=config.getServletName()%><br>

config.getServletContext()<br>

</p>


<p>

<h2>pageContext-javax.servlet.jsp.PageContext</h2>

pageContext.getRequest()(JSP中的requset隐式对象)<br>

pageContext.getResponse()(JSP中的response隐式对象)<br>

pageContext.getServletConfig()(JSP中的config隐式对象)<br>

pageContext.getServletContext()(JSP中的application隐式对象)<br>

pageContext.getException()(JSP中的exception隐式对象)<br>

pageContext.getSession()(JSP中的session隐式对象)<br>

pageContext.getOut()(JSP中的out隐式对象)

</p>


<p>

<h2>request-javax.servlet.http.HttpServletRequest</h2>

request.getLocalAddr()-<%=request.getLocalAddr()%><br>

request.getServletPath()-<%=request.getServletPath()%><br>

request.getContextPath()-<%=request.getContextPath()%><br>

request.getLocalName()-<%=request.getLocalName()%><br>

request.getLocalPort()-<%=request.getLocalPort()%><br>

request.getPathInfo()-<%=request.getPathInfo()%><br>

request.getProtocol()-<%=request.getProtocol()%><br>

request.getQueryString()-<%=request.getQueryString()%><br>

request.getRemoteAddr()-<%=request.getRemoteAddr()%><br>

request.getRemoteHost()-<%=request.getRemoteHost()%><br>

request.getRemotePort()-<%=request.getRemotePort()%><br>

request.getRemoteUser()-<%=request.getRemoteUser()%><br>

request.getRequestedSessionId():<%=request.getRequestedSessionId()%><br>

request.getRequestURI()-<%=request.getRequestURI()%><br>

request.getRequestURL()-<%=request.getRequestURL()%>

</p>


<p>

<h2>session-javax.servlet.http.HttpSession</h2>

session.getServletContext()

</p>


</body>

</html:html>


客户端显示如下:
application-javax.servlet.ServletContext
application.getRealPath("/")F:\workspace\tomcat\123\
application.getMajorVersion()-2
application.getMimeType("txt")-null
application.getServerInfo()-ApacheTomcat/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(Stringstr)返回UIL中str对应于本地磁盘的绝对路径
request.getLocalAddr()返回服务器IP地址

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: