Java/JSP/Cookie
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
Cookie display in a JSP page
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
<head><title>Cookie display</title></head>
<body>
<h2>Here are all the Available Cookies</h2>
<c:forEach var="cookies" items="${cookie}">
<strong><c:out value=
"${cookies.key}"/></strong>: Object=<c:out value="${cookies.value}"/>, value=<c:out value="${cookies.value.value}"/><br />
</c:forEach>
</body>
</html>
Deal with the cookie
// cookieReader.jsp
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
<body>
<c:choose>
<c:when test="${empty cookie}" >
<h2>We did not find any cookies in the request</h2>
</c:when>
<c:otherwise>
<h2>The name and value of each found cookie</h2>
<c:forEach var="cookieVal" items="${cookie}">
<strong>Cookie name:</strong> <c:out value="${cookieVal.key}" /><br>
<strong>Cookie value:</strong> <c:out value="${cookieVal.value.value}" /><br><br>
</c:forEach>
</c:otherwise>
</c:choose>
</body>
</html>
// cookieSetter.jsp
<jsp:useBean id="cookieBean" class="com.jexp.CookieBean" />
<jsp:setProperty name="cookieBean" property="name" value="bakedcookie" />
<jsp:setProperty name="cookieBean" property="maxAge" value="<%=(365*24*60*60) %>" />
<jsp:setProperty name="cookieBean" property="path" value="<%= request.getContextPath() %>" />
<jsp:setProperty name="cookieBean" property="cookieHeader" value="<%= response %>" />
<html>
<head><title>Cookie Maker</title></head>
<body>
<h2>Here is information about the new cookie</h2>
Name: <jsp:getProperty name="cookieBean" property="name" /><br>
Value: <jsp:getProperty name="cookieBean" property="value" /><br>
Path: <jsp:getProperty name="cookieBean" property="path" />
</body>
</html>
// put the class file to WEB-INF/classes/com/jexp
//CookieBean.java
package com.jexp;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
public class CookieBean {
private Cookie cookie = null;
public CookieBean() {
}
public void setName(String name) {
if (name == null || (name.equals("")))
throw new IllegalArgumentException("Invalid cookie name set in: "
+ getClass().getName());
cookie = new Cookie(name, "" + new java.util.Date().getTime());
}
public void setValue(String value) {
if (value == null || (value.equals("")))
throw new IllegalArgumentException("Invalid cookie value set in: "
+ getClass().getName());
if (cookie != null)
cookie.setValue(value);
}
public void setMaxAge(int maxAge) {
if (cookie != null)
cookie.setMaxAge(maxAge);
}
public void setPath(String path) {
if (path == null || (path.equals("")))
throw new IllegalArgumentException("Invalid cookie path set in: "
+ getClass().getName());
if (cookie != null)
cookie.setPath(path);
}
public void setCookieHeader(HttpServletResponse response) {
if (response == null)
throw new IllegalArgumentException(
"Invalid HttpServletResponse set in: "
+ getClass().getName());
if (cookie != null)
response.addCookie(cookie);
}
public String getName() {
if (cookie != null)
return cookie.getName();
else
return "unavailable";
}
public String getValue() {
if (cookie != null)
return cookie.getValue();
else
return "unavailable";
}
public String getPath() {
if (cookie != null)
return cookie.getPath();
else
return "unavailable";
}
}
JSP Create and List Cookie
<%@ page import="java.util.*" %>
<%
Cookie[] cookies = request.getCookies();
if (cookies == null)
cookies = new Cookie[0];
Date thisVisit = new Date();
// add data cookie to session
Cookie c = new Cookie("lastVisit",""+thisVisit);
response.addCookie(c);
// add cookie to session
String name = request.getParameter("name");
String value = request.getParameter("value");
Cookie added = null;
if (name!=null && value!=null && name.length()>0) {
added = new Cookie(name,value);
response.addCookie(added);
}
%>
<HTML>
<HEAD>
<TITLE>Cookie List</TITLE>
</HEAD>
<BODY>
<H1>Cookie List</H1>
This visit: <%= thisVisit %><BR>
Number of cookies: <%= cookies.length %><BR>
<H2>Cookies</H2>
<%
for (int i=0; i<cookies.length; i++) {
out.println(cookies[i].getName()+":\t"+
cookies[i].getValue()+"<BR>");
// check if added cookie already present
if (added!=null && added.getName().equals(cookies[i].getName()))
added = null;
}
if (added != null)
out.println("new cookie: "+added.getName()+":\t"+
added.getValue()+"<BR>");
%>
<H2>New cookie</H2>
<FORM>
<P>Name: <INPUT TYPE="TEXT" NAME="name"></P>
<P>Value: <INPUT TYPE="TEXT" NAME="value"></P>
<INPUT TYPE="SUBMIT" VALUE="Add new value">
</FORM>
</BODY>
</HTML>
JSP: deal with cookie
<%@ page import="javax.servlet.http.Cookie" %>
<html>
<head>
<title>This page leaves a cookie</title>
</head>
<body>
<h1>Cookies</h1>
<%
Cookie[] allCookies = request.getCookies();
Cookie ourCookie = null;
if (allCookies!=null)
{
for (int i=0; i<allCookies.length; i++)
{
if (allCookies[i].getName().equals("TestCookie"))
{
ourCookie = allCookies[i];
}
}
}
if (ourCookie == null)
{
Cookie cookie = new Cookie("TestCookie", "hello from cookie");
//cookie.setMaxAge(1800);
//cookie.setDomain("alex:8080");
cookie.setPath("/");
response.addCookie(cookie);
%>
A cookie has been added to your machine!
<br>Select refresh to see the details of this cookie.
<%
}
else
{
%>
The following cookie was added earlier to your machine:
<br>Version: <%=ourCookie.getVersion() %>
<br>Name: <%=ourCookie.getName() %>
<br>Value: <%=ourCookie.getValue() %>
<br>MaxAge: <%=ourCookie.getMaxAge() %>
<%
}
%>
</body>
</html>
JSP List All Cookie
<%@ page import="java.util.*" %>
<%
Cookie[] cookies = request.getCookies();
if (cookies == null)
cookies = new Cookie[0];
Date thisVisit = new Date();
// add data cookie to session
Cookie c = new Cookie("lastVisit",""+thisVisit);
response.addCookie(c);
// add cookie to session
String name = request.getParameter("name");
String value = request.getParameter("value");
Cookie added = null;
if (name!=null && value!=null && name.length()>0) {
added = new Cookie(name,value);
response.addCookie(added);
}
%>
<HTML>
<HEAD>
<TITLE>Cookie List</TITLE>
</HEAD>
<BODY>
<H1>Cookie List</H1>
This visit: <%= thisVisit %><BR>
Number of cookies: <%= cookies.length %><BR>
<H2>Cookies</H2>
<%
for (int i=0; i<cookies.length; i++) {
out.println(cookies[i].getName()+":\t"+
cookies[i].getValue()+"<BR>");
// check if added cookie already present
if (added!=null && added.getName().equals(cookies[i].getName()))
added = null;
}
if (added != null)
out.println("new cookie: "+added.getName()+":\t"+
added.getValue()+"<BR>");
%>
<H2>New cookie</H2>
<FORM>
<P>Name: <INPUT TYPE="TEXT" NAME="name"></P>
<P>Value: <INPUT TYPE="TEXT" NAME="value"></P>
<INPUT TYPE="SUBMIT" VALUE="Add new value">
</FORM>
</BODY>
</HTML>
Setting a Cookie
//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);
%>
== Setting and Reading Cookies ==
<!-- start source code -->
<source lang="java">
<HTML>
<HEAD>
<TITLE>Setting and Reading Cookies</TITLE>
</HEAD>
<BODY
<%
Cookie[] cookies = request.getCookies();
boolean foundCookie = false;
for(int i = 0; i < cookies.length; i++) {
Cookie c = cookies[i];
if (c.getName().equals("color")) {
out.println("bgcolor = " + c.getValue());
foundCookie = true;
}
}
if (!foundCookie) {
Cookie c = new Cookie("color", "cyan");
c.setMaxAge(24*60*60);
response.addCookie(c);
}
%>
>
<H1>Setting and Reading Cookies</H1>
This page will set its background color using a cookie.
</BODY>
</HTML>