java 中sendredirect()和forward()方法的区别和一些学习整理(Differences between sendredirect () and forward () methods in Java and some learning and sorting)

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

 HttpServletResponse.sendRedirect与RequestDispatcher.forward方法都可以实现获取相应URL资源。

sendRedirect实现请求重定向,forward实现的是请求转发。

在web服务器内部的处理机制也是不一样的。 

1. 跳转方式

 运用forward方法只能重定向到同一个Web应用程序中的一个资源。而sendRedirect方法可以让你重定向到任何URL。 

 表单form的action= “/uu “;sendRedirect( “/uu “);表示相对于服务器根路径。

2.forward重定向后,浏览器url地址不变,sendRedirect转发后,浏览器url地址变为目的url地址。

forward()无法重定向至有frame的jsp文件,可以重定向至有frame的html文件, 同时forward()无法在后面带参数传递,

比如servlet?name=frank,这样不行,可以程序内通过response.setAttribute( “name “,name)来传至下一个页面.  

3. 使用forward重定向的过程,是浏览器先向目的Servlet发送一次Request请求,然后再服务器端由Servlet再将请求发送到目的url,再由服务器端Servlet返回Response到浏览器端。浏览器和服务器一次请求响应。 

使用sendRedirect转发的过程,浏览器先向目的Servlet发送一次请求,Servlet看到sendRedirect将目的url返回到浏览器,浏览器再去请求目的url,目的url再返回response到浏览器。浏览器和服务器两次请求响应。

4. forward方法的调用者与被调用者之间共享Request和Response

sendRedirect方法由于两次浏览器服务器请求,所以有两个Request和Response。 

如果使用request.setAttribute传递一些属性就需要用forward,如果想要跳转到别的应用的资源,就需要用sendRedirect。 

5.无论是forward方法还是sendRedirect方法调用前面都不能有PrintWriter输出到客户端。 

forward方法报错: Java.lang.IllegalStateException: Cannot forward after response has been committed 

sendRedirect报错:java.lang.IllegalStateExceptionat org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)

实现 Enumeration 接口的对象,它生成一系列元素,一次生成一个。连续调用 nextElement 方法将返回一系列的连续元素。

例1,要输出 Vector<E> v 的所有元素,可使用以下方法:

   for (Enumeration<E> e = v.elements(); e.hasMoreElements();)

       System.out.println(e.nextElement());

例2

Enumeration headerNames = request.getHeaderNames();

while (headerNames.hasMoreElements()) {

String headerName = (String) headerNames.nextElement();

out.print(headerName + ” : ” + request.getHeader(headerName) + “<br />”);

}

实现该接口的对象由一系列的元素组成,可以连续地调用nextElement()方法来得到 Enumeration枚举对象中的元素。Enumertion接口中仅定义了下面两个方法。

·boolean hasMoreElemerts()

测试Enumeration枚举对象中是否还含有元素,如果返回true,则表示还含有至少一个的元素。

·Object nextElement()

如果Bnumeration枚举对象还含有元素,该方法得到对象中的下一个元素。

servlet容器对url的匹配过程:

当一个请求发送到servlet容器的时候,容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url,比如我访问的是 http://localhost/test/aaa.html,我的应用上下文是test,容器会将http://localhost/test去掉,剩下的/aaa.html部分拿来做servlet的映射匹配。这个映射匹配过程是有顺序的,而且当有一个servlet匹配成功以后,就不会去理会剩下的servlet了(filter不同,后文会提到)。其匹配规则和顺序如下:

1. 精确路径匹配。例子:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,这个时候,如果我访问的url为http://localhost/test ,这个时候容器就会先进行精确路径匹配,发现/test正好被servletA精确匹配,那么就去调用servletA,也不会去理会其他的servlet了。

2. 最长路径匹配。例子:servletA的url-pattern为/test/*,而servletB的url-pattern为/test/a/*,此时访问http://localhost/test/a时,容器会选择路径最长的servlet来匹配,也就是这里的servletB。

chain.doFilter作

1.般filter都是个链,web.xml 配置了个就有个。个个的连在起

request -> filter1 -> filter2 ->filter3 -> …. -> request resource.

2.chain.doFilter将请求转发给过滤器链下个filter , 如果没有filter那就是你请求的资源

举例说明

1.input.jsp 是来提交输的 : 当提交后,过滤器检测姓名和年龄 .

2.如果正常的话会提交给output.jsp 如果不正常提交给erroroutput.jsp.

3.在此 , 同时也有个过滤器,防乱码问题存在 , 该过滤器检测个页是否设置了字符编码,如果没有则进设置。

————————

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

 HttpServletResponse.sendRedirect与RequestDispatcher.forward方法都可以实现获取相应URL资源。

Sendredirect implements request redirection, and forward implements request forwarding.

The processing mechanism inside the web server is also different.

1. Jump method < / strong >

Using the forward method can only redirect to one resource in the same web application. The sendredirect method allows you to redirect to any URL.

Action = “/ UU” of the form; sendRedirect( “/uu “); Represents the root path relative to the server.

2. After forward reset, the browser URL address remains unchanged. After sendredirect forwarding, the browser URL address becomes the destination URL address.

Forward() cannot be redirected to JSP file with frame, but can be redirected to HTML file with frame. Meanwhile, forward() cannot be passed with parameters,

Like servlets? Name = frank, this will not work. You can use response in the program SetAttribute (“name”, name) to pass to the next page

3. The process of using forward redirection is that the browser first sends a request to the destination servlet, then the server-side servlet sends the request to the destination URL, and then the server-side servlet returns the response to the browser. The browser and server respond to the request at once.

In the process of sendredirect forwarding, the browser first sends a request to the destination servlet. The servlet sees that sendredirect returns the destination URL to the browser, the browser requests the destination URL, and the destination URL returns the response to the browser. The browser and server respond to the request twice.

4. The caller and callee of forward method share request and response

The sendredirect method has two requests and responses because the browser server requests twice.

If you use request SetAttribute needs to use forward to pass some attributes. If you want to jump to the resources of other applications, you need to use sendredirect.

5. No printwriter can be output to the client before the call of forward method or sendredirect method.

forward方法报错: Java.lang.IllegalStateException: Cannot forward after response has been committed 

sendRedirect报错:java.lang.IllegalStateExceptionat org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)

The object that implements the enumeration interface. It generates a series of elements, one at a time. Successive calls to the nexterelement method will return a series of consecutive elements.

Example 1: to output vector & lt; E> For all elements of V, the following methods can be used:

   for (Enumeration<E> e = v.elements(); e.hasMoreElements();)

       System.out.println(e.nextElement());

Example 2

Enumeration headerNames = request.getHeaderNames();

while (headerNames.hasMoreElements()) {

String headerName = (String) headerNames.nextElement();

out.print(headerName + ” : ” + request.getHeader(headerName) + “<br />”);

}

The object implementing the interface is composed of a series of elements. You can call the nexterelement () method continuously to get the elements in the enumeration object. Only the following two methods are defined in the enumeration interface.

·boolean hasMoreElemerts()

Test whether the enumeration enumeration object still contains elements. If true is returned, it indicates that it also contains at least one element.

·Object nextElement()

If the bnumeration enumeration object also contains elements, this method gets the next element in the object.

< strong > matching process of servlet container to URL: < / strong >

When a request is sent to the servlet container, the container will first subtract the path of the current application context from the requested URL as the mapping URL of the servlet. For example, I visited http://localhost/test/aaa.html , my application context is test, and the container will http://localhost/test Remove the rest / AAA The HTML part is used for servlet mapping and matching. This mapping matching process is sequential, and when a servlet is successfully matched, it will not pay attention to the remaining servlets (filter is different, which will be mentioned later). The matching rules and order are as follows:

1. Exact path matching. Example: for example, the URL pattern of servleta is / test, and the URL pattern of servletb is / *. At this time, if the URL I visit is http://localhost/test At this time, the container will first carry out accurate path matching. If it is found that / test is exactly matched by servleta, it will call servleta and ignore other servlets.

2. Longest path matching. Example: the URL pattern of servleta is / test / *, while the URL pattern of servletb is / test / A / *. Access http://localhost/test/a When, the container will select the servlet with the longest path to match, that is, servletb here.

chain.doFilter作

1. General filters are a chain, web XML configuration has one. All of them are connected

request -> filter1 -> filter2 ->filter3 -> …. -> request resource.

2.chain. Dofilter forwards the request to the next filter in the filter chain. If there is no filter, it is the resource you requested

Examples

1.input. JSP is to submit input: when submitted, the filter detects the name and age

2. If normal, it will be submitted to output If the JSP is abnormal, submit it to erroroutput jsp.

3. At the same time, there is also a filter. The anti aliasing problem exists. The filter detects whether a page is set with character code. If not, it will be set.