Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report