实验三 动态添加下拉列表的列表项
一、实验目的
1、掌握代码片段中的注释的应用;
2、掌握JSP脚本标示---Java代码片段的应用。
二、实验内容
1、设计教师与学生不同登陆界面,如下图;
2、验证码随机生成;
3、提交后分别转向教师页面和学生页面进行判断用户名和密码正确性;
4、如果正确,3秒后,转向成功页面,否则跳回验证页面;
三、实验方法
1、在同一页面上设计两个单选按钮:教师、学生,当点击提交按钮后,进入相应的JSP页面。当用户名及密码均正确时,进入欢迎界面;如果两者其一不正确就要提醒需要重新输入。在这些操作中,注意request内置对象的正确使用方法;
2、类似于1,使用request和out对象;
3、学习使用重定向方法解决实验内容3。
四、实验学时:2学时
五、实验代码
文件位置:
登录界面:
登录界面 login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!doctype html> <html> <head> <script type="text/javascript"> function reImg() { var img = document.getElementById("Img"); img.src = "image.jsp?code=" + Math.random(); } </script> <title>登录界面</title> <style type="text/css"> .login{ width:400px; height:400px; position:absolute; top:40%; left:40%; margin-top:-100px; margin-left:-100px; } .right{ width: 44px; height: 300px; float:right; } .left{ width: 400px; height: 400px; position:absolute; top:40%; left:35%; margin-top:-100px; margin-left:-100px; } .center{ width:500px; height:500px; position:relative; top:60%; left:35%; background-image:url(images/login_right.jpg); background-repeat:no-repeat; } </style> </head> <body> <div class="center"> <div class="left"> <img src="images/login_ico2.gif"> </div> <div class="login"> <form action="Login_check.jsp" method="post"> <table> <tr> <td> 用户名: </td> <td> <input type="text" name="username"> </td> </tr> <tr> <td>密码:</td> <td> <input type="password" name="password" > </td> </tr> <tr> <td>验证码:</td> <td> <input type="password" name="Yzm" size=4><img border=0 id="Img" src="image.jsp" alt="验证码"><a href="#" onclick="reImg();">看不清 </a> </td> </tr> <tr> <td> <input type=radio name=type value=partment>部门 <input type=radio name=type value=teacher>教师 </td> <td> <input type=radio name=type value=student>学生 <input type=radio name=type value=guest>游客 </td> </tr> <tr> <td> <input type="image" src="images/login_in.gif" name="submit" alt="登录" > </td> <td> <input type="image" src="images/login_res.gif" alt="重置" onclick="reset();return false;" /> </td> </tr> </table> </form> </div> </div> </body> </html>
图片验证 image.jsp:
<%@ page contentType="image/JPEG" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="GBK"%> <%!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(new Color()); //g.drawRect(0,0,width-1,height-1); // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 g.setColor(getRandColor(160, 200)); for (int i = 0; i < 100; 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); } // 取随机产生的认证码(4位数字) 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("code", sRand); // 图象生效 g.dispose(); // 输出图象到页面 ImageIO.write(image, "JPEG", response.getOutputStream()); %>
登录验证界面 Login_check.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String name = request.getParameter("username"); String passwd = request.getParameter("password"); String type = request.getParameter("type"); String yzm = request.getParameter("Yzm"); String code = (String) session.getAttribute("code"); if(yzm.equals(code)){ if(type.equals("teacher") && name.equals("123") && passwd.equals("123")){ session.setAttribute("name", name); session.setAttribute("passwd", passwd); session.setAttribute("type", type); out.print("<script>alert('验证成功!');</script>"); response.sendRedirect("teacher.jsp"); } else { out.print("<script>alert('请正确填写信息!');window.location.href='login.jsp'</script>"); } if(yzm.equals(code)&&type.equals("student")&&name.equals("456")&&passwd.equals("456")){ session.setAttribute("name", name); session.setAttribute("passwd", passwd); session.setAttribute("type", type); out.print("<script>alert('验证成功!');</script>"); response.sendRedirect("student.jsp"); } else //登录失败,回到Login.jsp页面。 { out.print("<script>alert('请正确填写信息!');window.location.href='login.jsp'</script>"); } } else { out.print("<script>alert('请正确填写信息!');window.location.href='login.jsp'</script>"); } %>
教师界面 teather.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>欢迎教师登录</title> </head> <body> <table boder=0> <tr> <th align="left">用户名:</th> <td><%=session.getAttribute("name") %></td> </tr> <tr> <th align="left">密码:</th> <td><%=session.getAttribute("passwd") %></td> </tr> <tr> <th align="left">登录身份:</th> <td><%=session.getAttribute("type") %></td> </tr> </table> </body> </html>
学生界面 student.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>学生登录界面</title> </head> <body> <table boder=0> <tr> <th align="left">用户名:</th> <td><%=session.getAttribute("name") %></td> </tr> <tr> <th align="left">密码:</th> <td><%=session.getAttribute("passwd") %></td> </tr> <tr> <th align="left">登录身份:</th> <td><%=session.getAttribute("type") %></td> </tr> </table> </body> </html>