java请求头cookie里放值,在头刷新后,在Java中的响应中添加一个cookie?

I have a custom tag that does some processing and then sets a cookie. However, the cookie wasn't being set, and I couldn't figure out why. Another developer pointed out that because we're using a template system, the point at which the tag evaluates, the response header has already been flushed as a part of an include. Since the header has been sent, it doesn't look possible to add the cookie (however, no state exceptions are thrown when I try to do it). Is there a way around this? Does this sound like that might be the problem?

解决方案

Like other folks have said, you really want to avoid having to do this in the first place. You'd like maybe a nice MVC kind of model where you have some servlets that do your heavy work like database and cookies and all that, and then pass control off to a JSP that actually renders the response. Since the JSP isn't called to generate HTML until after your servlets are done, you shouldn't run into grief like this.

It is kind of lame that setCookie doesn't tell you when it fails, but it also makes sense that probably you don't want it to break your entire page. ServletResponse.isCommitted() will tell you if the headers are already written, and consequently if your setCookie() call will fail.

If you are in dire straits and absolutely need to be able to do this (while you look for a better solution), you could create a servlet filter to buffer the response in memory until after your cookie is set. The pattern would be something like this:

doFilter(request, response, chain)

{

BufferedResponse bufferedResponse = new BufferedResponse(response);

try

{

// pass control to the next filter or to the JSP/servlet servicing the request

chain.doFilter(request, bufferedResponse);

}

finally

{

bufferedResponse.flush();

}

}

BufferedResponse would need to implement HttpServletResponse and basically keep everything in memory until you explicitly flush it. At that point it would write out the headers, cookies, and the buffered response body.

This will totally work, but it will cause your web server to use a bunch of extra memory since it has to buffer the entire response body in memory for every request. Also, your pages will load slower since the server can't start sending anything to the client browser until after your page is completely done. Bad juju my friend.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 实现页面访问量统计可以使用 Servlet 技术,具体实现步骤如下: 1.创建一个 Servlet 类,实现 doGet() 方法来处理 GET 请求。 2.在 doGet() 方法,创建一个计数器变量,用来记录页面访问量。 3.通过 Cookie 技术来避免用户刷新页面导致访问量重复计算的问题。如果用户没有访问过该页面,则创建一个新的 Cookie,并将计数器变量赋为 1。如果用户已经访问过该页面,则读取该用户的 Cookie 的计数器变量,并将其加 1。 4.将计数器变量的输出到页面上,用来显示当前页面的访问量。 下面是一个示例代码: ```java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class PageVisitCounter extends HttpServlet { private int counter = 0; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 从请求获取用户的 Cookie Cookie[] cookies = request.getCookies(); Cookie counterCookie = getCookie(cookies, "counter"); if (counterCookie == null) { // 如果用户没有访问过该页面,则创建一个新的 Cookie counterCookie = new Cookie("counter", "1"); counter = 1; } else { // 如果用户已经访问过该页面,则读取该用户的 Cookie 的计数器变量,并将其加 1 counter = Integer.parseInt(counterCookie.getValue()); counter++; counterCookie.setValue(String.valueOf(counter)); } // 将 Cookie 添加响应 response.addCookie(counterCookie); // 输出当前页面的访问量 response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<h2>Page visit counter</h2>"); out.println("<p>Current page visits: " + counter + "</p>"); out.println("</body></html>"); } private Cookie getCookie(Cookie[] cookies, String name) { if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals(name)) { return cookie; } } } return null; } } ``` 在 web.xml 文件配置 Servlet 映射: ```xml <servlet> <servlet-name>PageVisitCounter</servlet-name> <servlet-class>PageVisitCounter</servlet-class> </servlet> <servlet-mapping> <servlet-name>PageVisitCounter</servlet-name> <url-pattern>/pageVisitCounter</url-pattern> </servlet-mapping> ``` 这样,当用户访问 /pageVisitCounter 页面时,就会触发 PageVisitCounter Servlet 的 doGet() 方法,从而实现页面访问量的统计。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值