spring boot之http,页面状态跳转与异常处理实战

Http使用

  http的使用主要是三个类:HttpServletRequest(向客户端取数据),HttpServletResponse(向客户端发数据),HttpSession(记录浏览器和服务器的长期交互)。

HttpServletRequest

  取出http的请求头的参数和cookie数据:

 1     @RequestMapping(path= {"/request"})
 2     @ResponseBody
 3     public String request(HttpServletRequest request,
 4                                     HttpServletResponse response,
 5                                     HttpSession session) {
 6         StringBuilder sb = new StringBuilder();
 7         Enumeration<String> headerNames = request.getHeaderNames();
 8         while(headerNames.hasMoreElements()) {
 9             String name = headerNames.nextElement();
10             sb.append(name + " ::::::::::::::::::::::::::::: " + request.getHeader(name) + "<br/>");
11         }
12         for (Cookie cookie : request.getCookies()) {
13             sb.append("Cookie: ");
14             sb.append(cookie.getName());
15             sb.append(":");
16             sb.append(cookie.getValue());
17             sb.append("<br>");
18         }
19         return sb.toString();
20     }

 

  显示结果:

HttpServletResponse

  添加cookie并在页面上显示:

 1     @RequestMapping(value= {"/response"})
 2     @ResponseBody
 3     public String response( @CookieValue(value = "cookieid", defaultValue = "cookieId") String cookieId,
 4                             @RequestParam(value = "key", defaultValue = "key") String key,
 5                             @RequestParam(value = "value", defaultValue = "value") String value,
 6                             HttpServletResponse response) {
 7         response.addCookie(new Cookie(key, value));
 8         //response.addHeader(key, value);
 9         return "cookievalue: " + cookieId;
10     }

 

 显示结果:

  备注:在url中把key写入cookieid,显示时,cookieid就会显示value值,随value的改变而改变。

301,302页面跳转与HttpSession

  若访问/redirect/301,就永久跳转到/hello下(301),不经过判断。否则暂时性跳转(302)。

 1    //301永久性跳转
 2     //302暂时性跳转
 3     @RequestMapping(path = {"/redirect/{code}"})
 4     @ResponseBody
 5         public RedirectView redirect(@PathVariable("code") int code,
 6                                      HttpSession session) {
 7         RedirectView red = new RedirectView("/hello", true);
 8         if (code == 301)
 9             red.setStatusCode(HttpStatus.MOVED_PERMANENTLY);//301跳转
10         //session,记录浏览器和服务器长期的交互。
11         session.setAttribute("msg", "jump from redirect");
12         return red;
13     }

 

  其次,/hello路径:

 1     @RequestMapping(path = {"/hello"})
 2     @ResponseBody
 3     public String Hello_World(Model model,
 4                               HttpSession session) {
 5         model.addAttribute("aaa", "naive");
 6         model.addAttribute("l", "<br/>");
 7         List<String> ls = Arrays.asList(new String[] {"1","111", "2323a"});
 8         model.addAttribute("liststring", ls);
 9         model.addAttribute("user", new User("aw12"));
10         logger.info("yeah,finally i learn how to use logger");
11         //显示session的msg信息。
12         return "session" + session.getAttribute("msg") + sss.show();
13     }

  

异常及异常处理

  当key的值不是admin时,抛出异常,在exception函数中处理异常。

 1 //异常
 2     @RequestMapping(path = {"/admin"})
 3     @ResponseBody
 4     public String error(@RequestParam(value = "key", required = false) String key) {
 5         if ("admin".equals(key))
 6             return "hello admin";
 7         throw new IllegalArgumentException("Key 错误");
 8     }
 9     //处理异常
10     @ExceptionHandler
11     @ResponseBody
12     public String exception(Exception e) {
13         return "error" + e.getMessage();
14     }

  结果一,key=‘admin"时:

  

  结果二,key!="admin"时:

  

 

转载于:https://www.cnblogs.com/yulianggo/p/10467428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值