Jsp第五次作业
login.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=utf-8"> <title>登陆页面</title> <style type="text/css"> body{font-size: 16px;} </style> <script type="text/javascript"> function mycheck() { //判断用户名是否为空 if (form1.userName.value==""){ alert("用户名不能为空,请输入用户名!"); form1.userName.focus(); return; } //判断密码是否为空 if (form1.password.value=="") { alert("密码不能为空,请输入密码!"); form1.password.focus(); return; } //判断验证码是否为空 if (form1.validationCode.value==""){ alert("验证码不能为空,请输入验证码!"); form1.validationCode.focus(); return; } //判断验证码是否正确 if (form1.validationCode.value != form1.validationCode1.value) { alert("请输入正确的验证码!!"); form1.validationCode.focus(); return; } form1.submit1(); } </script> </head> <body bgcolor="pink"> <form action="loginCheck.jsp" name="form1" method="post"> 用户名:<input type="text" name="userName" size="16"> <br> 密 码: <input type="password" name="password" size="18"> <br> 验证码:<input type="text" name="validationCode" onKeyDown="if(event.keyCode==13){form1.submit.focus();}" size="6"> <% int intmethod1 = (int) ((((Math.random()) * 7)) - 1); int intmethod2 = (int) ((((Math.random()) * 7)) - 1); int intmethod3 = (int) ((((Math.random()) * 7)) - 1); int intmethod4 = (int) ((((Math.random()) * 7)) - 1); //将得到的随机数进行连接 String intsum=intmethod1+""+intmethod2+intmethod3+intmethod4; %> <input type="hidden" name="validationCode1" value="<%=intsum%>"> <img style="height:20px;weight:20px" src="images/<%=intmethod1 %>.png"> <img style="height:20px;weight:20px" src="images/<%=intmethod2 %>.png"> <img style="height:20px;weight:20px" src="images/<%=intmethod3 %>.png"> <img style="height:20px;weight:20px" src="images/<%=intmethod4 %>.png"> <br> <input type="submit" name="submit1" value="登录" onClick="mycheck()"> <input type="reset" value="重置"> </form> </body> </html>
loginCheck.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=utf-8"> <title>处理登陆页面的数据</title> </head> <body bgcolor="pink"> <% //设置请求的编码,用于解决中文乱码问题 request.setCharacterEncoding("UTF-8"); String name =request.getParameter("userName"); String password=request.getParameter("password"); if(request.getParameter("validationCode1").equals(request.getParameter("validationCode")))//判断输入的和随机的是否相等 { if(name.equals("gjm")&&(password.equals("123456"))){ //把用户名到session中 session.setAttribute("userName",name); //若用户名密码正确,将用户名添加到对象中 response.sendRedirect("main.jsp"); }else{ response.sendRedirect("login.jsp"); //若用户名密码不正确执行 } }else{ response.sendRedirect("login.jsp"); //若验证码不相等执行 } %> </body> </html>
main.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=utf-8"> <title>主页面</title> </head> <body bgcolor="pink"> <% //获取保存在session中的用户名 String name=(String)session.getAttribute("userName"); %> 您好<%=name%>,欢迎您访问!<br> <a href="exit.jsp">[退出系统]</a> </body> </html>
exit.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=utf-8"> <title>退出系统</title> </head> <body> <% session.invalidate();//销毁session response.sendRedirect("login.jsp");//再次去登陆页面 %> </body> </html>