您的位置:首页 > 编程语言 > Java开发

javaweb简单模拟购物车

2016-10-22 17:21 253 查看
<span style="font-family:Arial, Helvetica, sans-serif;">下面的代码主要实现了保存密码,自动登录,用Session保存购买信息的功能。</span>
<span style="font-family: Arial, Helvetica, sans-serif;">BookAddedListener.java</span>

<span style="font-family: Arial, Helvetica, sans-serif;">package ex2;</span>
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
* @author 高尉峰
*
*/
public class BookAddedListener implements HttpSessionAttributeListener{

@Override
public void attributeAdded(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
System.out.println(event.getName()+":"+event.getValue());
}

@Override
public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
System.out.println(event.getName()+":"+event.getValue());
}

}
CheckFilter.java
package ex2;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckFilter implements Filter {

/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
Cookie[] cookies = request.getCookies();
String name = "";
String passwd = "";
// 遍历cookie
if (cookies != null) {
for (Cookie coo : cookies) {
if (coo.getName().equals("loginInfo")) {
String[] info = coo.getValue().split(",");
name = info[0];
passwd = info[1];
}
}
}
// 如果在cookie中得到保存的用户名和密码,验证正确后,直接转到主页
if (new LoginBiz().isValid(name, passwd)) {
response.sendRedirect(request.getContextPath() + "/bookstore.html");
} else {
arg2.doFilter(request, response);
}
}

public void init(FilterConfig arg0) throws ServletException {

}

public void destroy() {

}

}
LoginAction.java
package ex2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author 高尉峰
*
*/
public class LoginAction extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求参数
String name=request.getParameter("name");
String passwd=request.getParameter("passwd");
String savetimeString=request.getParameter("savetime");

if(new LoginBiz().isValid(name, passwd)){
int savetime=Integer.parseInt(savetimeString)*24*3600;
//创建cookie对象
Cookie loginInfo=new Cookie("loginInfo",name+","+passwd);
//设置cookie最大存活时间
loginInfo.setMaxAge(savetime);
//服务端向客户端响应cookie
response.addCookie(loginInfo);
//登陆成功转向主页
response.sendRedirect(request.getContextPath() +"/bookstore.html");
}else{
response.sendRedirect(request.getContextPath() +"/login.jsp");
}
}

} LoginBiz.java
package ex2;
/**
* @author高尉峰
*javaBean
*/
public class LoginBiz {
static boolean isValid(String name,String passwd){
if(name.equals("1")&&passwd.equals("1")){
return true;
}else{
return false;
}
}
} ShoppingCart.java
package ex2;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ShoppingCart extends HttpServlet {
/*
* (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
* , javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
/*
* (non-Javadoc) 取得Session对象如果Session不存在,为本次会话创建此对象
*/
HttpSession session = req.getSession(true);
Integer itemCount = (Integer) session.getAttribute("itemCount");
// 如果session是新的
if (itemCount == null)
itemCount = new Integer(0);

PrintWriter out = res.getWriter();
res.setContentType("text/html");

// 接收传来的参数
String[] itemsSelected;
String itemName;
itemsSelected = req.getParameterValues("item");

if (itemsSelected != null) {
for (int i = 0; i < itemsSelected.length; i++) {
itemName = itemsSelected[i];
System.out.println(itemName);
itemCount = new Integer(itemCount.intValue() + 1);
// 购买的条目
session.setAttribute("item" + itemCount, itemName);
// 总条目
session.setAttribute("itemCount", itemCount);
}
}

out.println("<html>");
out.println("<title>");
out.println("item list");
out.println("</title>");
out.println("<body><h4>Session List:</h4><hr><br><br>");
for (int i = 1; i <= itemCount.intValue(); i++) {
out.println((String) session.getAttribute("item" + i) + "<hr>");
}
out.println("</body>");
out.println("</html>");
out.close();
}
}bookstore.html
<html>
<head>
<title>Shop</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF">
<h1 align="center">A Book Store</h1>
<hr>
<p><b>Choose following information:</b></p>
<form method="post" action="ShoppingCart.do">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="69">
<input type="checkbox" name="item" value="Thinking in Java">
</td>
<td width="431">Item1: Thinking in Java</td>
</tr>
<tr>
<td width="69">
<input type="checkbox" name="item" value="Head first servlets and JSP">
</td>
<td width="431">Item2: Head first servlets and JSP</td>
</tr>
<tr>
<td width="69">
<input type="checkbox" name="item" value="Enterprise JavaBeans 3.0">
</td>
<td width="431">Item3: Enterprise JavaBeans 3.0</td>
</tr>
</table>
<hr>
<p>
<input type="submit" name="btn_submit" value="Buy Now">
</p>
</form>
<p> </p>
</body>
</html>login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>登陆</title>
<link rel="stylesheet" href="css/login.css">
<script type="text/javascript">
function checkForm(form) {
if (form.name.value == "") {
alert("用户名不能为空!");
form.name.focus();
return false;
}
if (form.passwd.value == "") {
alert("密码不能为空!");
form.passwd.focus();
return false;
}
return true;
}
</script>
</head>
<body>

<form id="form" action="<%=request.getContextPath()%>/LoginAction"
method="post" onsubmit="checkForm(this);">
<div class="content">
<!-- 登录面板 -->
<div class="panel">

<!-- 账号和密码组 -->
<div class="group">
<label>账号</label> <input placeholder="请输入账号" type="text"
name="name">

</div>
<div class="group">
<label>密码</label> <input placeholder="请输入密码" type="password"
name="passwd">
</div>
<div class="group">
<label>保存时间</label> <select name="savetime">
<option value="7">一周</option>
<option value="30">一个月</option>
</select>
</div>
<!-- 登录按钮 -->
<div class="login">
<button type="submit">登录</button>
</div>
</div>

</div>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息