我想從一個servlet發送一個圖像到客戶端,並添加一個包含圖像的id到repsonse的cookie。 (我不想顯示相同的圖像超過N次)。IE和Chrome的Cookie問題(使用java)
看起來Internet Explorer不關心cookies,當我調用request.getCookies();時我總是得到空引用。隨着Opera一切正常。
鉻看到餅乾,但我得到下面的異常時,我的形象寫的OutputStream: ClientAbortException:java.net.SocketException異常:軟件導致連接中止:套接字寫入錯誤
我還沒試過Mozilla的。
Internet Explorer是否有解決方法,除Cookie之外?會話與我的Internet Explorer一起工作。
當我使用Chrome時引發異常的任何想法? (圖像小於1 MB)。
這裏的servlet代碼:
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的價值並將其發回?我將發送cookie值作爲請求參數,但是我需要更改servlet中的值。我怎樣才能讀回它的JavaScript?謝謝。 – 2011-05-25 13:14:47
這是不可能的。你需要在JS中完全管理它。 – BalusC 2011-05-25 13:17:46
謝謝,所以我必須使用ajax請求從servlet獲取數據,然後在javascript中管理cookie。另一個(也許是愚蠢的)問題:我想跟蹤用戶,所以如果用戶進入站點A和站點B,我想從同一個cookie中檢索信息。這是不知何故? – 2011-05-25 14:37:02