首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

request对象的一些易搅混的函数

2012-10-30 
request对象的一些易混淆的函数1.getServletConfig() 与getServletContext().??? 这两个方法其实区别是比

request对象的一些易混淆的函数

1.getServletConfig() 与getServletContext().
??? 这两个方法其实区别是比较明显的.只是需要理解ServletConfig与ServletContext两个对象。Head First Servlet and Jsp书中对这两个方法特别的提醒了一番.ServletConfig是表对web.xml文件中servlet配置的封装,每一个servlet都持 有一个ServletConfig封装对象.可以通过这个对象读取servlet配置中的参数和存取servlet属性等,但要注意的是servlet对象本身并不是线程安全的,当有多个请求同时请求同一个servlet的时候要注意线程安全.而ServletContext是个全局的对象每个web应用 只有唯一的一个ServletContext对象,这个对象的作用跟ServletConfig对象很像但是是针对整个应用的,同样访问 ServletContext对象的时候也需要注意线程安全.

2.getRequestURL() /getRequstURI()/getContextPath()/getServletPaht()/getPathInfo()

???说这四个函数之前不得不说servlet mapping,我们知道从客户端请求到执行一个服务器端的servlet响应请求,这个过程是通过servlet mapping来查找的,容器找到所有的servlet mapping配置中的uri-pattern与当前请求最匹配的一项,然后找到servlet-name,找到servlet名字后,再到 servlet配置中找到这个servlet-name对应的servlet-class,然后找到这个servlet对象或实例化并且初始化这个类(如果还未实例化的话),最后执行servlet对象的service方法.

servlet mapping配置中url-pattern一共有三种方式

1.全名称的方式:

形如:/abc/efg.action

其中.action为后缀名,后缀名可谓任意形式如.do,.action等,也可以没有后缀名,这种形式必须以/开头

2.目录形式:

形如:/abc/*

这种形式必须以/开头

3.后缀名形式:

形如:*.action

其中后缀名可为任意形式.

下面就来讲将那几个方法:

假设一个项目web.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
? <display-name>test</display-name>
? <welcome-file-list>
??? <welcome-file>index.html</welcome-file>
??? <welcome-file>index.htm</welcome-file>
??? <welcome-file>index.jsp</welcome-file>
??? <welcome-file>default.html</welcome-file>
??? <welcome-file>default.htm</welcome-file>
??? <welcome-file>default.jsp</welcome-file>
? </welcome-file-list>
??
? <servlet>
? ??? <servlet-name>test</servlet-name>
? ?? <servlet-class>kzq.test.ServletTest</servlet-class>
? </servlet>
??
? <servlet-mapping>
? ??? <servlet-name>test</servlet-name>
?? ?? <url-pattern>/abc/abc.action</url-pattern>
? </servlet-mapping>
</web-app>

?

kzq.test.ServletTest这个类源码如下:

package kzq.test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletTest extends HttpServlet {
??? @Override
??? protected void doGet(HttpServletRequest req, HttpServletResponse resp)
??? ??? ??? throws ServletException, IOException {
??? ??? // TODO Auto-generated method stub
??? ??? System.out.println("RequestURL: " + req.getRequestURL());
??? ??? System.out.println("RequestURI: " + req.getRequestURI());
??? ??? System.out.println("ContextPath: " + req.getContextPath());
??? ??? System.out.println("ServletPath: " + req.getServletPath());
??? ??? System.out.println("PathInfo: " + req.getPathInfo());
??? }
??? @Override
??? protected void doPost(HttpServletRequest req, HttpServletResponse resp)
??? ??? ??? throws ServletException, IOException {
??? ??? // TODO Auto-generated method stub
??? ??? doGet(req, resp);
??? }
????
}

客户端请求为:? http://localhost:8080/test/abc/abc.action
这几个方法返回的值,都去掉了请求中的查询字符串,即?号及其后面的值,除了getRequestURL外,其他几个方法的返回值都是以/开头的

getRequestURL():返回完整的请求url :? http://localhost:8080/test/abc/abc.action

getRequestURI():返回资源请求路径: /test/abc/abc.action

getContextPath():返回项目上下文名称,一般来说是/加上项目名称: /test

getServletPath():返回ContextPath后面的一串值,这个方法跟servlet mapping配置中url-pattern大有关系,如果url-pattern是全名称形式的话,那么一旦一个请求匹配上这个url- pattern,那么这个方法返回url-pattern中的值,如此处的/abc/abc.action,如果是目录形式的话,则返回url- pattern中去掉*号后及其前面的/后的值,如果是后缀名形式的话则客户端输入的是什么则全盘返回什么,包括后缀名.

getPathInfo():这个方法跟getServletPath()又有莫大的关系,请求字符串中(去掉查询字符串后),除去SevletPath后剩下的就是PathInfo了,除去ServletPath后什么都没剩下的话就返回null

上面例子中url-pattern为全名称形式/abc/abc.action的话返回:

RequestURL: http://localhost:8080/test/abc/abc.action
RequestURI: /test/abc/abc.action
ContextPath: /test
ServletPath: /abc/abc.action
PathInfo: null


url-pattern为目录形式/abc/*的话返回:

RequestURL: http://localhost:8080/test/abc/abc.action
RequestURI: /test/abc/abc.action
ContextPath: /test
ServletPath: /abc
PathInfo: /abc.action

url-pattern为后缀名形式*.action:

RequestURL: http://localhost:8080/test/abc/abc.action
RequestURI: /test/abc/abc.action
ContextPath: /test
ServletPath: /abc/abc.action
PathInfo: null


热点排行