这次采用MVC的模式来写这个功能
项目目录
Servlet层代码(LoginServlet.java)
package com.qianfeng.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qianfeng.javaBean.User;
import com.qianfeng.service.UserService;
import com.qianfeng.serviceImpl.UserServiceImpl;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
//获取姓名,密码,以及复选框的状态
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String auto = request.getParameter("box");
//通过密码和用户名查询数据库
UserService bService = new UserServiceImpl();
User user = bService.findUserByNameAndPwd(name,pwd);
//user不为空,说明数据库中有该数据,登录成功
if(user != null){
//编码--解决乱码
String ss = URLEncoder.encode(name+"&"+pwd, "utf-8");
//获取Cookie对象
Cookie cookie = new Cookie("user",ss);
//设置路径
cookie.setPath("/");
if(auto != null){//auto有值是on,没有值是null
//设置保存时间为integer的最大值
cookie.setMaxAge(Integer.MAX_VALUE);
}else{
//为0表示没有存进去
cookie.setMaxAge(0);
}
//将cookie发回客户端
response.addCookie(cookie);
//跳转到主界面
request.getSession().setAttribute("user", user);
request.getRequestDispatcher("/main.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
过滤器代码(LoginFilter.java)
package com.qianfeng.filter;
import java.io.IOException;
import java.net.URLDecoder;
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 com.qianfeng.javaBean.User;
import com.qianfeng.service.UserService;
import com.qianfeng.serviceImpl.UserServiceImpl;
public class LoginFilter implements Filter {
//销毁
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest hsr = (HttpServletRequest)request;
//获取Cookie
Cookie[] cookies = hsr.getCookies();
//保存cookie中的用户名,密码
String name = null;
String pwd = null;
if(cookies != null){
for(Cookie cookie : cookies){
//判断键
if(cookie.getName().equals("user")){
//获取值
String value = cookie.getValue();
//在cookie中保存中文的时候---解码
String newValue = URLDecoder.decode(value, "utf-8");
//切割--获取用户名和密码
String[] ss = newValue.split("&");
//保存用户名和密码
name = ss[0];
pwd = ss[1];
//先查询数据库进行验证
UserService uService = new UserServiceImpl();
User user = uService.findUserByNameAndPwd(name, pwd);
if(user != null){
//设置user
hsr.getSession().setAttribute("user", user);
}else{
//没有保存的值,说明没有自动登录设置,让用户重新登录
hsr.getSession().setAttribute("user", null);
}
}
}
}
chain.doFilter(hsr, response);
}
//注册
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
dao层处理(UserDaoImpl.java)
package com.qianfeng.daoImpl;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.qianfeng.dao.UserDao;
import com.qianfeng.javaBean.User;
import com.qianfeng.util.C3P0Util;
public class UserDaoImpl implements UserDao {
@Override
public User findUserByNameAndPwd(String name, String pwd) throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
return qr.query("select * from user where name=? and password=?",new BeanHandler<User>(User.class),name,pwd);
}
}
这里查询数据库用的DBUtils与C3P0一起用的,可以取我们的资源下载
登录界面(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>
<form action="${pageContext.request.contextPath }/LoginServlet" method="post">
用户名:<input type="text" name="name"><br>
密 码:<input type="password" name="pwd"><br>
<input type="checkbox" name="box">记住密码<br>
<input type="submit" value="登录">
</form>
</body>
</html>
主界面(main.jsp)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<c:if test="${empty user }">
<a href="${pageContext.request.contextPath }/login.jsp">登录</a><br>
<a>注册</a>
</c:if>
<c:if test="${not empty user }">
欢迎${user.name }登录
</c:if>
</body>
</html>
解释:第一次登录时显示要登录,注册的界面.当有选择了自动登录后,会显示欢迎界面.
在service层和dao层中的接口只是方法的声明,实现都是在serviceImpl以及daoImpl中实现.
效果图:
实现自动登录需要的jar包:http://download.csdn.net/download/itqingliang/9961045