简体   繁体   English

禁用servlet中的浏览器缓存

[英]Disable browser caching in servlet

I know this question has been asked and answered several times before but the solution doesn't seem to work in my WebApp. 我知道这个问题之前曾被问过并回答过几次,但是该解决方案似乎在我的WebApp中不起作用。

This servlet handles a JSP page on which users can (fictively) buy credits. 该Servlet处理一个JSP页面,用户可以在该页面上(虚拟地)购买信用。 In the get-method, the servlet requests the current credits of the user from the database to show them on the JSP page. 在get方法中,servlet从数据库请求用户的当前信用,以在JSP页面上显示它们。 Then the user can choose the amount of credits he wants to buy. 然后,用户可以选择他想购买的信用额度。

The post method checks which value the user has chosen and updates the value in the database for that user. post方法检查用户选择了哪个值,并为该用户更新数据库中的值。

After buying new credits, it looks for the user like his credits are not updated because the browser (Google Chrome) caches an old version of the page. 购买新的抵免额后,由于浏览器(Google Chrome)缓存了该页面的旧版本,因此用户看上去好像抵免额未更新。 Only after a while the user can see his updated credits. 只有一段时间后,用户才能看到他的更新积分。

This is my servlet: 这是我的servlet:

<!-- language: lang-java -->

    package Controller.Servlets;

    import Controller.ListenerHelper;
    import Model.Listener;
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    /**
     *
     * @author Jens
    */
    public class CreditServlet extends HttpServlet {

    private Listener listener;
    private int credits;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        ListenerHelper listenerHelper = new ListenerHelper();

        listener = (Listener) session.getAttribute("Listener");

        credits = listener.getCredits();

        request.setAttribute("credits", credits);

        request.getRequestDispatcher("/listener/credits.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        listener = (Listener) session.getAttribute("Listener");

        String choice = request.getParameter("plan");

        ListenerHelper listenerHelper = new ListenerHelper();

        int creditsToAdd=0;

        switch (choice){
            case "plan_10":
                creditsToAdd=100;
                break;
            case "plan_15":
                creditsToAdd=150;
                break;
            case "plan_25":
                creditsToAdd=250;
                break;
            case "plan_50":
                creditsToAdd=500;
                break;
        }

        try{
            listenerHelper.updateCredits(listener.getUserID(), creditsToAdd, "add");
        } catch(Exception e){
            request.setAttribute("error", e.getMessage());
        }

        httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
        httpResponse.setDateHeader("Expires", 0); // Proxies.

        response.sendRedirect("store.jsp?success=login");

    }
}

At the top of the JSP page I added these lines: 在JSP页面的顶部,我添加了以下几行:

<%
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>

UPDATE Since I can't answer my own question, I'll just update my question. 更新由于我无法回答自己的问题,所以我只更新我的问题。 I found out after some more debugging it's not the browser caching the old result but the database only updating the result after restarting the web app. 我经过更多调试后发现,不是浏览器缓存旧结果,而是数据库仅在重新启动Web应用程序后才更新结果。 I'm sorry to have wasted your time with this. 很抱歉浪费您的时间。 I'm still new to programming. 我还是编程新手。 I'll try to find out why the database acts like this and post my answer. 我将尝试找出为什么数据库会这样,然后发布我的答案。 Thanks for your help. 谢谢你的帮助。

Notice that in the servlet, before redirecting, you are setting this: 请注意,在servlet中,在重定向之前,您需要进行以下设置:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");

Then in the JSP after redirection just: 然后在重定向后的JSP中只是:

response.setHeader("Cache-Control","no-store"); //HTTP 1.1

Why did you back off of using the more powerful header? 为什么不使用更强大的标头呢? What happened to "no-cache, no-store, must-revalidate"? “没有缓存,没有存储,必须重新验证”发生了什么?

What you set prior to redirection just disappears. 您在重定向之前设置的内容只会消失。 Its as if you didn't even do it. 好像您甚至都没有做。 Only what you are setting after the redirection is being sent to the client. 仅将您在重定向后设置的内容发送到客户端。 So you need to put the more insistent header in the JSP like you did in the servlet. 因此,您需要像在servlet中一样将更具持久性的标头放入JSP。

Try with different URL always to make it a new call every time from browser. 始终尝试使用其他URL,以使其每次都从浏览器发出新呼叫。

This trick will force the browser to fetch the new copy of the JSP because the URL has been changed. 该技巧将迫使浏览器获取JSP的新副本,因为URL已更改。

response.sendRedirect("store.jsp?success=login&random_uuid");

You can generate unique id using UUID 您可以使用UUID生成唯一的ID

UUID.randomUUID().toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM