13
我試圖設置cookie內的unicode值,但它不接受這個並拋出異常。我檢查了字符串的十六進制值,它是正確的,但在添加到cookie時拋出異常。java.lang.IllegalArgumentException:Cookie值或屬性中的控制字符
private void fnSetCookieValues(HttpServletRequest request,HttpServletResponse response)
{
Cookie[] cookies=request.getCookies();
for (int i = 0; i < cookies.length; i++) {
System.out.println(""+cookies.length+"Name"+cookies[i].getName());
if(cookies[i].getName().equals("DNString"))
{
System.out.println("Inside if:: "+cookies[i].getValue()+""+cookies.length);
try {
String strValue;
strValue = new String(request.getParameter("txtIIDN").getBytes("8859_1"),"UTF8");
System.out.println("Cookie Value To be stored"+strValue);
for (int j = 0; j < strValue.length(); j++) {
System.out.println("Code Point"+Integer.toHexString(strValue.codePointAt(j)));
}
Cookie ck = new Cookie("DNString",strValue);
response.addCookie(ck);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我得到:
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
添加cookie來響應物體時。我使用Tomcat 7和Java 7作爲運行時環境。
感謝它的作品! – 2012-02-03 05:14:11
不客氣。 – BalusC 2012-02-03 05:14:32