servlet代码:
try {
String cookMessage = request.getParameter("cookieMessage");
Cookie cookDemo = new Cookie("cookDemo", cookMessage);
cookDemo.setMaxAge(60);
response.addCookie(cookDemo);
} catch (Exception e) {
// TODO: handle exception
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/cookieShow.jsp");
dispatcher .forward(request, response);
输出页面代码:
<%
Cookie[] cookies = request.getCookies();
if(cookies == null)
{
out.print("none any cookies");
}else{
try{
for(int i=0;i
String cookValue = cookies[i].getValue();
out.print("@@"+cookValue+"@@");
}
}catch(Exception e)
{
}
}
%>
页面输出结果:
9F1749627BA07B57E55A6E925ACBB923