《Cookie案例3》——————使用Cookie记录登录的用户名,并用Session获取到用户名
程序员文章站
2024-03-20 13:33:46
...
1.依旧是准备工作,导包,工具类
2.实体类
package com.offcn.bean;
public class User {
private int id;
private String username;
private String password;
public User(){
}
public User(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.查询数据库
package com.offcn.dao;
import com.offcn.bean.User;
public interface UserDao {
public User login(String username,String password);
}
package com.offcn.dao.impl;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.offcn.bean.User;
import com.offcn.dao.UserDao;
import com.offcn.utils.C3P0Utils;
public class UserDaoImpl implements UserDao {
public User login(String username, String password) {
User user = null;
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
String sql = "select * from user where username=? and password=?";
try {
user = qr.query(sql, new BeanHandler<>(User.class),username,password);
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
}
3.主程序
package com.offcn.servlet;
import java.io.IOException;
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 javax.servlet.http.HttpSession;
import com.offcn.bean.User;
import com.offcn.dao.UserDao;
import com.offcn.dao.impl.UserDaoImpl;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String record = request.getParameter("record");
UserDao dao = new UserDaoImpl();
User user = dao.login(username, password);
if(user!=null){ //登录成功
HttpSession session = request.getSession();
session.setAttribute("user", user);
int num = Integer.parseInt(record);
if(num!=0){ // 要记录用户名
//中文字符不能存在Cookie,使用URLEncoder的encode方法进行格式化
username = URLEncoder.encode(username, "UTF-8");
Cookie c = new Cookie("username",username);
c.setMaxAge(num*24*60*60);
response.addCookie(c);
}
response.sendRedirect("index.jsp");
}else{
response.sendRedirect("login.jsp?msg=loginerror");
}
}
}
4.登录页面
<%@page import="java.net.URLDecoder"%>
<%@page import="com.offcn.utils.CookieUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
<%
Cookie[] cs = request.getCookies();
Cookie c = CookieUtils.findCookieByName(cs,"username");
String username = "";
if(c!=null){
username = c.getValue();
// 解码
username = URLDecoder.decode(username, "UTF-8");
}
%>
<div style="color:red">
<c:if test="${param.msg=='loginerror' }">
用户名或密码错误
</c:if>
<c:if test="${param.msg=='pleaselogin' }">
请登录
</c:if>
</div>
<form action="${pageContext.request.contextPath }/LoginServlet" method="post">
用户名:<input type="text" name="username" value="<%=username %>" /><br/>
<%--用户名: <input type="text" name="username" value="${cookie.username.value }" /><br/> --%>
密码: <input type="password" name="password" /><br/>
记录用户名: <select name="record">
<option value="0">不记录</option>
<option value="7">记录7天</option>
<option value="30">记录30天</option>
</select><br/>
<input type="submit" value=" 登录 " />
</form>
</body>
</html>
5.登录成功跳转页面
<%@ 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>Insert title here</title>
</head>
<body>
<h1>主页,欢迎,${sessionScope.user.username }</h1>
</body>
</html>
上一篇: cookie
下一篇: Node Express框架快速入门教程