昨天在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
分享到:
相关推荐
Servlet之forward、sendRedirect、 include区别与使用实例 ,具体效果和过程看博文http://blog.csdn.net/evankaka/article/details/45169569
<jsp:forward page="转的页面"> 与 response.sendredirect("转的页面")的区别 sendredirect:
页面之间的跳转可以通过两种方式实现:forward和sendRedirect;forward:可以在JSP页面中使用,可以在Servlet中实现,需要的朋友可以了解下
sendRedirect()和forward()的区别,我是个新手,觉得很受用,呵呵,大家学习
sendredirect()和forward()方法的区别_xyy511的专栏-CSDN博客.mht
用于初学者,牛人不用了,jsp增删改查...
运用forward方法只能重定向到同一个Web应用程序中的一个资源。而sendRedirect方法可以让你重定向到任何URL。 表单form的action="/uu";sendRedirect("/uu");表示相对于服务器根路径。如http://localhost:8080/Test...
转发和重定向的区别 forward和redirect的区别 request.getRequestDispatcher()..forward(request,response);和 response.sendRedirect();的区别详解
Jsp利用response.sendRedirect、cookie传参
主要介绍了java 中sendredirect()和forward()方法的区别,需要的朋友可以参考下
RequestDispatcher.forward()方法和HttpServletResponse.sendRedirect()方法的区别是:前者仅是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后的地址,他是不会改变Request的值,如果你需要在下一个...
Servlet跳转方式 sendReDirectServlet跳转方式 sendReDirectServlet跳转方式 sendReDirectServlet跳转方式 sendReDirectServlet跳转方式 sendReDirect
jsp response.sendRedirect不跳转的原因分析及解决.docx
jsp跳转getRequestDispatcher()和sendRedirect()的区别.docx
jsp response.sendRedirect()用法详解.docx
<一>——forward与sendRedirect的区别 ********************************************************************************* forward:可以在JSP页面中使用,可以在Servlet中实现。 在JSP页面中使用<jsp:forward...
主要介绍了jsp跳转getRequestDispatcher()和sendRedirect()的区别,需要的朋友可以参考下
最近做项目时遇到一个问题,明明加了... 用””/>则不同,它是直接在server做的,浏览器并不知道,也不和浏览器打交道,这从浏览器的地址并不变化可以看出。 所以使用response.sendRedirect时就需要注意以下两点: 1、
9. 重定向(forward与sendRedirect)区别 6 10. JSP和Servlet的区别 7 11. Cookie学习 7 12. Session学习 9 Servlet技术总结 9 1. Servlet生命周期 9 2. Servlet的方法说明: 10 3. Servlet 在web.xml中的配置 10 ...