使用JSP或Javascript从浏览器缓存中清除图像 [英] Clear image from browser cache in JSP or Javascript

查看:81
本文介绍了使用JSP或Javascript从浏览器缓存中清除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为大学开发JSP Web应用程序,并且在用户页面上显示了人事图片.

I'm developing a JSP Web application for a university, and there is a personnel picture displayed in user page.

用户注销后,如何从网络浏览器缓存中清除这张图片?

How can i clear this picture from web-browser cache once the user logged out ?

使用JSP或javascript

using JSP or javascript

推荐答案

这是不可能的.最好的选择是完全禁用有关资源的缓存.创建一个过滤器,该过滤器将在doFilter()方法中完成以下工作.

That's not possible. Your best bet is to just entirely disable the caching of the resource in question. Create a filter which does the following job in the doFilter() method.

HttpServletResponse hsr = (HttpServletResponse) response;
hsr.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
hsr.setHeader("Pragma", "no-cache");
hsr.setDateHeader("Expires", 0);
chain.doFilter(request, response);

并将其映射到覆盖感兴趣图像的URL模式上.

and map it on an URL pattern covering the image(s) of interest.

编辑:如果您实际上不关心浏览器缓存中是否存在图片,但是您的具体问题是,不同的登录用户基本上会使用相同的图片网址,因为它是由某些servlet动态提供的,那么您也可以通过为每个唯一的用户提供一个唯一的图片网址来解决它.您可以通过将用户ID作为请求参数附加到图像URL,或将其包括在图像的路径中来实现此目的.

if you actually don't care about the image being present in the browser cache, but your concrete problem is that different logged-in users basically use the same image URL because it's been served dynamically by some servlet, then you can also solve it by giving every unique user an unique image URL. You can do this by appending for example the user ID as request parameter to the image URL, or including it in the image's path.

<img src="profileimage?id=${user.id}" />

<img src="profileimage/${user.id}" />

这篇关于使用JSP或Javascript从浏览器缓存中清除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆