//File: createCookie.jsp
<HTML>
<HEAD>
<TITLE>Setting a Cookie</TITLE>
</HEAD>
<BODY>
<H1>Setting a Cookie</H1>
<%
Cookie cookie1 = new Cookie("message", "Hello!");
cookie1.setMaxAge(24 * 60 * 60);
response.addCookie(cookie1);
%>
<A HREF="readCookie.jsp"/>Read the cookie</A>
</BODY>
</HTML>
//File: readCookie.jsp
<HTML>
<HEAD>
<TITLE>Reading a Cookie</TITLE>
</HEAD>
<BODY>
<H1>Reading a Cookie</H1>
<%
Cookie[] cookies = request.getCookies();
for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) {
if (cookies[loopIndex].getName().equals("message")) {
out.println("The cookie says " + cookies[loopIndex].getValue());
}
}
%>
</BODY>
</HTML>
|