提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
Session:存储在服务器端
Cookie:存储在浏览器端
提示:以下是本篇文章正文内容,下面案例可供参考
一、Sesion实现项目登录控制
登录成功 创建session 向里面存入一个username 访问其他页面时候都去看一下session是否被创建 并且里面是否有username信息
注意 :request.getSession(false) 表示如果session存在就获取这个session 如果不存在就不创建了
//登录成功
HttpSession session = request.getSession();
session.setAttribute("username",username);
response.sendRedirect("/oa/listoa");
访问项目所有页面都去获取session数据进行校验
校验通过往下走 校验不通过返回登录页面
HttpSession session = request.getSession(false);
if (session != null && session.getAttribute("username") != null){ //有登录信息
二、利用Cookie实现多天免登录
前台页面调整 登录页面增加复选框 用户选择是否多天免登录
<form action="<%=request.getContextPath()%>/loginoa" method="post">
用户名<input type="text" name="username" ><br>
密码<input type="password" name="password" ><br>
<input type="checkbox" name="f" value="1"> 是否1天免登录<br>
<input type="submit" value="登录">
</form>
登录servlet判断 用户是否勾选多天免登录 勾选了 存入cookie
private void doLogin(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
String password = request.getParameter("password");
//判断是否勾选了一天免登录 勾选了 将用户名密码存入Cookie
String flag = request.getParameter("f");
if ("1".equals(flag)){
Cookie cookie1 = new Cookie("username", username);
Cookie cookie2 = new Cookie("password", password);
cookie1.setPath(request.getContextPath());
cookie2.setPath(request.getContextPath());
cookie1.setMaxAge(60*60*24);
cookie2.setMaxAge(60*60*24);
response.addCookie(cookie1);
response.addCookie(cookie2);
}
创建一个新的servlet 将其配置成欢迎页面 替换原来的欢迎页面 以他为入口 先判断cookie里面是否有我们要的用户名密码信息 如果有代表上次用户勾选了多天免登录 我们拿着这个数据去数据库校验
//这个servlet起到一个入口作用 进行一个分流 判断如果用户 cookie信息中有上次点击一天免登录存入的用户名密码 就自动登录进入列表页面 如果没有 就跳到登录页面
//需要取消默认跳转到index.jsp 再web.xml中将这个servlet配置到欢迎页中
@WebServlet("/welcomeoa")
public class WelcomeOA extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
String username = "";
String password = "";
if (cookies != null){
for (Cookie cookie:cookies){
if ("username".equals(cookie.getName())){
username = cookie.getValue();
}else if ("password".equals(cookie.getName())){
password = cookie.getValue();
}
}
}
//遍历后 取到cookie的username 和password 如果为空 则直接跳转登录页面 如果不为空 连接数据库进行校验 防止用户修改密码 校验没问题直接进入列表页面 有问题 跳转登录页面
if (username!=null&&password!=null){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
conn = DBUtil.getConnection();
String sql = "select * from t_user where loginName = ? and loginPwd = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
if (rs.next()){
//登录成功
HttpSession session = request.getSession();
session.setAttribute("username",username);
response.sendRedirect(request.getContextPath()+"/listoa");
}else{
//登录失败
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
DBUtil.distroy(conn,ps,rs);
}
}else {
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
}
安全退出功能:
目的是退出时删除cookie信息 防止别人直接输入网址就进入了你的用户
private void doExit(HttpServletRequest request, HttpServletResponse response) {
//将cookie删除 原理 建立一个新的cookie 与原来的返回浏览器的cookie同名 set存活时间为0 这样就关闭浏览器就没了 路径也要保持一致 这里只需要杀死username 即可导致下次用户需要重新登录
Cookie cookie = new Cookie("username",request.getSession().getAttribute("username").toString());
cookie.setMaxAge(0);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
}
原理:建立一个新的cookie 与原来的返回浏览器的cookie同名 set存活时间为0 这样就关闭浏览器就没了 路径也要保持一致 这里只需要杀死username 即可导致下次用户需要重新登录
总结
利用Session和Cookie来实现登录与免登录功能 要深刻理解Session和Cookie的区别