共用界面
login.jsp
<html> <head> <title>Title</title> </head> <body> <form action="/login" method="post"> //action的内容根据实验对象不同需要修改 <table> <tr> <th>用户名:</th> <td><input type="text" name="username"></td> </tr> <tr> <th>密码:</th> <td><input type="text" name="password"></td> </tr> <tr> <td><input type="submit" value="登录"></td> <td><input type="submit" value="重置"> </td> </tr> </table> </form> </body> </html>
一.request
LoginServlet
@WebServlet("/login") public class LoginServlet extends HttpServlet { private String myusername="admin"; private String mypassword="123123"; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username= req.getParameter("username"); String password=req.getParameter("password"); if(username.equals(myusername) &&password.equals(mypassword)){ req.setAttribute("username",username); //request存信息的方法 req.getRequestDispatcher("welcome.jsp").forward(req,resp); }else{ resp.sendRedirect("login.jsp"); } } }
LogoutServlet
@WebServlet("/logout") public class LogoutServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.sendRedirect("login.jsp"); } }
welcome.jsp
hello <%=request.getAttribute("username")%> // request 读取信息的方法 <a href="/logout"></a>
结果
可以看到启动welcome时用户信息已经没了
原因:request只能在一次请求响应中存在,运行welcome.jsp时已经是另外一次响应了
二.Session
SessionLoginServlet
@WebServlet("/session_login") public class SessionLoginServlet extends HttpServlet { private String myusername="admin"; private String mypassword="123123"; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username= req.getParameter("username"); String password=req.getParameter("password"); if(username.equals(myusername) &&password.equals(mypassword)){ HttpSession session=req.getSession(); //创建Session的方法 session.setAttribute("username",username); //Session存储信息的方法 resp.sendRedirect("session_welcome.jsp"); }else{ resp.sendRedirect("session_login.jsp"); } } }
SessionLogoutServlet
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session =req.getSession(); session.invalidate(); //session销毁自己的方法 }
session_welcome.jsp
hello<%=session.getAttribute("username")%> //session获取信息的方法 <a href="/session_logout">退出登录</a>
结果
怎样刷新都还有信息再那
当把服务器停止再启动时
可以看到信息已经不在了
原因:
session的信息保存在服务器端,当服务器停止了则信息没了
三.Cookie
CookieLoginServlet
@WebServlet("/cookie_login") public class CookieLoginServlet extends HttpServlet { private String myusername="admin"; private String mypassword="123123"; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username= req.getParameter("username"); String password=req.getParameter("password"); if(username.equals(myusername) &&password.equals(mypassword)){ Cookie cookie=new Cookie("username",username); //创建cookie resp.addCookie(cookie); //将变动的cookie从服务器响应给客户端 resp.sendRedirect("cookie_welcome.jsp"); }else{ resp.sendRedirect("cookie_login.jsp"); } } }
CookieLogoutServlet
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cookie[]cookies=req.getCookies(); //获取到cookie数组 for(Cookie cookie:cookies){ //用foreach找到我们传的cookie
if(cookie.getName().equals("username")){ cookie.setMaxAge(0); //设置cookie的生命周期为0 } }
}
cookie_welcome.jsp
<% Cookie []cookies=request.getCookies(); for(Cookie cookie:cookies){ if(cookie.getName().equals("username")){ out.write(cookie.getValue()); } } %> <a href="/cookie_logout">退出登录</a>
结果
怎么刷新都在
将服务器停止再启动
发现仍然还在
原因:
cookie不因为服务器的停止就不在,它保存在浏览器中