`

全解forward()和sendRedirect

阅读更多

昨天在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

 
分享到:
评论

相关推荐

    forward PK sendredirect

    在Web开发中,"forward"和"sendRedirect"是两种常用的HTTP请求处理方式,它们都是用来实现页面跳转,但工作原理和应用场景有所不同。这里我们将深入探讨这两种方法的区别。 首先,`jsp:forward page="转的页面"`是...

    forward-sendRedirect

    在Java Web开发中,"forward-sendRedirect"是一个关键的概念,主要涉及到Servlet和JSP之间的页面跳转技术。这里我们将深入探讨这两个方法的工作原理及其在实际应用中的差异。 首先,我们来了解一下`forward()`方法...

    Servlet之forward、sendRedirect、 include区别与使用实例

    Servlet之forward、sendRedirect、 include区别与使用实例 ,具体效果和过程看博文http://blog.csdn.net/evankaka/article/details/45169569

    sendRedirect()和forward()

    sendRedirect()和forward()方法的区别 sendRedirect()和forward()方法都是Servlet编程中常用的方法,它们可以将客户端的请求重定向或转发到其他的资源,如Servlet、JSP页面或HTML文件中。但是,这两个方法之间存在...

    jsp中页面之间的跳转forward与sendRedirect的区别

    在JSP(Java Server Pages)中实现页面间的...总结来说,forward和sendRedirect是JSP中实现页面跳转的两种基本方法,它们在性能、使用场景和实现效果上各有优劣。开发者需要根据具体的应用场景和需求来选择合适的方法。

    sendredirect()和forward()方法的区别_xyy511的专栏-CSDN博客.mht

    sendredirect()和forward()方法的区别_xyy511的专栏-CSDN博客.mht

    response_sendRedirect和request_getRequestDispatcher()_forward的区别.

    运用forward方法只能重定向到同一个Web应用程序中的一个资源。而sendRedirect方法可以让你重定向到任何URL。 表单form的action="/uu";sendRedirect("/uu");表示相对于服务器根路径。如http://localhost:8080/Test...

    Servlet跳转方式sendReDirect

    在Servlet中,有两种主要的跳转方式:`RequestDispatcher.forward()`和`ServletResponse.sendRedirect()`。这两种方法在处理客户端请求时有不同的行为和应用场景。 1. `RequestDispatcher.forward()` - `forward()...

    Jsp利用response.sendRedirect、cookie传参

    ### JSP中利用`response.sendRedirect`与...总之,在JSP开发中,熟练掌握`response.sendRedirect`和Cookie的使用,能够帮助开发者更灵活地控制页面流程,有效管理用户数据,从而构建更加健壮和高效的Web应用程序。

    java 中sendredirect()和forward()方法的区别

    Java 中 sendRedirect() 和 forward() 方法的区别 Java 中的 sendRedirect() 和 forward() 方法都是用于页面跳转的,但是它们之间存在着一些关键的区别。 1. 跳转方式 sendRedirect() 方法可以将请求重定向到任何...

    jsp response.sendRedirect()用法详解.docx

    下面我们将详细介绍jsp response.sendRedirect()函数的用法和实现原理。 首先,我们需要了解response和request对象。response对象是JSP中的内置对象,负责处理用户的请求,而request对象则是猎取用户的恳求。...

    jsp response.sendRedirect不跳转的原因分析及解决.docx

    ### jsp response.sendRedirect不跳转的原因分析及解决 #### 一、问题背景 在进行Web应用开发的过程中,经常需要使用到服务器端重定向的技术。在Java Server Pages (JSP) 技术中,`response.sendRedirect()` 是一...

    转发和重定向的区别 forward和redirect的区别

    在Java的Servlet编程中,`转发`和`重定向`是两种不同的机制,用于在服务器端将客户端的请求导向不同的资源。它们的主要区别在于请求处理流程、URL改变以及数据共享方式。 1. 转发(Forward): - 使用`...

    java中dispcter和forward的区别

    `forward()`操作发生在服务器端,对客户端浏览器而言是透明的,即浏览器的URL不会改变,而且请求和响应对象在转发过程中保持不变,可以共享`Request`范围内的数据。 相对地,`sendRedirect()`是`...

    jsp跳转getRequestDispatcher()和sendRedirect()的区别.docx

    ### jsp跳转getRequestDispatcher()和sendRedirect()的区别 在Java Web开发中,经常会遇到页面间的跳转操作,其中两种常见的方法是`getRequestDispatcher()`和`sendRedirect()`。这两种方式虽然都能实现页面跳转,...

    forward与redirect区别

    在开发Web应用时,我们经常会遇到“forward”和“redirect”的概念,它们是两种不同的页面跳转方式。理解和掌握它们的区别对于优化用户交互和处理请求流至关重要。 **forward(转发)**: - 转发是由服务器端完成的...

    jsp:forward方法实例

    本实例将深入探讨`jsp:forward`方法的使用及其背后的原理,帮助你更好地理解和应用这一功能。 `jsp:forward`标签通常用在JSP页面中,它的主要作用是将当前请求转发到另一个资源,如另一个JSP页面、Servlet或者静态...

    forward方法实现请求转发

    - **原因分析**:由于`forward`方法只是简单地将请求和响应对象传递给另一个资源,因此这些信息不会被改变。 - **解决策略**:开发者需要注意,在转发过程中可能需要调整响应状态码或添加额外的响应头信息时,应该...

    JavaWeb中URL重写用Forward方式并使其后filter可以拦截内部转发

    在JavaWeb中,我们通常会借助于像`RequestDispatcher`的`forward()`方法来实现页面间的转发,而不是直接使用`response.sendRedirect()`。`forward()`方法能在服务器端完成请求的转向,使得客户端浏览器的URL保持不变...

Global site tag (gtag.js) - Google Analytics