代码:
package org.example.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @RestController public class ResponseParamController { @RequestMapping(value = "/responseTest1") //获取请求行数据 public void test1(HttpServletRequest request,HttpServletResponse response) { response.setStatus(302); response.setHeader("location","/test1"); } @RequestMapping(value = "/responseTest15") public String test15(HttpServletResponse response) { response.setHeader("content-type", "text/html;charset=UTF-8"); //告知浏览器使用什么码表 //告知客户端不缓存 response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache"); response.setDateHeader("expires", 0); //添加Cookie response.addCookie(new Cookie("cook-001-name" ,"cookie-001-value")); response.addCookie(new Cookie("cook-002-name" ,"cookie-002-value")); response.setStatus(404); //我们将状态设置成404 return "test2"; } }
方法2:
package org.example.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @RestController public class ResponseParamController { @RequestMapping(value = "/responseTest1") //获取请求行数据 public void test1(HttpServletRequest request,HttpServletResponse response) throws IOException { // response.setStatus(302); // response.setHeader("location","/test1"); response.sendRedirect("/test1"); } @RequestMapping(value = "/responseTest15") public String test15(HttpServletResponse response) { response.setHeader("content-type", "text/html;charset=UTF-8"); //告知浏览器使用什么码表 //告知客户端不缓存 response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache"); response.setDateHeader("expires", 0); //添加Cookie response.addCookie(new Cookie("cook-001-name" ,"cookie-001-value")); response.addCookie(new Cookie("cook-002-name" ,"cookie-002-value")); response.setStatus(404); //我们将状态设置成404 return "test2"; } }
标签:test1,重定向,value,Cookie,import,response,setHeader From: https://www.cnblogs.com/xiaobaibailongma/p/17045017.html