java为什么Chrome会为sessionID返回null?
不久前,我遇到了一个问题,Chrome和Firefox在通过JSF中的servlet创建从小程序到支持bean的连接时更改了sessionid(参见here)。上次我通过在HttpURLConnection上手动设置sessionId来解决这个问题
小程序通过servlet从支持bean请求排名标准对象。然后,用户在applet中定制排名标准(在其他代码中,将排名标准提交回支持bean,以便根据新定制的排名标准对产品进行排名)
现在Chrome将请求sessionId设置为null,但Firefox和Internet Explorer可以正常工作
在小程序中:
try
{
// Get the URL for the servlet.
URL url = new URL(getCodeBase(), "editCriteriaServlet");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/plain");
connection.setRequestProperty("Cookie", "JSESSIONID=" + sessionID);
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject("Request criteria Object");
out.flush();
out.close();
// Read in the search criteria object.
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
SealedObject sealedObject = (SealedObject)in.readObject();
in.close();
// Decrypt the sealed object and get the zipped data.
SecretKey key = buildSecretKey(crypKeyString);
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] baos = (byte[]) sealedObject.getObject(cipher);
ByteArrayInputStream gis = new ByteArrayInputStream(baos);
// Unzip and recover the original object.
GZIPInputStream unzipped = new GZIPInputStream(gis);
ObjectInputStream ois = new ObjectInputStream(unzipped);
tempMultipleSlideDataObject = (MultipleSlideDataObject15) ois.readObject();
}
catch (MalformedURLException ex)
{
errorMessage = "Submit criteria file Malformed URL." + ex.toString();
fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "showErrorMessageDialog_"));
System.out.println("Model_CriteriaInterface: loadCriteriaObject: MalformedURLException occurred");
}
catch (Exception e)
{
errorMessage = "Submit criteria file ERROR exception:" + e.toString();
fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "showErrorMessageDialog_"));
System.out.println("Model_CriteriaInterface: loadCriteriaObject: Submit criteria file ERROR exception: " + e.toString());
}
在servlet中:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("Servlet: SessionID: " + ((HttpServletRequest)request).getRequestedSessionId());
response.setContentType("application/x-java-serialized-object");
try
{
ObjectInputStream in = new ObjectInputStream(request.getInputStream());
in.close();
// Get the backing bean and then use that to get the search criteria object.
ProductSelectionBean productSelection = (ProductSelectionBean)request.getSession().getAttribute("productSelectionBean");
Object searchObject = productSelection.getSealedRankingCriteria();
// Send the object, in the response, back to the applet.
ObjectOutputStream outputToApplet = new ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(searchObject);
outputToApplet.flush();
outputToApplet.close();
}
catch (ClassNotFoundException ex)
{
Logger.getLogger(EditCriteriaServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
servlet中的以下行:
System.out.println("Servlet: SessionID: " + ((HttpServletRequest)req).getRequestedSessionId());
-打印“null”,并且“productSelection”支持bean为null。Firefox和IE可以很好地打印sessionId
我怀疑这是Chrome中的一个bug,但是Chrome没有人回复我。 有什么想法吗?有没有绕过它的建议
编辑更新-我找到了合适的Chrome报告网站(我用谷歌搜索Chrome而不是Chrome)here。看看他们是否能通过这件事联系到我
非常感谢
# 1 楼答案
我不确定这是否解决了Chrome上的问题,但使用BalusC的答案修复:ViewExpiredException exception problem here似乎也解决了这个问题。或者是谷歌的人在最后一天左右修复了它,这只是巧合
由于问题cookie已被清除,我无法重现错误,以检查这是否是Chrome中空指针问题的真正原因。为什么它只会影响Chrome是任何人的猜测