【java web】用jsp实现简单的购物车程序
【代码】【java web】用jsp实现简单的购物车程序[实现大部分功能,暂时还有界面翻译未完成]
·
这里写自定义目录标题
index.jsp 首页
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<center>
简易购物车演示程序
<a href = "zuce.jsp" >注册</a>
<a href = "login.jsp">登录</a>
</center>
</body>
</html>
zuce.jsp 注册界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册界面</title>
</head>
<body>
<form>
姓名:<input type = "text" name = "n"/><br>
密码:<input type = "password" name = "p"/><br>
性别:
<input type = "radio" name = "g" value = "男"/>男
<input type = "radio" name = "g" value = "女"/>女<br>
爱好:
<input type = "checkbox" name = "l" value = "音乐"/>音乐
<input type = "checkbox" name = "l" value = "运动"/>运动
<input type = "checkbox" name = "l" value = "电影"/>电影
<input type = "checkbox" name = "l" value = "编程"/>编程<br>
班级:
<select name = "c">
<option value = "2006班">2006班</option>
<option value = "2007班">2007班</option>
</select><br>
个人说明:<br>
<textarea name = "i" rows = "4" cols = "100">请填写个人说明</textarea><br>
<input type = "submit" value = "提交" >
<input type = "reset">
</form>
</body>
</html>
login.jsp 登录界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%--在上述的基础上接收最开始的index.jsp界面输入的用户名或者密码 --%>
<% String e_1=(String)request.getAttribute("error_1"); %>
<% String pass=(String)request.getAttribute("p_1"); %>
<% String e_2=(String)request.getAttribute("error_2"); %>
<% String name=(String)request.getAttribute("n_1"); %>
<% String e=(String)request.getAttribute("e"); %>
<%-- 修改用户名信息 --%>
<%if (session.getAttribute("username")!=null) session.removeAttribute("username"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action = "login_OK.jsp">
<%--判断是否为空,如果不为空则表示login_OK接收到最开始的index.jsp的数据又传递回来 --%>
用户名:<input type = "text" name = "n" <% if (name!=null)%>value = "<%=name%>"/>
密码:<input type = "password" name = "p" <% if (pass!=null)%>value = "<%=pass%>"/>
<input type = "submit" value = "提交">
<input type = "reset">
</form>
<%
if(e_1 != null){
out.print("<h3> <font color = red>"+e_1+"</font></h3>");
}
if(e_2 != null){
out.print("<h3> <font color = red>"+e_2+"</font></h3>");
}
if(e != null){
out.print("<h3> <font color = red>"+e+"</font></h3>");
}
%>
</body>
</html>
index_OK.jsp 登录成功界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- 从b或者c中接受数据 --%>
<% if (session.getAttribute("username")!=null && session.getAttribute("password")!= null){ %>
用户名:<%=session.getAttribute("username")%><br>
密码:<%=session.getAttribute("password")%><br>
<center>恭喜你!<%=session.getAttribute("username")%> 登录成功<br>
<a href = "l1.jsp">访问栏目1</a>
<a href = "l2.jsp">访问栏目2</a>
<a href = "login.jsp">退出</a>
</center>
<% }else{ %>
<%-- 从index接受到的数据 --%>
<%
String n = request.getParameter("n");
String p = request.getParameter("p");
String n_r = "Bessie";
String p_r = "123";
%>
<%-- 判断是否为空,将提示信息和原始数据一并传给index.jsp中--%>
<% if(n=="" || n == null){
request.setAttribute("error_1","用户名未输入");
request.setAttribute("p_1",p);
request.getRequestDispatcher("login.jsp").forward(request, response);
}%>
<%--同理即可理解 --%>
<% if(p=="" || p == null){
request.setAttribute("error_2","密码未输入");
request.setAttribute("n_1",n);
request.getRequestDispatcher("login.jsp").forward(request, response);
}%>
<%--密码用户名判断是否正确 --%>
<%if(!(n.equals(n_r) && p.equals(p_r))) {
String w = "用户名或者密码错误";
request.setAttribute("e",w);
request.getRequestDispatcher("login.jsp").forward(request, response);
}else {%>
用户名:<%=request.getParameter("n")%><br>
<% session.setAttribute("username",n); %>
密码:<%=request.getParameter("p")%><br>
<% session.setAttribute("password",p); %>
<center>恭喜你!<%=n%> 登录成功<br>
<a href = "l1.jsp">访问栏目1</a>
<a href = "l2.jsp">访问栏目2</a>
<a href = "login.jsp">退出</a>
</center>
<%}%>
<% }%>
<%-- 页面跳转 --%>
</body>
</html>
l1.jsp 商品1【自提交】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%
String name = (String)session.getAttribute("username");
String pass = (String)session.getAttribute("password");
ArrayList goods = (ArrayList)session.getAttribute("goods");
String[] selected = request.getParameterValues("book");
if(goods == null){
goods = new ArrayList();
}
if(selected != null){
for(int i = 0; i < selected.length; i++){
goods.add(selected[i]);
}
}
session.setAttribute("goods",goods);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>栏目1</title>
</head>
<body>
你好!<%=name %><br>
这是栏目1-图书页面<br>
<form action = "">
<input type = "checkbox" name = "book" value = "b_1"/>数据库
<input type = "checkbox" name = "book" value = "b_2"/>机器学习
<input type = "checkbox" name = "book" value = "b_3"/>深度学习
<input type = "checkbox" name = "book" value = "b_4"/>web开发<br>
<input type = "submit" value = "提交" >
<input type = "reset" value = "重置">
</form>
<%if(goods != null){ %>
<font color = "red">购物车里有:<%=goods.size() %>件商品</font>
<a href = "shopping.jsp">点击查看购物车</a>
<%} %>
<a href = "l2.jsp">前往栏目2</a>
<a href = "login_OK.jsp">回到购物界面</a>
</body>
</html>
l2.jsp 商品2【自提交】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%
String name = (String)session.getAttribute("username");
String pass = (String)session.getAttribute("password");
ArrayList goods = (ArrayList)session.getAttribute("goods");
String[] selected = request.getParameterValues("food");
if(goods == null){
goods = new ArrayList();
}
if(selected != null){
for(int i = 0; i < selected.length; i++){
goods.add(selected[i]);
}
}
session.setAttribute("goods",goods);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>栏目2</title>
</head>
<body>
你好!<%=name %><br>
这是栏目2-食品页面<br>
<form action = "">
<input type = "checkbox" name = "food" value = "f_1"/>面包
<input type = "checkbox" name = "food" value = "f_2"/>牛奶
<input type = "checkbox" name = "food" value = "f_3"/>豆浆
<input type = "checkbox" name = "food" value = "f_4"/>热干面<br>
<input type = "submit" value = "提交" >
<input type = "reset" value = "重置">
</form>
<%if(goods != null){ %>
<font color = "red">购物车里有:<%=goods.size() %>件商品</font>
<a href = "shopping.jsp">点击查看购物车</a>
<%} %>
<a href = "l1.jsp">前往栏目1</a>
<a href = "login_OK.jsp">回到购物界面</a>
</body>
</html>
shopping.jsp 购物车界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%
String name = (String)session.getAttribute("username");
String pass = (String)session.getAttribute("password");
ArrayList goods = (ArrayList)session.getAttribute("goods");
int b_1 = 0,b_2 = 0,b_3 = 0,b_4 = 0;
int f_1 = 0,f_2 = 0,f_3 = 0,f_4 = 0;
for(int i = 0; i < goods.size();i++){
//if (goods.get(i) == "f_2") System.out.print("1111111111");
if (goods.get(i).equals("b_1")) b_1++;
if (goods.get(i).equals("b_2")) b_2++;
if (goods.get(i).equals("b_3")) b_3++;
if (goods.get(i).equals("b_4")) b_4++;
if (goods.get(i).equals("f_1")) f_1++;
if (goods.get(i).equals("f_2")) f_2++;
if (goods.get(i).equals("f_3")) f_3++;
if (goods.get(i).equals("f_4")) f_4++;
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车界面</title>
</head>
<body>
<h3>数据库<%=b_1 %></h3><br>
<h3>机器学习<%=b_2 %></h3><br>
<h3>深度学习<%=b_3 %></h3><br>
<h3>web开发<%=b_4 %></h3><br>
<h3>面包<%=f_1 %></h3><br>
<h3>牛奶<%=f_2 %></h3><br>
<h3>豆浆<%=f_3 %></h3><br>
<h3>热干面<%=f_4 %></h3><br>
<a href = "l1.jsp">前往栏目1</a>
<a href = "l2.jsp">前往栏目2</a>
<a href = "login_OK.jsp">回到购物界面</a>
</body>
</html>
更多推荐
已为社区贡献6条内容
所有评论(0)