package com.bw.servlet;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.bw.dao.LoginDaoImpl;
import com.bw.entity.User;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
String cook = request.getParameter("cook");
LoginDaoImpl login = new LoginDaoImpl();
User uname1 = login.yh(uname);
if(uname1!=null){
User dl = login.dl(uname, pwd);
if(dl!=null){
if(cook!=null){
Cookie cookiename = new Cookie("uname", URLEncoder.encode(uname, "utf-8"));
Cookie cookiepwd = new Cookie("pwd", URLEncoder.encode(pwd, "utf-8"));
//cookie记住用户名和密码7天
cookiename.setMaxAge(60*60*24*7);
cookiepwd.setMaxAge(60*60*24*7);
response.addCookie(cookiename);
response.addCookie(cookiepwd);
}
//登录成功把信息存到session中
HttpSession session = request.getSession();
//给session设置过期时间为1小时
session.setMaxInactiveInterval(3600);
session.setAttribute("dl", dl);
//登录成功跳转到列表
response.sendRedirect("SeachLbServlet");
}else{
request.setAttribute("error", "密码输入有误!");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}else{
request.setAttribute("error", "账户输入有误!");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.URLDecoder"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!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>
<center>
<%
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if(cookie.getName().equals("uname")){
String uname = URLDecoder.decode(cookie.getValue(), "utf-8");
request.setAttribute("uname", uname);
}
if(cookie.getName().equals("pwd")){
String pwd = URLDecoder.decode(cookie.getValue(), "utf-8");
request.setAttribute("pwd", pwd);
}
}
%>
${error }
<form action="LoginServlet">
用户名:<input type="text" name="uname" value="${uname }"><br>
密码:<input type="password" name="pwd" value="${pwd }"><br>
记住用户名和密码:<input type="checkbox" name="cook" ${pwd!=null?'checked':"" } ><br>
<input type="submit" value="登录">
</form>
</center>
</body>
</html>