IE和Chrome的Cookie问题(使用java) [英] Cookie problems with IE and Chrome (working with java)
问题描述
我正在尝试从一个servlet发送一个图像到客户端,并添加一个包含图像的id的cookie到repsonse。 (我不想显示相同的图像超过N次)。
I'm trying to send an image to the client from a servlet, and add a cookie containing the id of the image to the repsonse. ( i don't want to display the same image more than N times).
看起来像Internet Explorer不关心cookie,我总是得到一个null引用当我调用request.getCookies();.
Looks like Internet Explorer doesn't care about the cookies and i always get a null reference when i call request.getCookies();. With Opera everything works great.
Chrome看到cookie,但是当我将图像写入outputStream时会出现以下异常:
ClientAbortException:java.net。 SocketException:软件导致连接中止:socket写错误
Chrome sees the cookies but i get the following exception when i write the image to the outputStream : ClientAbortException: java.net.SocketException: Software caused connection abort: socket write error
我还没有尝试Mozilla。
I haven't tried yet Mozilla.
Internet Explorer有解决方法(Cookie除外)吗?会话与我的Internet Explorer配合使用。
Is there a workaround for Internet Explorer, except cookies? Sessions work with my Internet Explorer.
我使用Chrome时出现的异常提示? (图片小于1 MB)。
Any ideas for the exception raised when i use Chrome ? ( the image is less than 1 MB).
以下是servlet代码:
Here's the servlet code:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
response.addHeader ("Content-Disposition", "attachment");
response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
HttpSession session = request.getSession();
String requestURI = request.getParameter("requestURI");
String resolution = request.getParameter("resolution");
Cookie[] cookies = request.getCookies();
try
{
if (cookies == null)
coada = (new BannerChooser().Choose(1));
String filePath = null;
Iterator it = coada.iterator();
boolean found =false;
while ((!found) && it.hasNext())
{
found = true;
if (cookies!=null)
for (int i = 0; i < cookies.length; i++)
if ( Integer.parseInt(cookies[i].getValue()) == ((BannerNota)it.next()).getB().getId())
{
found = false;
break;
}
if (found)
{
BannerNota bannerToDisplay = (BannerNota)it.next();
Cookie cookie = new Cookie(bannerToDisplay.getB().getId().toString(),bannerToDisplay.getB().getId().toString());
cookie.setMaxAge(60*60*24);
cookie.setPath("/licenta");
filePath = bannerToDisplay.getB().getPath();
response.addCookie(cookie);
break;
}
}
filePath = "h:/program files/Workspace/licenta/WebRoot/" + filePath;
File f = new File(filePath);
byte[] b = new byte[(int)f.length()];
FileInputStream fis = new FileInputStream(f);
fis.read(b);
ServletOutputStream out = response.getOutputStream();
out.write(b);
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
推荐答案
Cookie是基于网域的。许多浏览器会拒绝从网页以外的网域提供的嵌入资源(CSS / JS /图片)Cookie。
Cookies are domain based. Many browsers reject cookies on embedded resources (CSS/JS/images) which are served from other domain than the page is been served from.
您要管理Cookie JavaScript代替。 Google Analytics(分析)也是这样做的。在quirksmode.org,您可以找到一个很好的教程如何使用JavaScript管理Cookie 。任何基于Cookie的信息都可以作为图片网址上的请求参数发送。
You want to manage the cookie using JavaScript instead. Google Analytics is also doing it that way. At quirksmode.org you can find a nice tutorial how to manage cookies using JavaScript. Any cookie-based information can then be sent as a request parameter on the image URL.
这篇关于IE和Chrome的Cookie问题(使用java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!