login.jsp
<%@ 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>首页</title>
</head>
<body>
<jsp:include page="insert.jsp"></jsp:include>
<form action="" method=post>
username:<input type="text" name="un">
<br><br>
paasword:<input type="password" name="ps"><br><br>
<input type="submit" value="登录"><br><br>
</form>
<%
String name=request.getParameter("un");
String ps=request.getParameter("ps");
if(session.getAttribute("name")!=null)
{
out.println("你已经登录过了,不用重复登录");
}
else{
if(name!=null&&ps!=null)
{
session.setAttribute("name", name);
if(name.equals("admin")&&ps.equals("123123"))
{
out.println("登陆成功");
}
else {
out.println("用户账号或密码错误");
}
}
else{
out.println("请登录");
}
}
%>
</body>
</html>
book.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<!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>书</title>
</head>
<body>
<jsp:include page="insert.jsp"></jsp:include>
<form method="post">
<input type="checkbox" name="check" value="Java程序设计#1">Java程序设计¥1<br><br>
<input type="checkbox" name="check" value="数据库原理#2">数据库原理¥2<br><br>
<input type="checkbox" name="check" value="操作系统#3">操作系统¥3<br><br>
<input type="checkbox" name="check" value="数据结构#4">数据结构¥4<br><br>
<input type="checkbox" name="check" value="web开发#5">web开发¥5<br><br>
<input type="submit" name="submit1" value="添加到购物车">
</form>
<%
List book=(List)session.getAttribute("book");
//out.println(book);
request.setCharacterEncoding("UTF-8");
String[] s=request.getParameterValues("check");
if(book==null)
{
book=new ArrayList();
}
if(s!=null)
{
for(int i=0;i<s.length;i++)
book.add(s[i]);
session.setAttribute("book", book);
}
%>
</body>
</html>
checkOut.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<!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>
<jsp:include page="insert.jsp"></jsp:include>
<%
if(session.getAttribute("name")==null)
{
out.println("该页面为授权页面,请先登录");
}
else{
List b=(List)session.getAttribute("book");
int sum=0;//存放总金额
if(b!=null){
out.println("您购买了:"+"<br>");
for(int i=0;i<b.size();i++)
{
String b1=(String)b.get(i);
String[] b2=b1.split("#");
out.println(b2[0]+"¥"+b2[1]+"<br>");
sum+=Integer.parseInt(b2[1]);
}
out.println("总金额为"+sum+"<br>");
}
}
%>
</body>
</html>
insert.jsp
<%@ 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>插入</title>
</head>
<body>
<a href="login.jsp">登录</a>
<a href="book.jsp">选择图书</a>
<a href="checkOut.jsp">结账</a>
<a href="exit.jsp">退出</a>
<hr>
</body>
</html>
exit.jsp
<%@ 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>退出</title>
</head>
<body>
<jsp:include page="insert.jsp"></jsp:include>
<%
session.invalidate();
out.println("账号退出成功");
%>
<a href="login.jsp">登录</a>
</body>
</html>
实验效果图