标签:test ica row tom windows header xtend method 相对
假定你的工程名称为projects,你在浏览器中输入请求路径:
http://127.0.0.1:8080/projects/pages/newForm.jsp
则执行下面向行代码后打印出如下结果:
1、 System.out.println(request.getContextPath());
打印结果:/projects
2、System.out.println(request.getServletPath());
打印结果:/pages/newForm.jsp
3、 System.out.println(request.getRequestURI());
打印结果:/projects/pages/newForm.jsp
4、 System.out.println(request.getRealPath("/"));
JSP servlet API提供了getRealPath(path)方法,返回给定虚拟路径的真实路径,如果转换错误,则返回null。
打印结果:C:\Tomcat5.0\webapps\projects\test
假定你的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("/"));
request.getRealPath("/")已经不建议使用。
改成req.getServletContext().getRealPath("/")
getRealPath();
返回一个字符串,包含一个给定虚拟路径的真实路径。
struts2中:
ServletContext ctx=ServletActionContext.getServletContext();
String path=ctx.getRealPath("/");
String path1=ctx.getRealPath("/files/view.jsp");
输出:path为D:\Javasoftware\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\struts2-1\
path1为D:\Javasoftware\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\struts2-1\files\view.jsp
\files\view.jsp这部分就是虚拟路径
D:\Javasoftware\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\struts2-1\ 为项目的绝对路径
servlet中:
private ServletConfig config;
public void init(ServletConfig config) throws ServletException {
this.config=config;}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext ctx=config.getServletContext();
String temp=ctx.getRealPath("/");
}
打印结果:F:\Tomcat 6.0\webapps\news\test
注:
URI=contextPath+servletPath
现在项目做到加权限了,如何防止用户通过直接输入URL访问网页,去查资料,了解到在filter里用HttpServletRequest里的方法得到该请求的父URL-fatherURL,判断该fatherURL是否为空,如果为空就表示该请求是通过直接输入URL访问的。
代码:
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- //首先判断如果是在URL输入页面访问,直接非法
- HttpServletRequest req=(HttpServletRequest)request;
- HttpServletResponse resp=(HttpServletResponse)response;
- String fatherUrl = fatherUrl = req.getHeader("REFERER");//取得上一级页面的RUL
-
- if("".equals(fatherUrl ) || null == fatherUrl ) {
- String url = req.getServletPath();
- //第一次转发
- if("//pages/login.jsp".equals(url )) {
- chain.doFilter(request, response);
- } else { //设置输出页面编码
- resp.setContentType("text/html;charset=gb2312");
- resp.getWriter().write("错误页面 ,无此页");
- return ;
- }
- } else{ //如果不是直接URL输入访问
- if(null==req.getSession().getAttribute("inWebUser")) {
- resp.getWriter().write("非法访问");
- return;
- } else {
- chain.doFilter(request, response);
- }
- }
- }
知道这个方法后,感觉HttpServletRequest太好用了,就去了JavaEE API文档,看到HttpServletRequest里有几个得到URL的方法,既然有好几个,说明它们之间肯定有不同之处,针对各种实际情况运用。下面就说说它们的区别:
getServletPath:得到相对项目的url,不带项目名称 如://sys/menu/gomain.htm
- String path = ((HttpServletRequest)req).getServletPath();
getRequestURI:得到相对服务器的url,和上面有不同之处,getRequestURI带有项目root,以项目名称为开头,如:/cms//sys/menu/gomain.htm
- String requestURI = ((HttpServletRequest)req).getServletURI();
getContextPath:得到部署的项目名(Context-root),不是URL,仅仅项目名字 如:/cms
- String ContextPath = ((HttpServletRequest)req).getContextPath();
getRequestURL:得到绝对地址url如:http://localhost:8080/cms//sys/menu/gomain.htm
- StringBuffer RequestURL = ((HttpServletRequest)req).getRequestURL();
getRealPath:得到应用程序部署在服务器上的绝对路径。如:D:\apache-tomcat-5.5.23\webapps\cms\
- String realPath = this.fc.getServletContext().getRealPath("/");
System.out.println("ServletPath:"+path+"\n"+"ContextPath:"+ContextPath+"\n"+"requestURI:"+requestURI+"\n"+"RequestURL:"+RequestURL+"\n"+"realPath:"+realPath);
结果如下:
ServletPath:/sys/menu/gomain.htm
ContextPath:/cms
requestURI:/cms//sys/menu/gomain.htm
RequestURL:http://localhost:8080/cms//sys/menu/gomain.htm
realPath:D:\apache-tomcat-5.5.23\webapps\cms\
速记:
- getServletPath与getRequestURI相似,区别在于有无项目名称
- getRequestURL是带有HTTP协议的URL,
- getContextPath得到项目的名称
- getRealPath得到在服务器上的绝对路径
Servlet的Request中和用户请求路径相关的操作有多个,下面一一列举:
1.getQueryString(),&之后GET方法的参数部分。
2.getServletPath(),web.xml中定义的Servlet访问路径。
3.getPathInfo(),Servlet访问路径之后,QueryString之前的中间部分。
4.getContextPath(),Context路径前缀(这个应该是最熟悉的啦)。
5.getRequestURI(),等于getContextPath() + getServletPath() + getPathInfo()。
6.getRequestURL(),等于getScheme() + "://" + getServerName() + ":" + getServerPort() + getRequestURI()。
7.getPathTranslated(),等于getServletContext().getRealPath("/") + getPathInfo()。
注意:
1.getServletContext().getRealPath("/") 后包含当前系统的文件夹分隔符(windows系统是"\",linux系统是"/"),而getPathInfo()以"/"开头。
- windows 下面
- E:\apache-tomcat-6.0.20\webapps\standardweb\
2.getPathInfo()与getPathTranslated()在servlet的url-pattern被设置为/*或/aa/*之类的pattern时才有值,其他时候都返回null。
3.在servlet的url-pattern被设置为*.xx之类的pattern时,getServletPath()返回的是getRequestURI()去掉前面ContextPath的剩余部分。
本篇文章来源于 Goldtimes.net 原文链接:http://www.goldtimes.net/teach/view/1124.htm
- request对象中的方法是比较实用的,大家应该对它们都有所了解。下面我们通过一个TestServlet来讲解一下常用的方法。TestServlet源代码如下:
- package coresun;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class TestServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
- response.setContentType("text/html;charset=gb2312");
- PrintWriter out = response.getWriter();
- out.println("<html><head><title>使用request中的方法</title></head><body><h2>");
-
- out.println("request.getRequestURL()= "+request.getRequestURL()+"<br>");
- out.println("request.getRequestURI()= "+request.getRequestURI()+"<br>");
- out.println("request.getContextPath()= "+request.getContextPath()+"<br>");
- out.println("request.getServletPath()= "+request.getServletPath()+"<br>");
- out.println("request.getQueryString()= "+request.getQueryString()+"<br>");
- out.println("request.getPathInfo()= "+request.getPathInfo()+"<br>");
- out.println("request.getPathTranslated()= "+request.getPathTranslated());
- out.println("<br>");
- out.println("request.getProtocol()= "+request.getProtocol()+"<br>");
- out.println("request.getMethod()= "+request.getMethod()+"<br>");
- out.println("request.getScheme()= "+request.getScheme()+"<br>");
- out.println("request.getRequestedSessionId()= "+request.getRequestedSessionId()+"<br>");
- out.println("request.isRequestedSessionIdFromCookie()= "+request.isRequestedSessionIdFromCookie()+"<br>");
- out.println("request.isRequestedSessionIdFromURL()= "+request.isRequestedSessionIdFromURL()+"<br>");
- out.println("request.isRequestedSessionIdValid()= "+request.isRequestedSessionIdValid()+"<br>");
-
- out.println("</h2></body></html>");
- }
- }
-
- 此Servlet在web.xml文件中的部署如下:(只有关键代码)
- <servlet>
- <servlet-name>test</servlet-name>
- <servlet-class>coresun.TestServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>test</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- *.do表示只要是以.do结尾的地址,都可以访问此Servlet。然后在地址栏中输入如下网址:(假设服务器为本地,端口8080,Web根目录为FilterWeb)
- http:
- 页面返回的结果如下:
- request.getRequestURL() = http:
- request.getRequestURI() = /FilterWeb/update.do
- request.getContextPath() = /FilterWeb
- request.getServletPath() = /update.do
- request.getQueryString() = userName=zhangsan&age=20
- request.getPathInfo() = null
- request.getPathTranslated() = null
- request.getProtocol() = HTTP/1.1
- request.getMethod() = GET
- request.getScheme() = http
- request.getRequestedSessionId() = 0D5219B7FF11D47EBE95B2E6A31076B5
- request.isRequestedSessionIdFromCookie() = true
- request.isRequestedSessionIdFromURL() = false
- request.isRequestedSessionIdValid() = true
- request.getAuthType() = null
- 这些方法的含义大家通过它的名字应该能够揣摩出来的。
-
- 下面更改servlet的url-pattern地址,如下:
- <servlet>
- <servlet-name>test</servlet-name>
- <servlet-class>coresun.TestServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>test</servlet-name>
- <url-pattern>/faces/*</url-pattern>
- </servlet-mapping>
- 以上表示只要是以/faces/开头的地址,此Servlet都可以访问
- 浏览器的地址栏中填入如下地址:
- http:
- 页面返回的结果如下:
- request.getRequestURL() = http:
- request.getRequestURI() = /FilterWeb/faces/update
- request.getContextPath() = /FilterWeb
- request.getServletPath() = /faces
- request.getQueryString() = userName=zhangsan&age=20
- request.getPathInfo() = /update
- request.getPathTranslated() = D:\FilterProject\FilterWeb\update
- request.getProtocol() = HTTP/1.1
- request.getMethod() = GET
- request.getScheme() = http
- request.getRequestedSessionId() = 0D5219B7FF11D47EBE95B2E6A31076B5
- request.isRequestedSessionIdFromCookie() = true
- request.isRequestedSessionIdFromURL() = false
- request.isRequestedSessionIdValid() = true
- request.getAuthType() = null
getContextPath、getServletPath、getRequestURI的区别
标签:test ica row tom windows header xtend method 相对
原文地址:http://www.cnblogs.com/yasepix/p/7442368.html