问题:直接用后台代码做了页面跳转(没有前端代码)。想实现效果:页面请求跳转的同时,服务器返回给页面cookie与uuid。
1.java代码:
@Controller
@RequestMapping("/acc")
public class beifen {
@AccessLimit(seconds = 5, maxCount = 2)//访问控制,5秒内只能访问2次
@ResponseBody
@GetMapping
public Result access(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.getRequestDispatcher("/static/wjupload.html").forward(request, response);
doGet(request,response);
return new Result(true, StatusCode.OK, "成功");
}
public void doGet(HttpServletRequest request, HttpServletResponse response) {
boolean a = response.isCommitted();
System.out.println(a);
//创建cookie
try {
UUIDUtils uuidUtils = new UUIDUtils();
String getuuid = uuidUtils.getId();
response.setContentType("text/html;charset=uft-8");
PrintWriter pw = response.getWriter();
//当用户访问该servlet时, 就将信息创建到该用户的cookie中
//1. 现在服务器端创建一个cookie
Cookie myCookie = new Cookie("getuuid", getuuid);
myCookie.setPath("/");
//2. 该cookie存在的时间 以秒为单位
myCookie.setMaxAge(30000);
//如果你不设置存在时间,那么该cookie将不会保存
//3. 将该cookie写回到客户端
response.addCookie(myCookie);
pw.println("已经创建了cookie");
} catch (Exception ex) {
ex.printStackTrace();
}
// 读取cookie
try {
response.setContentType("text/html;charset=utf-8");
PrintWriter pw = response.getWriter();
//从客户端得到所有cookie信息
Cookie[] allCookies = request.getCookies();
int i = 0;
//如果allCookies不为空...
if (allCookies != null) {
//从中取出cookie
for (i = 0; i < allCookies.length; i++) {
//依次取出
Cookie temp = allCookies[i];
if (temp.getName().equals("getuuid")) {
//得到cookie的值
String val = temp.getValue();
pw.println("getuuid=" + val);
break;
}
}
if (allCookies.length == i) {
pw.println("cookie 过期");
}
} else {
pw.println("不存在color1这个cookie/或是过期了!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
2.出现问题:
启动后,页面没有cookie
3.后端控制台报错:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:584)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:227)
at com.example.xiangmu.controller.beifen.doGet(beifen.java:37)
at com.example.xiangmu.controller.beifen.access(beifen.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1732)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
java.lang.IllegalStateException: getOutputStream() has already been called for this response
4.在网上找了很多相关问题,找到几个可能的原因:
request.getRequestDispatcher("/static/wjupload.html").forward(request, response);
doGet(request,response);
return new Result(true, StatusCode.OK, "成功");
(1)能够导致响应已经committed的操作包括:forward, redirect, flushBuffer。
在一次响应commit之前,所有的内容输出都将写入servlet引擎的缓冲区(tomcat或
weblogic的内容空间), 而在commit之后,上一次response向缓冲区写入的内容,将清空。
PrintWriter pw = response.getWriter();
//使用的是springboot框架
(2)如果用了OutputStream,而web容器生成的servlet代码中有out.write(””),这个和JSP中调用的
response.getOutputStream()冲突。out.write()这个是字符流,而response.getOutputStream()
是字节流,你不能在同一个页面中调用多个输出流。无论先调用哪一个,在调用第二个时都会抛出
IllegalStateException,因为在jsp中,out变量是通过response.getWriter得到的。在多个使用了
在JSP页面做输出的时候有两种方式.一是通过JspWriter,另一个是通过OutputStream,但二者互相排
斥.如果并存的话就会报告以上异常.