1.设计教师与学生不同登录界面
2.验证码随机生成
3.提交后分别转向教师页面和学生页面进行判断用户名和密码的正确性
如图所示的页面结构:
首先,登录做好页面的背景图片:
然后开始编程,我在这里设定的用户名和密码都是特定的,比较简单的方法:
login.jsp页面:登录
<%@ page contentType="text/html;charset=gbk"%> <%@ page language="java" import="java.sql.*" errorPage=""%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用户登录</title> <style type="text/css"> table { background-image: url(1.png); width: 443px; height: 317px; margin: 0 auto; } </style> <script language="javascript"> function loadimage() { document.getElementById("randImage").src = "image.jsp?" + Math.random(); } </script> </head> <body> <table border="0" cellpadding="0" cellspacing="0"> <form action="validate.jsp" method="post" name="loginForm"> <tr height="70"> </tr> <tr> <td> </td> <td width="118" height="25" valign="middle" align="left"><input type="text" name="id" size="15" ></td> </tr> <tr> <td> </td> <td width="118" height="25" valign="middle" align="left"><input type="password" name="password" size="15"></td> </tr> <tr> <td> </td> <td width="118" height="25" valign="middle" align="left"><input type="text" name="rand" size="15"></td> </tr> <tr> <td width="138" valign="middle" align="right"><img alt="code..." name="randImage" id="randImage" src="image.jsp" width="60" height="20" border="1" align="absmiddle"> </td> <td height="36" align="left" valign="middle"><a href="javascript:loadimage();"><font class=pt95>看不清点我</font></a></td> </tr> <tr> <td width="118" height="22" valign="middle" align="right"><input type="radio" name="radio" value="1" />部门 <input type="radio" name="radio" value="2" />教师 </td> <td width="118" height="22" valign="middle" align="left"><input type="radio" name="radio" value="3" />学生 <input type="radio" name="radio" value="4" />访客</td> </tr> <tr> <td height="36" align="right" valign="middle"><input type="submit" name="login" value="登录"> </td> <td height="36" align="left" valign="middle"><input type="reset" value="重 置"></td> </tr> </form> </table> </body> </html>
image.jsp页面,用来画出验证码
<%@ page contentType="image/jpeg" import="java.awt.*, java.awt.image.*,java.util.*,javax.imageio.*"%> <%!Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); }%> <% response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); int width = 60, height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); Random random = new Random(); g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } String sRand = ""; for (int i = 0; i < 4; i++) { String rand = String.valueOf(random.nextInt(10)); sRand += rand; g.setColor(new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 + random.nextInt(110))); g.drawString(rand, 13 * i + 6, 16); } // 将认证码存入SESSION session.setAttribute("rand", sRand); g.dispose(); ImageIO.write(image, "JPEG", response.getOutputStream()); %>
validate.jsp页面:验证教师和学生不同用户登录时的用户名和密码以及验证码的正确性
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage=""%> <% String rand = (String) session.getAttribute("rand"); String input = request.getParameter("rand"); String login = request.getParameter("radio"); String name = request.getParameter("id"); String password = request.getParameter("password"); if (login != null) { if (login.equals("1")) { out.print("部门"); } if (login.equals("2")) { if (name.equals("teacher")) { if (password.equals("teacher")) { if (rand.equals(input)) { out.print("验证码通过!"); out.print("教师登录"); } else { out.print("<script>alert('请输入正确的验证码!');location.href='login.jsp';</script>"); } } else { out.print("<script>alert('请输入正确的密码!');location.href='login.jsp';</script>"); } }else{ out.print("<script>alert('请输入正确的用户!');location.href='login.jsp';</script>"); } } if (login.equals("3")) { if (name.equals("yeting")) { if (password.equals("yeting")) { if (rand.equals(input)) { out.print("验证码通过!"); out.print("学生登录"); } else { out.print("<script>alert('请输入正确的验证码!');location.href='login.jsp';</script>"); } } else { out.print("<script>alert('请输入正确的密码!');location.href='login.jsp';</script>"); } }else{ out.print("<script>alert('请输入正确的用户!');location.href='login.jsp';</script>"); } } if (login.equals("4")) { out.print("访客"); } } %>
完成结果:
代码上传至附件中。
相关推荐
JSP开发中在spring mvc项目中实现登录账号单浏览器登录.docx
利用jsp实现登录界面的跳转,数据库采用的是mysql,可以输入账号密码进行登录
使用JSP实现用户登录验证功能 创建index.jsp 代码: Insert title here 账号: 密码: 验证成功: 验证失败: 这是上机实验(第三次) 作者:艺博东
Javaweb账号登陆 实现登录页面login.jsp,在其中输入用户名,密码,发送到登录验证页面loginCL.jsp,验证登录成功.
用户填写完信息后,通过JSP获得用户信息,并对用户输入的信息进行校验,至少包括三个校验: 用户名是否存在 两次输入的密码是否相同 用户输入的年龄是否为数字 用户输入的日期格式是否正确(可选) 如果校验不通过,...
jsp+servlet+jdbc实现简单的购物车实例,购物数据通过session存储 1、加入购物车时,把数据保存在session中 2、点击系统退出时,购物车中的数据插入到数据库购物车表 3、再次登录时,查询数据库购物车表的数据加载到...
jsp+sql+javabean用户登录页面 用户在登录页面login.jsp的两个文本框中分别输入用户名和密码,若输入正确(即:输入的数据和数据库中的数据相一致,使用JavaBean验证)则弹出对话框,显示提示信息“登录成功,确定后...
是jsp的MD5加密包括JSP程序和JAVA程序
使用MySQL+jsp+正则验证身份证、电话号码、邮箱格式是否正确,验证用户名是否被注册,验证密码是否正确,登录错误次数过多锁住账户一天。
用myeclipse+jsp+javabean+mysql实现的用户登录,登录时需要输入验证码。
采用java技术和mysql数据库技术开发的比克系统,是比较完善的博客系统开发案例,包含博客的发布,博客管理,用户评论,账号管理等功能,是学习的非常好的案例。
如何设计一个通过数据库验证的简单的登录界面,若用户输入正确的账号,密码,则提示成功,否则失败。
jsp+javabean实现的分页web程序,另外附上我对这个程序做了个总结 和程序所需要用到的数据库,希望对大家有帮助。
自己写的一个简单的用户登录页面,实现与后台数据库的交互。非常适合新手
1、登录页面login.jsp:通过提交表单实现一个简单的登陆页面 2、判断用户名密码页面dologin.jsp:通过对比用户输入的账号密码与后台提前写好的账号密码是否一致,一致则跳转至登录成功页面,失败则跳转至登录失败...
本项目是一套基于servlet和jsp实现的学生信息管理系统,主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者。 包含:项目源码、项目文档、数据库脚本、软件工具等,该项目附带全部源码可作为...
JSP开发中在spring mvc项目中实现登录账号单浏览器登录 在很多web产品中都需要实现在同一时刻,只能允许一个账号同时只能在一个浏览器当中登录。通俗点讲就是当A账号在 浏览器1当中登录了,此时在浏览器2中登录A账号...
通过综合运用JSP、Servlet、JavaBean、JDBC、MySQL和MVC + DAO等技术与方法,设计与实现一个留言管理系统,进一步理解和掌握Java Web编程开发的基本技术和方法,熟练使用开发工具。 二、课程设计内容 设计留言管理...
用户可以登录、注册、用户信息的管理等操作,利用jsp与JavaBean来实现