第一章:cookie与session的使用
1.cookie的使用
1:在index.jsp做一个登录表单作为login请求
<h1>登录</h1>
<form action="login" method="post">
用户名:<input type="text" name="username" value="${unamecookie}"><br>
密码:<input type="password" name="pass"><br>
<input type="submit" value="登录">
</form>
2:创建servlet处理类处理login请求
package servlet;
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 java.io.IOException;
@WebServlet(urlPatterns = "/login")
public class loginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接参数
String username = req.getParameter("username");
String pass = req.getParameter("pass");
//写逻辑:判断正误
if ("admin".equals(username)&&"123456".equals(pass)){
//登录成功
resp.sendRedirect("success.jsp");
}else{
//登录失败
//1.后台创建cookie
Cookie cookie = new Cookie("uname",username);
//2.返回给前端
resp.addCookie(cookie);
resp.sendRedirect("index.jsp");
}
}
}
3:?前端使用cookie
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
String value="";
if (cookies.length>0){//这个if判断避免因为遍历空数据而报错
for (Cookie cookie : cookies) {//遍历cookies数组
if (cookie.getName().equals("uname")){
value = cookie.getValue();
break;
};
}
}
pageContext.setAttribute("unamecookie",value);
%>
<h1>登录</h1>
<form action="login" method="post">
用户名:<input type="text" name="username" value="${unamecookie}"><br>
密码:<input type="password" name="pass"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
?2.session的使用
1.登录成功session存值,设置失效时间
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet(urlPatterns = "/login")
public class loginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接参数
String username = req.getParameter("username");
String pass = req.getParameter("pass");
//写逻辑:判断正误
if ("admin".equals(username)&&"123456".equals(pass)){
//登录成功
//session存值
HttpSession session = req.getSession();
session.setAttribute("username",username);
session.setAttribute("psssword",pass);
//设置session失效时间
session.setMaxInactiveInterval(60*30);//单位是秒
//重定向或者转发,session数据是不会丢失的,但重定向时,requet中的数据会丢失
resp.sendRedirect("success.jsp");
}else{
//登录失败
//1.后台创建cookie
Cookie cookie = new Cookie("uname",username);
//2.返回给前端
resp.addCookie(cookie);
resp.sendRedirect("index.jsp");
}
}
}
2.显示session
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>testsession.jsp</h1>
当前登录用户:${sessionScope.username}<br>
</body>
</html>
3.退出清空session请求
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>success.jsp</h1>
当前登录用户:${sessionScope.username}<br>
<a href="loginout">退出</a>
</body>
</html>
?4.servlet处理类处理退出请求
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/loginout")
public class LoginOutServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().invalidate();//使session失效
resp.sendRedirect("index.jsp");
}
}
|