1、创建简单的用户登录界面(jsp文件),其中<select name="daylength">的功能是将用户登录后自动保存用户名及密码的时间,保证该用户再次登录时不需要输用户名及密码
<body> <div style="color:red;">${errmsg }</div> <form action="myservlet" method="post"> 用户名:<input name="uname"> <br> 密码:<input type="password" name="upwd"> <br> <select name="daylength"> <option value="-1">请选择</option> <option value="3">3天</option> <option value="7">7天</option> <option value="30">30天</option> </select> <br> <input type="submit" value="登录"> </form> </body>
2、在index.jsp文件中获取cookie(通过cookie获取用户名及密码)
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //获取全部cookie Cookie[] cookies = request.getCookies(); String name = null,pwd = null; //遍历 for(Cookie c : cookies){ if(c.getName().equals("username")){ //获取用户名 name = c.getValue(); } if(c.getName().equals("userpwd")){ //获取密码 pwd = c.getValue(); } } if(name != null && pwd != null){ //登录 request.getRequestDispatcher("myservlet?uname="+name+"&upwd="+pwd).forward(request, response); } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3、用户登录失败界面(用户登录失败将返回登录界面)
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //判断session是否有用户名 String name = (String)session.getAttribute("currName"); if(name == null){ //保存错误信息 request.setAttribute("errmsg", "登录失败,请重新登录!"); //非法访问,跳转回登录页面 request.getRequestDispatcher("MyJsp.jsp").forward(request, response); } %>
4、用户登录成功的界面设定
(1)判断用户之前是否登陆过并作出相应判断
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //判断session是否有用户名 String name = (String)session.getAttribute("currName"); if(name == null){ //保存错误信息 request.setAttribute("errmsg", "您还没有登录,请先登录!"); //非法访问,跳转回登录页面 request.getRequestDispatcher("MyJsp.jsp").forward(request, response); } %>
(2)用户登录成功后的显示并记录该用户是第几位登录,以及退出登录后返回到登录界面
<body> 欢迎!${currName } <br /> 您是第${count }位访问者。 <br /> <a href="outservlet">退出登录</a> </body>
5、用户信息的提取与保存(servlet)
package day01; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; 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; public class myservlet extends HttpServlet { public myservlet() { super(); System.out.println("调用了构造方法"); } public void destroy() { super.destroy(); // Just puts "destroy" string in log System.out.println("servlet成功销毁!"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取表单提交的数据 String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); if("lxd".equals(uname) && "123".equals(upwd)) { //获取是否保存用户名密码 String daylength = request.getParameter("daylength"); if(daylength != null && !daylength.equals("-1")){ //保存 int day = Integer.parseInt(daylength); Cookie cname = new Cookie("username", uname); Cookie cpwd = new Cookie("userpwd",upwd); //设置保存时间 cname.setMaxAge(day*24*3600); cpwd.setMaxAge(day*24*3600); //添加到response response.addCookie(cname); response.addCookie(cpwd); } //将用户名保存在session中 HttpSession session = request.getSession(); session.setAttribute("currName", uname); //将用户名保存在request中 request.setAttribute("abc", uname); //登录人数+1 ServletContext sc = this.getServletContext(); Integer count = (Integer)sc.getAttribute("count"); if(count != null){ count++; }else{ count=1; } sc.setAttribute("count",count); //跳转到成功页面 //response.sendRedirect("success.jsp"); request.getRequestDispatcher("success.jsp").forward(request, response); }else{ //跳转到失败页面 response.sendRedirect("fail.jsp"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } public void init() throws ServletException { System.out.println("init()成功执行"); } }
6、退出登录并返回登录界面设定
package day01; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class outservlet extends HttpServlet { public outservlet() { super(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //让session失效 request.getSession().invalidate(); response.sendRedirect("MyJsp.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
7、执行效果
(1)登录界面
(2)用户登录失败
3)用户登录
!用户之前未登录时:
!!用户之前已经登录过时
(4)登录成功显示
(5)退出登录