Servlet的Request中和用户请求路径相关的操作有多个(网络整理)

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://localhost:8080/FilterWeb/update.do?userName=zhangsan&age=20
页面返回的结果如下:
request.getRequestURL() = http://localhost:8080/FilterWeb/update.do
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://localhost:8080/FilterWeb/faces/update?userName=zhangsan&age=20
页面返回的结果如下:
request.getRequestURL() = http://localhost:8080/FilterWeb/faces/update
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

 

你可能感兴趣的:(tomcat,linux,windows,Web,servlet)