Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the path problem of forwarding and redirection in Servlet

Shulou Source: shulou.com Published: 2022-06-02 05:07:00 10月30日 Update

This article mainly explains "how to solve the path problem of forwarding and redirection in Servlet". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve the path problem of forwarding and redirection in Servlet.

There are two ways to obtain the Servlet forwarding object (RequestDispatcher): one is obtained through the getRequestDispatcher () method of HttpServletRequest, and the other is obtained through the getRequestDispatcher () method of ServletContext

There is only one way to redirect Servlet: the sendRedirect () method of HttpServletResponse.

The parameters of all three methods are a string in the form of URL, but there are differences in using relative or absolute paths.

◆ HttpServletResponse.sendRedirect (String)

Parameters can be specified as relative paths, absolute paths, or other Web applications.

Suppose the Servlet to which the method belongs is reached through a http://localhost/myApp/cool/bar.do request.

Relative path: response.sendRedirect ("foo/stuff.do")

The container adds parameters to the directory where the URL was originally requested to generate the complete URL-- http://localhost/myApp/cool/foo/stuff.do.

Absolute path: response.sendRedirect ("/ foo/stuff.do")

The container creates a complete URL-- http://localhost/foo/stuff.do with parameters relative to the Web application itself.

Other Web applications: response.sendRedirect ("http://www.xxx.com")

The container is directed directly to the URL.

◆ HttpServletRequest.getRequestDispatcher (String)

Parameters can be specified as relative or absolute paths.

The complete URL generated in the case of relative paths is the same as the redirection method.

The absolute path is different from Servlet redirection, in which the container generates a complete URL with parameters relative to the root directory of the Web application, that is:

The URL generated by request.getRequestDispatcher ("/ foo/stuff.do") is http://localhost/myApp/foo/stuff.do.

◆ ServletContext.getRequestDispatcher (String)

Parameter can only be specified as an absolute path, and the resulting full URL is the same as HttpServletRequest.getRequestDispatcher (String).

At this point, I believe you have a deeper understanding of "how to solve the routing problem of forwarding and redirection in Servlet". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Path method parameter generation container application problem same content learning different practical deeper three interest only character string practicality actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn Apple NVidia Linux