昨天在servlet中搞些页面重定向的问题,在处理页面的path时遇到了一些恼人的问题,今天来深究一下:
Javax.servlet.RequestDispatcher.forward(ServletRequest request, ServletResponse response)
Javax.servlet.http.HttpServletResponse.sendRedirect(String location)
forward
作用于服务器端,重定向后客户端浏览器地址栏的URL不变,无法通过get方式传递参数,不过可以通过HttpServletResponse. setAttribute(key, values)来做:
JDK的DOC中的描述:
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.
forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
这个方法允许一个servlet对一个请求作完处理后,并使用另一个资源作响应。
forward应该在向客户端发出响应之前被调用(response的输出流被刷新之前),如果已经向客户端发出了响应,这个方法将抛出一个“IllegalStateException”的异常。未发出响应response的输出缓冲区在调用forward会被自动的清空。
得到RequestDispatcher引用的方法:
1、 javax.servlet.ServletContext.getRequestDispatcher(String path)
JDK的DOC中的描述:
The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.
用的最多,最容易理解的方法,path必须是以“/”开头,路径是相对于全局上下文路径,对应于web应用的根目录
2、javax.servlet.ServletRequest.getRequestDispatcher(String path)
JDK的DOC中的描述:
The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.
可以是相对地址,以当前资源的根目录,但不能超出当前资源根目录。
可以是以“/”开头的地址,当前root作为根目录环境。
2、 javax.servlet.ServletContext.getNamedDispatcher(String name)
JDK的DOC中的描述:
Servlets (and JSP pages also) may be given names via server administration or via a web application deployment descriptor.
得到名为name的资源。
Servlets(也可是JSP 页面)可能会有个给定的名字,该名字是服务器或者web应用的部署描述web.xml提供的。
sendRedirect
工作在客户端,重定向后客户端浏览器地址栏的URL变为新的资源URL,可以通过get方式传递参数。
JDK的DOC的描述:
Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
这个方法可以接受相对的URL,在发送响应之前servlet容器会将其转化为绝对的URL。如果相对的URL不以“/”开头,容器将解释为是相对当前请求的路径URI,如果是以“/”开头,则解释为是相对于servlet容器的更目录环境。
注意:如果已经向客户端响应了请求,这个方法将抛出一个“IllegalStateException”的异常。调用了这个方法后,这个响应是否被提交或是否提交完毕。
sendRedirect比较简单,参数location就是代表重定向目标新资源。
localtion可以有以下几种情况:
可以是绝对地址,如:http://localhost:8080/servlet/a.jsp
可以是相对地址,相对于当前访问资源的根目录,如:a.jsp
可以是以“/”开头的地址,则认为是先对该web应用的根目录,如:/a.jsp