首页 > 代码库 > [Java][Web]Response学习.缓存

[Java][Web]Response学习.缓存

response.setHeader("expires", String.valueOf(System.currentTimeMillis() + 1000 * 3600));
String dataString = "aaaaa";
response.getWriter().write(dataString);

如上代码,加上 expires 有效时间后,等同于给资源加了缓存。

// 实现请求重定向 方法一
response.setStatus(302);
response.setHeader("location", "/day06");

// 实现请求重定向 方法二
response.sendRedirect("/day06");

以上两方法都是发送请求重定向到 /day06 页面。服务器会收到两次请求,浏览器地址栏会发生变化。

每次浏览器刷新时,会将上次动作重新提交,用重定向可以避开此问题。

HTTP Status 500 - getOutputStream() has already been called for this response

type Exception report

message getOutputStream() has already been called for this response

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.IllegalStateException: getOutputStream() has already been called for this response
    org.apache.catalina.connector.Response.getWriter(Response.java:662)
    org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
    cn.itcast.response.ResponseDemo8.doGet(ResponseDemo8.java:27)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.77 logs.
这是一个常见异常,原因是代码段里同时使用了 response.getOutputStream(); 和 response.getWriter();

[Java][Web]Response学习.缓存