Tag Archives: forward()

Servlet jump mode sendredirect() and forward ()

A super detailed tutorial: springboot integrates mybatis plus>>>

In web application services, we often face the jump between different servlets. At present, we can achieve it in the following two ways:

1.RequestDispatcher.forward()

2.ServletResponse.sendReDirect()

The difference between the two is as follows

1. The redirect mode can be accessed across applications, and forward can only jump in the same application

2. Forward client accesses the server, and the server finds the corresponding URL to jump to and returns it to the client. The client’s address bar shows the original URL

The server finds the URL to jump and sends it to the client, then the client sends the request to the new URL again. Guest

The address bar of the client shows the URL of the second request

3. Data transmission: the data in the request can be accessed by forward, but not by redirect, because redirect is already a new request

Understanding of forward jump:

1. Get jump

The getrequestdispatcher method uses string parameter to describe the path of ServletContext scope. The path must be relative to the root of the ServletContext and begin with a ‘/’. This method uses the path to find the servlet, wraps it with the requestdispatcher object, and then returns the result object. If there is no servlet based on the specified path, provide a requestdispatcher to return the path value

The getnameddispatcher method uses a string parameter to represent the servlet name known to ServletContext. If the servlet is found, wrap it with the requestdispatcher object and return it. If there is no servlet associated with the specified name, the method must return null

In order to use the relative path of the current request (not the relative path of the ServletContext root) to obtain the requestdispatcher object, the ServletRequest interface provides the following methods:

getRequestDispatcher

The behavior of this method is similar to the method with the same name of ServletContext. The servlet container uses the information in the request object to convert the specified relative path into a full path. For example, for the context with the root of ‘/’, request/Garden/tools.html. The requestdispatcher obtained through servletrequest.getrequestdispatcher (“header. HTML”) is actually equivalent to calling servletcontext.getrequestdispatcher (“/ Garden/header. HTML”)

Java code

request.getRequestDispatcher(“/uu”).forward(request,response)

this.getServletContext().getRequestDispatcher(“/uu”).forward(request,response)

this.getServletContext().getNamedDispatche(“ServletName”).forward(request,response)

1.1 query string of requestdispatcher path

ServletContext and ServletRequest use the path parameter to create the method of the requestdispatcher object, allowing to add the query string after the path. For example, a developer can obtain a requestdispatcher by the following code:

String path = “/raisons.jsp?orderno=5”;

RequestDispatcher rd = context.getRequestDispatcher(path);

rd.include(request, response);

The parameters specified in the query string are used to create the requestdispatcher, which takes precedence over other parameters with the same name passed to the servlet. This parameter is associated with the requestdispatcher and is only valid in include or forward calls

2 using requestdispatcher

To use the requestdispatcher, the servlet can call the include or forward method of the requestdispatcher interface. The parameters of these methods can be the request and response parameters passed through the service method of the servlet interface, or they can be instances of the subclasses of the request and response wrapper classes introduced in specification 2.3. In the latter case, the wrapper instance must wrap the request and response objects that the container passes to the service method

The container vendor must ensure that the forwarding request to the target servlet and the original request are in the same VM and thread

3 include method

The include of the requestdispatcher interface can be called at any time. The target servlet of the include method can fully access the request object, but there are some restrictions on using the response object

It can only write information to the ServletOutputStream or writer of the response object, submit the response (output the content to the tail of the response buffer), or explicitly call the flushbuffer method of the servletresponse interface. It cannot set the header or call any method that affects the response header. Any attempt like that has to be ignored

3.1 request parameters included

In addition to the servlet obtained using the getnameddispatcher method, the servlet of include can access the path to which it was called. Set the following request properties:

javax.servlet.include.request_ uri

javax.servlet.include.context_ path

javax.servlet.include.servlet_ path

javax.servlet.include.path_ info

javax.servlet.include.query_ string

These properties of the include servlet are accessed through the getattribute method of the request object

If the included servlet is obtained through the getnameddispatcher method, these properties are not set

4. Forward method

The forward method of the requestdispatcher interface can only be called by the calling servlet when the output has not been submitted to the client. If the output data exists in the uncommitted response buffer, the content must be cleared before the service method call of the target servlet (otherwise, the output of out. Println() is in the buffer and invalid). If the response has been submitted, an IllegalStateException must be thrown. The path element of the request object exposed to the target servlet must be mapped to the path used to get the requestdispatcher

The only exception is if the requestdispatcher is obtained through the getnameddispatcher method, in which case the path element of the request object must map to the original request

Before the forward method of the requestdispatcher interface returns, the servlet container must send, submit, and close the response content

Java code

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException{

Stringpage=request.getParameter(“page”);// In forward, it is covered by page = 1 in the URL

response.setCharacterEncoding(“UTF-8”);

//clearcache.

response.setHeader(“Pragma”,”No-cache”);

response.setHeader(“Cache-Control”,”no-cache”);

response.setHeader(“Cache-Control”,”no-store”);

response.setDateHeader(“Expires”,0);

PrintWriterout=response.getWriter();

out.println(“< html>”);

out.println(“< head>& lt; title> china</ title>& lt;/ hea>”);

out.println(“< body>”);

out.print(“==”);

out.println(“< br/>”);// JSP page wrap

out.println(“</ body>”);

out.println(“</ html>”);

//if out. Flush or out. Close (). Indicates that the response has been submitted, an IllegalStateException is thrown. Requestdispatcher. Forward(): will not execute

//out.flush();

//out.close();

//there are three jump modes

//request.getRequestDispatcher(“/productInfo.do?page=1”).forward(request,response);

}

4.1 query string

The request forwarding mechanism is responsible for passing query strings when forwarding or including requests

5 error handling

If the target servlet of the requestdispatcher throws a runtime exception of type ServletException or IOException or a checked exception, it should be passed to the calling servlet. All other exceptions should be wrapped as ServletException exceptions, and the root of the exception is set to the original exception before passing

Understanding of relative path:

“/” in forward is relative to web application.
from http://localhost : forward in 8080/test/GW/page.jsp
& lt; jsp:forward page= “OtherPage.jsp”/> After the JSP page is parsed, it is converted to pagecontext. Forward (“otherpage. JSP”)
“/ otherpage. JSP” submit to http://localhost : 8080/test/otherpage. JSP
“otherpage. JSP” submitted to http://localhost :8080/Test/gw/OtherPage.jsp

< form action=”/ddd”> And response. Sendredirect (“/ DDD”); Relative to the server root path.
suppose that the forwarding code is contained in the registered servlet – URL is/GGG/TT; JSP is/GGG/TT. Jsp:
absolute path: response.sendredirect(“ http://www.brainysoftware.com “) send http://www.brainysoftware.com
Root path: response. Sendredirect (“/ OOo”) sent to http://localhost : 8080/OOo
relative path: response.sendredirect (“OOo”) to http://localhost :8080/Test/ggg/ooo,