下面的代码主要实现了保存密码,自动登录,用Session保存购买信息的功能。
BookAddedListener.java
package ex2;
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("");
out.println("");
out.println("item list");
out.println(" ");
out.println("Session List:
");
for (int i = 1; i <= itemCount.intValue(); i++) {
out.println((String) session.getAttribute("item" + i) + "
");
}
out.println("");
out.println("");
out.close();
}
}
bookstore.html
Shop
A Book Store
Choose following information:
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
登陆