1、登录界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name="";
//获取所有cookie
Cookie[] ck=request.getCookies();
if(ck!=null)
{
//遍历
for(Cookie co:ck)
{
if(co.getName().equals("name"))
{
name=co.getValue();
}
}
}
%>
请登录购物账号
<form action="Test.jsp" method="post">
账户:<input type="text" name="name" value="<%=name %>">
密码:<input type="password" name="password">
<input type="submit" value="点击登录">
</form>
</body>
</html>
2,验证登录
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
response.setHeader("Cache-Control", "no-cache");
String name= request.getParameter("name");
String password=request.getParameter("password");
if(name == null || password == null || name.equals("") || password.equals(""))
{
out.write("请正确登录!");
response.setHeader("refresh", "2;url=Denglu.jsp");
}
else{
//验证
if(name.equals("1234")&&password.equals("123456"))
{
//用cookie记住账号
//创建cookie
Cookie coo=new Cookie("name",name);
coo.setMaxAge(20*24*3600);
response.addCookie(coo);
//保持登录状态
//创建session
session.setAttribute("name", name);
session.setMaxInactiveInterval(20*60);
response.sendRedirect("Gouwuche.jsp");
}
else
{
out.write("用户名或密码错误");
}
}
%>
</body>
</html>
3.购物界面
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
淘宝首页
<br>
<%
Object obj=session.getAttribute("name");
if(obj==null)
{
out.write("回话超时或未登录");
response.setHeader("refresh", "2;url=Denglu.jsp");
}
else
{
out.write(obj+",欢迎登录");
}
%>
<form action="Daoru.jsp" method="post">
请输入您想要购买的商品名称:<input type="text" name="namesp" /><br/>
<input type="submit" value="加入购物车" /><br/>
</form>
<br>
<a href="logout.jsp">退出</a>
</body>
</html>
4.退出界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//销毁session
session.invalidate();
out.print("退出成功");
response.setHeader("refresh", "2;url=Denglu.jsp");
%>
</body>
</html>
5.创建cookie商品
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String namesp=request.getParameter("namesp");
namesp=new String(namesp.getBytes("ISO-8859-1"),"UTF-8");
//设置cookie
Cookie coo = new Cookie("namesp", URLEncoder.encode(namesp) );//利用cookie记住购物车内的物品
//设置cookie的生命周期为10分钟
coo.setMaxAge(600);
//发送cookie
response.addCookie(coo);
//跳转页面到菜单选择
response.sendRedirect("ShangPin.jsp");
%>
</body>
</html>
6.购物车内商品
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Object obj=session.getAttribute("name");
if(obj!=null)
{
Cookie[] ck=request.getCookies();
if(ck!=null)
{
for(int i =0 ;i<ck.length;i++){
if(ck[i].getName().equals("namesp")){
out.print(ck[i].getName() + "= " + URLDecoder.decode(ck[i].getValue()) + "<br/>");
}
}
}
else
{
out.write("购物车没有商品") ;
}
}
else
{
out.write("未登录");
}
%>
<a href="Gouwuche.jsp">返回进行购物</a>
</body>
</html>