JSP基础练习---登录验证

1,准备数据

View Code
 1 create table tUser
2 (
3 id varchar(20) not null primary key,
4 name varchar(30) not null,
5 password varchar(50) not null
6 );
7
8 insert into tUser(id,name,password) values('admin','管理员','admin');
9 insert into tUser(id,name,password) values('guest','来宾','guest');
10
11 commit;

2,编写登录表单Login.jsp

View Code
 1 <%@ page contentType="text/html;charset=GBK"%>
2 <html>
3 <head>
4 <title>用户登录</title>
5 </head>
6 <script language="javaScript">
7 function validate(arg)
8 {
9 if(!(/^\w{5,15}$/.test(arg.userid.value)))
10 {
11 alert("用户ID必须是5-15位!");
12 arg.userid.focus();
13 return false;
14 }
15 if(!(/^\w{3,15}$/.test(arg.password.value)))
16 {
17 alert("用户密码必须是5-15位!");
18 arg.password.focus();
19 return false;
20 }
21 return true;
22 }
23 </script>
24 <%
25 request.setCharacterEncoding("GBK");
26 %>
27 <body>
28 <form action="Check.jsp" method="post" onsubmit="return validate(this)">
29 <table width="50%" align="center">
30 <tr>
31 <td colspan="2">用户登录</td>
32 </tr>
33 <tr>
34 <td>用户ID:</td>
35 <td><input type="text" name="userid"/></td>
36 </tr>
37 <tr>
38 <td>密码:</td>
39 <td><input type="password" name="password"/></td>
40 </tr>
41 <tr>
42 <td>验证码:<input type="text" name="code" size="4" maxlength="4"></td>
43 <td><img src="image.jsp"/></td>
44 </tr>
45 <tr>
46 <td><input type="submit" value="提交"></td>
47 <td><input type="reset" value="重置"></td>
48 </tr>
49 </table>
50 </form>
51 </body>
52 </html>
53 <%
54 if(request.getAttribute("err")!=null){
55 %>
56 <h3><%=request.getAttribute("err")%></h3>
57 <%
58 }
59 %>

3,编写验证页面Check.jsp

View Code
 1 <%@ page contentType="text/html" pageEncoding="GBK"%>
2 <%@ page import="java.sql.*"%>
3 <%!
4 public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
5 public static final String DBURL="jdbc:mysql://localhost:3306/study_java";
6 public static final String DBUSER="root";
7 public static final String DBPASSWORD="mysqladmin";
8 %>
9 <%
10 request.setCharacterEncoding("GBK");
11 Connection conn=null;
12 PreparedStatement psmt=null;
13 ResultSet rst=null;
14 String sql="select name from tUser where id=? and password=?";
15 String userid=request.getParameter("userid");
16 String password=request.getParameter("password");
17 String tempStr=null;
18 String codeNum=request.getParameter("code");
19 String rand=(String)session.getAttribute("rand");
20 if(!rand.equals(codeNum))
21 {
22 request.setAttribute("err","输入的验证码不正确!");
23 %>
24 <jsp:forward page="Login.jsp" />
25 <%
26 }
27 boolean flag=false;
28 try
29 {
30 Class.forName(DBDRIVER);
31 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
32 psmt=conn.prepareStatement(sql);
33 psmt.setString(1,userid);
34 psmt.setString(2,password);
35 rst=psmt.executeQuery();
36 if(rst.next())
37 {
38 tempStr=rst.getString(1);
39 request.setAttribute("name",tempStr);
40 flag=true;
41 }
42 if(flag)
43 {
44 %>
45 <jsp:forward page="sucess.jsp" />
46 <%
47 }
48 else
49 {
50 request.setAttribute("err","用户名或密码错误!");
51 %>
52 <jsp:forward page="Login.jsp" />
53 <%
54 }
55
56 }
57 catch(Exception ex)
58 {
59 ex.printStackTrace();
60 }
61 finally
62 {
63 try
64 {
65 rst.close();
66 psmt.close();
67 conn.close();
68 }
69 catch(Exception ex)
70 {
71 ex.printStackTrace();
72 }
73 }
74
75
76 %>

4,验证码image.jsp-------来自李兴华老师的讲课课件

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

5,编写登录成功页面sucess.jsp

View Code
1 <%@ page contentType="text/html" pageEncoding="GBK"%>
2 <%
3 String str=(String)request.getAttribute("name");
4 %>
5 <h1>欢迎,<%=str%>登陆!</h1><a href="Logff.jsp">注销登录</a>

6,编写注销页面logoff.jsp

View Code
1 <%@ page contentType="text/html;charset=GBK"%>
2 <%
3 session.invalidate();
4 %>
5 <jsp:forward page="Login.jsp" />









posted on 2012-01-30 00:19  茫然若失  阅读(547)  评论(0编辑  收藏  举报

导航