In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve redirection on the Servlet/JSP server. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Redirect related classes on the JSP server side
The redirection technology of JSP server involves several interfaces such as javax.servlet.ServletContext, javax.servlet.RequestDispatcher, javax.servlet.http.ServletRequest, javax.servlet.http.ServletResponse and so on.
JSP server-side redirection mode
There are two ways to redirect on the JSP server side, one is to use HttpServletResponse's sendRedirect () method, and the other is to use RequestDispatcher's forward () method. These two ways are introduced below.
HttpServletResponse.sendRedirect () method
The HttpServletResponse interface defines the sendRedirect () method that can be used for redirection. The code is as follows:
Public void sendRedirect (java.lang.String location) throws java.io.IOException
This method directs the response to the new URL specified by the parameter location. Location can be an absolute URL, such as response.sendRedirect ("http://java.sun.com") can also use a relative URL." If the location starts with "/", the container considers it to be relative to the root of the current Web application, otherwise the container will resolve to the URL relative to the current request. This method of redirection will cause the request URL of the client browser to jump. The new URL address can be seen from the address bar in the browser, which is similar to the implementation of setting the HTTP response header information above.
RequestDispatcher.forward () method
RequestDispatcher is a wrapper around a Web resource that can be used to pass the current request to the resource or to include new resources in the current response. Two methods are defined in the RequestDispatcher interface, as shown in the following code:
Public interface RequestDispatcher {void forward (ServletRequest request, ServletResponse response); void include (ServletRequest request, ServletResponse response);}
The forward () method redirects the current request and response to the resource specified by the RequestDispacher. This is widely used in actual projects, because completing a business operation often requires multiple steps, and after each step completes the corresponding processing, move on to the next step. For example, business processes are usually processed in Servlet, and the results of the processing are transferred to a JSP page for display. This looks similar to the functionality of the Servlet chain, but there are some differences. A RequestDispatcher object can send a request to any server resource, not just another Servlet. The include () method will include the output of the Request Dispatcher resource in the current output.
Note that the forward () method can be called only if the response has not been output to the client, and if the page cache is not empty, the cache will be automatically cleared before the relocation. Otherwise, an IllegalStateException exception will be thrown.
How to get RequestDispatcher
There are three ways to get the Request Dispatcher object.
1.javax.servlet. The getRequestDispatcher (String path) method of ServletRequest, where path can be a relative path, but cannot go beyond the current Servlet context. If path starts with "/", it resolves to the root relative to the current context.
2.javax.servlet. The getRequestDispatcher (String path) method of ServletContext, where path must start with "/" and the path is relative to the current Servlet context. You can call ServletContext's getContext (String uripath) to get another Servlet context, and you can go to a server resource link to the external context.
3. Use javax.servlet. ServletContext's getNamedDispatcher (String name) gets a Web resource named name, including Servlet and JSP pages. The name of this resource is specified in the Web application deployment description file web.xml.
There are slight differences in the use of these three methods. For example, the following is the configuration file web.xml for an application:
FirstServletorg. Javaresearch.redirecttest.ServletOneSecondServletorg.javaresearch. Redirecttest.ServletTwoFirstServlet/servlet/firstservlet/SecondServlet/servlet/secondservlet/
Two Servlet are defined, named FirstServlet and SecondServlet, and the corresponding classes are org.javaresearch. Redirecttest.ServletOne and org. Javaresearch.redirecttest.ServletTwo . It can be accessed in the browser through a link similar to the following:
Http://localhost:8080/servlet/firstservlet/
Using the method in 1, for example, in firstservlet, you can write the following code:
RequestDispatcher rd = request.getRequestDispatcher ("secondservlet"); rd.forward (request, response)
At this point, control will shift to the second Servlet.
Using the method in 2, you can get the RequestDispatcher code from Servlet Context as follows:
RequestDispatcher rd = getServletContext () .getRequest Dispatcher ("/ servlet/secondservlet"); rd.forward (request, response)
Using the method in 3, from the above web. The xml configuration file can see that two Servlet are defined, named FirstServlet and SecondServlet, so you can get the named Dispatcher:
RequestDispatcher rd = getServletContext () .getNamedDispatcher ("SecondServlet"); rd.forward (request, response)
You can also redirect to SecondServlet.
Redirects in JSP pages
JSP is compiled into a Servlet to run after parsing, so the above redirection code can also be used in JSP, and JSP provides a more convenient operation, as follows:
At this point in the execution of the JSP page, the current processing is terminated and control is handed over to nextpage.jsp.
How to choose the JSP server side
The difference between the RequestDispatcher.forward () method and the HttpServletResponse.sendRedirect () method is that the former is only the redirection of control in the container, and the redirected address will not be displayed in the client browser address bar, while the latter is a complete jump, and the browser will get the redirected address and resend the request link. In this way, the jumped link address can be seen from the browser's address bar. Therefore, the former is more efficient, using the Request Dispatcher.forward () method as much as possible when the former meets the needs, and it also helps to hide the actual links. In some cases, for example, if you need to jump to a resource on another server, you must use the HttpServletResponse.sendRequest () method.
This is the end of the article on "how to redirect the Servlet/JSP server". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.