Forward: Hello and redirect: Hello

Source: Internet
Author: User

For some controller processing methods, when the return value is of the string type, the returned results may contain the forward or redirect prefix;

For example:

 1 @Controller 2 @RequestMapping("/user") 3 public class UserController { 4     @RequestMapping("/forward") 5     public String replyWithForward(HttpServletRequest request, String userId){ 6         request.setAttribute("userid", userId); 7          8         System.out.println("userId =" +userId); 9         10         return "forward:hello";11     }12     13     @RequestMapping("/redirect")14     public String replyWithRedirect(HttpServletRequest request, String userId){15         request.setAttribute("userid", userId);16         17         System.out.println("userId = "+userId);18         19         return "redirect:hello";20     }21 }

Test page hello. jsp:

1 <% @ page Language = "Java" contenttype = "text/html; charset = UTF-8" 2 pageencoding = "UTF-8" %> 3 <% 4 string userid = (string) request. getattribute ("userid"); 5 system. out. println ("the obtained userid is:" + userid); 6%> 7 <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" http://www.w3.org/TR/html4/loose.dtd "> 8 <HTML> 9 

When the path is http: // localhost: 8080/crazysnilweb/user/forward? When userid = 123, the URL in the browser does not change and the page output is:

When the path is http: // localhost: 8080/crazysnilweb/user/redirect? When userid = 123, the URL in the browser changes to http: // localhost: 8080/crazysnilweb/user/Hello, And the page output is as follows:

Note: Redirect will allow the browser to initiate a new request, so the parameters in the original request object are lost, while the target address of forward is in the current request, and the parameters in the request will not be lost;
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.