Two redirection methods for servlet

zhaozj2021-02-16  142

In Servlet / JSP programming, server-side redirects can be implemented by both methods:

1, use the forward method of the Javax.Servlet.RequestDispatcher interface,

2, or use the sendredirect method for the Javax.Servlet.http.httpservletResponse interface.

To use the Forward method of the RequestDispatcher interface, you should first get a RequestDispatcher object. Servlet technology provides three ways to get it:

1. Pass a String containing the path to other resources by using the GetRequestDispatcher method of the Javax.Servlet.ServletContext interface. This path is the root path relative to servletContext. 2. Pass a String containing a path to other resources by using the getRequestDispatcher method of the Javax.Servlet.ServletRequest interface. This path is relative to the current HTTP request. 3. Pass the String containing other resource names by using the GetNamedDispatcher method using the Javax.Servlet.ServletContext interface.

But note that you can only call the Forward method when you don't have output on the client. If the buffer of the current page is not empty, you must first empty the buffer before calling the Forward method. Otherwise, an IllegalStateException will be thrown. The Forward method can also be used to send requests to a static page.

The easiest way to pass a control from a servlet called an XYZSERVLET with a servlet called an XYZSERVLET using the RequestDispatcher object, the easiest way is to put the ABCSERVLET and XYZSERVLET in the same directory. With this method, you can call the abcservlet from the URL HTTP: // Domain / VirtualDir / servlet / abcservlet, call XYZSERVLET from the URL http: // domain / virtgdir / servlet / xyzservlet. Then use the Forward method very simple. You can use getRequestDispatcher from the servletRequest interface to pass the second servlet name. In the abcservlet, you can write the following code: RequestDispatcher Rd = Request.GetRequestDispatcher ("SECONDSERVLET"); rd.forward (request, response); you don't need to play / symbol in XYZSERVLET. This method is the easiest because you don't need to worry about the path to both servlets. Slightly more complex approach is to put the following String passed to the ServletRequest of getRequestDispatcher: "/ servlet / XYZServlet" If you must call forward method of RequestDispatcher object of a derived from getRequestDispatcher ServletContext, you will need to "/ VirtualDir / servlet / XYZServlet" as The path parameters are passed, as follows: RequestDispatcher rd = getServletContext (). GetRequestDispatcher ("/ servlet / xyzservlet"); rd.forward (request, response);

Converts the following code in the result servlet: PageContext.Forward ("OtherPage.jsp"); sending a command to browse Make the browser to make a request for the URL specified in Location. This method can accept absolute or relative URLs. If the parameter passed to the method is a relative URL, the web container converts it into an absolute URL before sending it to the client. If the address is relative, there is no '/', then the Web Container considers that it is relative to the current request URI.

Which skill do you use? [Quote] In order to write the most effective code, you should understand the difference between these two redirect skills. The Forward method is working within the Web Container. The SendRedirect method needs to go to a round trip to the client. So the forward method is faster than Sendredirect. However, using the Forward method has limitations, you can only redirect a resource to the same web application. The SendRedirect method allows you to redirect to any URL. Conclusion: If you can solve your problem, use the Forward method. The sendredirect method is only used when you cannot use the Forward method.

Forward does not change the path in the current browser address bar

转载请注明原文地址:https://www.9cbs.com/read-8905.html

New Post(0)