JSP实现后台登录

「爱情、让人受尽委屈。」 2024-02-18 20:17 120阅读 0赞

20180520210035324

20180520210049282

20180520210123776

login.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'login.jsp' starting page</title>
  11. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  12. <script type="text/javascript">
  13. function check_admin(form){
  14. if(form.user.value==""){
  15. alert("用户名不能为空");
  16. form.user.focus();
  17. return false;
  18. }
  19. if(form.pass.value==""){
  20. alert("密码不能为空");
  21. form.pass.focus();
  22. return false;
  23. }
  24. if(form.verify.value==""){
  25. alert("验证码不能为空");
  26. form.verify.focus();
  27. return false;
  28. }
  29. }
  30. </script>
  31. </head>
  32. <body>
  33. <center>
  34. <form action="verify.jsp" method="post" onSubmit="return check_admin(this);">
  35. <table width="400" border="1" bordercolor="#99ccff" cellpadding="0" cellspacing="0" style="border-collapse: collapse;">
  36. <tr>
  37. <td height="40" colspan="2" align="center">
  38. <font>后台登录</font>
  39. </td>
  40. </tr>
  41. <tr>
  42. <td height="40" align="right"> 用户名 </td>
  43. <td width="200" align="left" valign="middle">
  44. <input type="text" name="user" size="21" />
  45. </td>
  46. </tr>
  47. <tr>
  48. <td height="40" align="right"> 密码 </td>
  49. <td width="40" align="left" valign="middle">
  50. <input type="password" name="pass" size="21" />
  51. </td>
  52. </tr>
  53. <tr>
  54. <td height="40" align="right"> 验证码 </td>
  55. <td >
  56. <img border="0" src="verifyCode.jsp">
  57. </td>
  58. </tr>
  59. <tr>
  60. <td height="40" align="right"> 输入验证码 </td>
  61. <td>
  62. <input type="text" name="verify" maxlength="4" size="21" value="" />
  63. </td>
  64. </tr>
  65. <tr>
  66. <td height="40" colspan="2" align="center">
  67. <input type="submit" />
  68. <input type="reset" />
  69. </td>
  70. </tr>
  71. </table>
  72. </form>
  73. </center>
  74. </body>
  75. </html>

login_ok.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'login_ok.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. String strLogin=(String)session.getAttribute("login");
  23. String strUser=(String)session.getAttribute("me");
  24. if (strLogin==null)
  25. {
  26. out.println("<h2>请先登录,谢谢!</h2>");
  27. out.println("<h2>5秒钟后,自动跳转到登录页面!</h2>");
  28. response.setHeader("Refresh","5;URL=login.jsp");
  29. }
  30. else
  31. {
  32. if (strLogin.equals("ok"))
  33. {
  34. out.println(strUser+"欢迎进入我们的网站!");
  35. }
  36. else
  37. {
  38. out.println("<h2>用户名或密码错误,请重新登录!</h2>");
  39. out.println("<h2>5秒钟后,自动跳转到登录页面!</h2>");
  40. response.setHeader("Refresh","5;URL=login.jsp");
  41. }
  42. }
  43. %>
  44. </body>
  45. </html>

login_error.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'login_error.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. <style>
  20. *{
  21. margin:0px;
  22. padding: 0px;
  23. }
  24. #t1{
  25. width:500px;
  26. height:300px;
  27. background: #FFE4C4;
  28. position:absolute;
  29. left:50%;
  30. top:50%;
  31. margin:-150px 0 0 -250px;
  32. text-align: center;
  33. line-height: 300px;
  34. font-size:22px;
  35. }
  36. #myProgress {
  37. width: 100%;
  38. height: 30px;
  39. position: relative;
  40. background-color: #ddd;
  41. }
  42. #myBar {
  43. background-color: #4CAF50;
  44. width: 10px;
  45. height: 30px;
  46. position: absolute;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div id="t1">
  52. <!-- <p>密码或验证码输入错误</p> -->
  53. <p>密码或验证码输入错误,0分3秒后可跳转至login页</p>
  54. </div>
  55. <div id="myProgress">
  56. <div id="myBar"></div>
  57. </div>
  58. <script type="text/javascript">
  59. function move() {
  60. var elem = document.getElementById("myBar");
  61. var width = 0;
  62. var id = setInterval(frame, 20);
  63. function frame() {
  64. if (width == 100) {
  65. clearInterval(id);
  66. } else {
  67. width++;
  68. elem.style.width = width + '%';
  69. }
  70. }
  71. }
  72. var maxtime = 2; //按秒计算,自己调整!
  73. function CountDown() {
  74. if (maxtime >= 0) {
  75. minutes = Math.floor(maxtime / 60);
  76. seconds = Math.floor(maxtime % 60);
  77. msg = "密码或验证码输入错误," + minutes + "分" + seconds + "秒后可跳转至login页";
  78. document.getElementById("t1").innerHTML=msg;
  79. //document.all["t1"].innerHTML = msg;
  80. //if (maxtime == 5 * 60)alert("还剩5分钟");
  81. --maxtime;
  82. } else{
  83. clearInterval(timer);
  84. //alert("时间到,结束!");
  85. window.location.href='./login.jsp';
  86. }
  87. }
  88. timer = setInterval("CountDown()", 1000);
  89. move();
  90. </script>
  91. <%-- <div style="font:20px;text-align:center;top:33%;left:33%;position:absolute;border:1px;border-color:red;">密码或验证码输入错误,3秒钟后跳转至login.jsp</div>
  92. <script type="text/javascript">
  93. window.navigator('./login.jsp');
  94. </script>
  95. <%
  96. out.println( "<HTML><HEAD><META http-equiv='refresh' content='3; URL=/login/login.jsp' target=Main></HEAD><BODY bgcolor='#FFFFFF'></body></html>");
  97. //out.println("<script language='javascript'>alert('账号或者密码输入不正确--您的操作有误!');window.location.href='/login/login.jsp';</script>");
  98. %> --%>
  99. </body>
  100. </html>

verify.jsp

  1. <%@ page language="java" import="java.util.*,com.JDBCUtil,java.sql.*" pageEncoding="utf-8"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4. <head>
  5. <title>My JSP 'verify.jsp' starting page</title>
  6. <meta http-equiv="pragma" content="no-cache">
  7. <meta http-equiv="cache-control" content="no-cache">
  8. <meta http-equiv="expires" content="0">
  9. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  10. <meta http-equiv="description" content="This is my page">
  11. </head>
  12. <body>
  13. <%
  14. String rand =(String)session.getAttribute("rand");
  15. String input =request.getParameter("verify");
  16. if(rand.equals(input))
  17. {
  18. String strUser=request.getParameter("user");
  19. String strPass=request.getParameter("pass");
  20. //打开连接
  21. Connection conn = JDBCUtil.getConnection();
  22. //设置sql语句和sql参数
  23. String sql="select COUNT(*) from Admin where a_name= ? and a_pass= ?";
  24. Object[] param =new Object[2];
  25. param[0]=strUser;
  26. param[1]=strPass;
  27. //查询并返回结果集
  28. ResultSet rs=JDBCUtil.doParamQuery(sql, param);
  29. //将指针移动到第一行
  30. rs.next();
  31. //显示第一行第一列的值
  32. System.out.println("数据库有"+rs.getInt(1)+"条记录与此相符合");
  33. if(rs.getInt(1)==1){
  34. System.out.println("登录成功");
  35. //保存登录状态信息
  36. session.setAttribute("login", "ok");
  37. session.setAttribute("me", strUser);
  38. response.sendRedirect("./login_ok.jsp");
  39. }else{
  40. System.out.println("登录失败");
  41. response.sendRedirect("./login_error.jsp");
  42. //直接在此页返回login.jsp,会使验证码不刷新,故使用跳转至错误处理页,再跳转至login.jsp
  43. //out.println("<script>alert('登录失败')</script>");
  44. //window.history.back(-1);验证码不刷新
  45. //out.println("<script>window.history.back(-1);</script>");
  46. //out.println("<script>window.location.href('./index.jsp')</script>");
  47. }
  48. }else{
  49. out.println("<script>alert('验证码错误')</script>");
  50. response.sendRedirect("./login_error.jsp");
  51. //直接在此页返回login.jsp,会使验证码不刷新,故使用跳转至错误处理页,再跳转至login.jsp
  52. //out.println("<script>alert('验证码错误')</script>");
  53. //out.println("<script>window.history.back(-1);</script>");
  54. //out.println("<script>window.location.href('./index.jsp')</script>");
  55. }
  56. /*if(strUser.equals("admin")&&strPass.equals("admin")&&rand.equals(input)){
  57. out.println("<h3>登陆成功</h3>");
  58. }else{
  59. out.println("<h3>登陆失败</h3>");
  60. }*/
  61. /*if(strUser.equals("admin"))
  62. {
  63. if(strPass.equals("admin"))
  64. {
  65. if(rand.equals(input))
  66. {
  67. out.println("<script>alert('登录成功')</script>");
  68. out.println("登录成功");
  69. }
  70. else{
  71. out.println("<script>alert('验证码错误')</script>");
  72. out.println("<script>window.location.href('./index.jsp')</script>");
  73. }
  74. }
  75. else{
  76. out.println("<script>alert('密码错误')</script>");
  77. out.println("<script>window.location.href('./index.jsp')</script>");
  78. }
  79. }
  80. else
  81. {
  82. out.println("<script>alert('用户名不存在')</script>");
  83. out.println("<script>window.location.href('./index.jsp')</script>");
  84. }
  85. */
  86. %>
  87. </body>
  88. </html>

verifyCode.jsp

  1. <!--设置页面类型和编码 -->
  2. <%@ page contentType="image/jpeg" language="java" pageEncoding="utf-8"%>
  3. <!--导入需要的包 -->
  4. <%@ page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*"%>
  5. <!--给定范围获得随机颜色 -->
  6. <%!
  7. Color getRandColor(int fc,int bc)
  8. {
  9. if(fc>255) fc=255;
  10. if(bc>255) bc=255;
  11. //随机生成rgb颜色
  12. Random random = new Random();
  13. int r=fc+random.nextInt(bc-fc);
  14. int g=fc+random.nextInt(bc-fc);
  15. int b=fc+random.nextInt(bc-fc);
  16. return new Color(r,g,b);
  17. }
  18. %>
  19. <%
  20. //设置页面不缓存
  21. response.setHeader("Pragma","No-cache");
  22. response.setHeader("Cache-Control","no-cache");
  23. response.setDateHeader("Expires", 0);
  24. // 在内存中创建图象
  25. int width=60, height=20;
  26. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  27. // 获取图形上下文
  28. Graphics g = image.getGraphics();
  29. //生成随机类
  30. Random random = new Random();
  31. // 设定背景色
  32. g.setColor(getRandColor(200,250));
  33. g.fillRect(0, 0, width, height);
  34. //设定字体
  35. g.setFont(new Font("Times New Roman",Font.PLAIN,22));
  36. //画边框
  37. //g.setColor(new Color());
  38. //g.drawRect(0,0,width-1,height-1);
  39. // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
  40. g.setColor(getRandColor(160,200));
  41. for (int i=0;i<155;i++)
  42. {
  43. int x = random.nextInt(width);
  44. int y = random.nextInt(height);
  45. int xl = random.nextInt(12);
  46. int yl = random.nextInt(12);
  47. g.drawLine(x,y,x+xl,y+yl);
  48. }
  49. // 取随机产生的认证码(4位数字)
  50. String sRand="";
  51. for (int i=0;i<4;i++){
  52. String rand=String.valueOf(random.nextInt(10));
  53. sRand+=rand;
  54. // 将认证码显示到图象中
  55. g.setColor(new Color(20+random.nextInt(110),40+random.nextInt(110),60+random.nextInt(110)));
  56. //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  57. g.drawString(rand,13*i+6,16);
  58. }
  59. // 将认证码存入SESSION
  60. session.setAttribute("rand",sRand);
  61. // 图象生效
  62. g.dispose();
  63. // 输出图象到页面
  64. ImageIO.write(image, "JPEG", response.getOutputStream());
  65. %>

发表评论

表情:
评论列表 (有 0 条评论,120人围观)

还没有评论,来说两句吧...

相关阅读