[入门]简单的cookies操作
index.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<%
String username = null;
Cookie[] cookies = request.getCookies();
if(cookies!=null)
{
for(int i=0;i<cookies.length;i++)
{
if("cookies_user".equals(cookies[i].getName()))
{
username = cookies[i].getValue();//cookies_user}
}
if("onepc".equals(username))
{
out.println("Hello");
}else
{
%>
<table width="302" border="1">
<form id="form1" name="form1" method="post" action="clogin.jsp">
<tr>
<td width="79"><div align="center"></div></td>
<td width="207"><input type="text" name="user" id="user" /></td>
</tr>
<tr>
<td><div align="center"></div></td>
<td><input type="text" name="textfield2" id="textfield2" /></td>
</tr>
<tr>
<td><div align="center">
</div></td>
<td><select name="select" id="select">
<option value="31536000">one year</option>
<option value="120">two min</option>
</select></td>
</tr>
<tr>
<td colspan="2"><label>
<input type="submit" name="button" id="button" value="" />
</label></td>
</tr>
</form>
</table>
<%
}
}
%>
</body>
</html>
login.jsp
<%
String user = request.getParameter("user");
Cookie cookie = new Cookie("cookies_user",user);
cookie.setMaxAge(120);
response.addCookie(cookie);
response.sendRedirect("cindex.jsp");
%>