Posted on 2007-07-25 14:26
胡娟 閱讀(716)
評(píng)論(1) 編輯 收藏
1.用戶信息VO包
1
package com.hujuan.VO;
2
3
public class UserInfo
{
4
private String username;
5
private String password;
6
public String getPassword()
{
7
return password;
8
}
9
public void setPassword(String password)
{
10
this.password = password;
11
}
12
public String getUsername()
{
13
return username;
14
}
15
public void setUsername(String username)
{
16
this.username = username;
17
}
18
}
2.驗(yàn)證登錄Dao包
1
package com.hujuan.dao;
2
3
import com.hujuan.VO.UserInfo;
4
5
public class UserInfoDao
{
6
public boolean checklogin(UserInfo userinfo)
{
7
System.out.println(userinfo.getUsername());
8
System.out.println(userinfo.getUsername());
9
if("hujuan".equals(userinfo.getUsername())&&"123".equals(userinfo.getPassword()))
{
10
return true;
11
}return false;
12
}
13
}
3.生產(chǎn)隨機(jī)碼驗(yàn)證servlet
1
package com.hujuan.servlet;
2
3
4
import java.awt.Graphics2D;
5
import java.awt.image.BufferedImage;
6
import java.io.IOException;
7
import java.util.Random;
8
import java.awt.Color;
9
import java.awt.Font;
10
import javax.imageio.ImageIO;
11
import javax.servlet.ServletException;
12
import javax.servlet.ServletOutputStream;
13
import javax.servlet.http.HttpServlet;
14
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
16
import javax.servlet.http.HttpSession;
17
18
public class RandomCodeServler extends HttpServlet
{
19
private int width = 60; //驗(yàn)證圖片的寬度
20
private int height = 20; //驗(yàn)證圖片的高度
21
@Override
22
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
23
24
BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
25
26
Graphics2D g = buffImg.createGraphics();
27
28
29
//創(chuàng)建一個(gè)隨機(jī)數(shù)生產(chǎn)器類
30
Random random = new Random();
31
32
g.setColor(Color.WHITE);
33
g.fillRect(0, 0, width, height);
34
35
//創(chuàng)建字體,字體的大小應(yīng)該根據(jù)圖片的高度來(lái)定
36
Font font = new Font("123123",Font.PLAIN,18);
37
//設(shè)置字體
38
g.setFont(font);
39
//畫(huà)邊框
40
g.setColor(Color.BLACK);
41
g.drawRect(0, 0, width-1,height-1);
42
//隨機(jī)生產(chǎn)100條干擾線,使圖像中的驗(yàn)證嗎不易被其他程序探測(cè)到
43
g.setColor(Color.GRAY);
44
for(int i=0;i<100;i++)
{
45
int x = random.nextInt(width);
46
int y = random.nextInt(height);
47
int x1 = random.nextInt(12);
48
int y1 = random.nextInt(12);
49
g.drawLine(x, y, x+x1, y+y1);
50
}
51
//randomCode 用于保存隨機(jī)產(chǎn)生的驗(yàn)證碼,以便用戶登錄后進(jìn)行驗(yàn)證
52
StringBuffer randomCode = new StringBuffer();
53
int red = 0,green = 0,blue = 0;
54
//隨機(jī)產(chǎn)生4位數(shù)字的驗(yàn)證碼
55
for(int i = 0;i<4;i++)
{
56
//得到隨機(jī)產(chǎn)生的驗(yàn)證碼數(shù)字
57
String strRand = String.valueOf(random.nextInt(10));
58
59
//產(chǎn)生隨機(jī)的顏色分量來(lái)構(gòu)造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同
60
red = random.nextInt(110);
61
green = random.nextInt(50);
62
blue = random.nextInt(50);
63
64
//用隨機(jī)產(chǎn)生的顏色將驗(yàn)證碼繪制到圖像中
65
g.setColor(new Color(red,green,blue));
66
g.drawString(strRand, 13*i+6, 16);
67
//將產(chǎn)生的四個(gè)隨機(jī)數(shù)組合在一起
68
randomCode.append(strRand);
69
}
70
//將四位數(shù)字的驗(yàn)證碼保存到session中
71
HttpSession session = req.getSession();
72
session.setAttribute("randomCode", randomCode.toString());
73
//禁止圖像緩存
74
// resp.setHeader("Pragma", "no-cache");
75
// resp.setHeader("Cache-Control", "no-cache");
76
// resp.setDateHeader("Expires", 0);
77
// resp.setContentType("image/jpeg");
78
//將圖像輸出到servlet輸出流中
79
ServletOutputStream out = resp.getOutputStream();
80
ImageIO.write(buffImg,"jpeg",out);
81
out.close();
82
}
83
}
84
4.驗(yàn)證登錄的servlet
1
package com.hujuan.servlet;
2
3
import java.io.IOException;
4
import java.io.PrintWriter;
5
import javax.servlet.ServletException;
6
import javax.servlet.http.HttpServlet;
7
import javax.servlet.http.HttpServletRequest;
8
import javax.servlet.http.HttpServletResponse;
9
import javax.servlet.http.HttpSession;
10
import com.hujuan.VO.UserInfo;
11
import com.hujuan.dao.UserInfoDao;
12
13
public class LoginCheckServlet extends HttpServlet
{
14
15
public void doPost(HttpServletRequest request, HttpServletResponse response)
16
throws ServletException, IOException
{
17
18
//request.setCharacterEncoding("gb2312");
19
response.setCharacterEncoding("gb2312");
20
21
response.setContentType("text/html; charset = gb2312");
22
PrintWriter out = response.getWriter();
23
24
HttpSession session = request.getSession();
25
String randomCode =(String)session.getAttribute("randomCode");
26
27
if(null == randomCode)
{
28
response.sendRedirect("index.jsp");
29
return;
30
}
31
32
String reqRandom = request.getParameter("randomcode");//meiyou huode
33
34
UserInfo userinfo = new UserInfo();
35
String name = request.getParameter("username");
36
String password = request.getParameter("password");
37
38
userinfo.setUsername(name);
39
userinfo.setPassword(password);
40
41
42
// System.out.println(reqRandom);
43
// System.out.println(randomCode);
44
// System.out.println(name);
45
// System.out.println(password);
46
if(randomCode.equals(reqRandom))
{
47
UserInfoDao userdao = new UserInfoDao();
48
if(userdao.checklogin(userinfo))
{
49
response.sendRedirect("succ.jsp");
50
}else
{
51
out.println("請(qǐng)檢查用戶名和密碼");
52
}
53
}else
{
54
out.println("請(qǐng)檢查驗(yàn)證碼");
55
}
56
}
57
58
}
59
5.登錄頁(yè)面
1
<%@ page language="java" import="java.util.*"
2
contentType="text/html; charset=gb2312"%>
3
<%
4
String path = request.getContextPath();
5
String basePath = request.getScheme() + "://"
6
+ request.getServerName() + ":" + request.getServerPort()
7
+ path + "/";
8
%>
9
10
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11
<html>
12
<head>
13
<base href="<%=basePath%>">
14
15
<title>My JSP 'Login.jsp' starting page</title>
16
17
<meta http-equiv="pragma" content="no-cache">
18
<meta http-equiv="cache-control" content="no-cache">
19
<meta http-equiv="expires" content="0">
20
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
21
<meta http-equiv="description" content="This is my page">
22
<!--
23
<link rel="stylesheet" type="text/css" href="styles.css">
24
-->
25
26
<style type="text/css">
27
<!--
28
.text
{
29
font-family: "宋體";
30
font-size: 12px;
31
font-style: normal;
32
line-height: 25px;
33
font-weight: normal;
34
font-variant: normal;
35
color: #000000;
36
}
37
-->
38
</style>
39
</head>
40
41
<body>
42
<form method="post" name="login" action="LoginCheckServlet">
43
<p>
44
45
</p>
46
<table width="40%" border="0" align="center">
47
<tbody>
48
<tr>
49
<td width="22%" align="right" class="text"> 用戶名 </td>
50
<td width="37%"><input name="username" type="text" size="20">
51
<br>
52
</td>
53
</tr>
54
<tr>
55
<td align="right" class="text"> 密 碼 </td>
56
<td><input name="password" type="password" size="20" maxlength="16">
57
</td>
58
</tr>
59
<tr>
60
<td align="right" class="text"> 驗(yàn)證碼 </td>
61
<td><input name="randomcode" type="text" size="5" maxlength="4">
62
<img src="imgcode"> </td>
63
</tr>
64
<tr>
65
<td align="right">
66
<input type="submit" value="提交" name="submit"> </td>
67
<td align="center">
68
<input type="reset" value="重置" name="reset"> </td>
69
</tr>
70
</tbody>
71
</table>
72
</form>
73
</body>
74
</html>
75
6.成功頁(yè)面
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
7
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8
<html>
9
<head>
10
<base href="<%=basePath%>">
11
12
<title>My JSP 'succ.jsp' starting page</title>
13
14
<meta http-equiv="pragma" content="no-cache">
15
<meta http-equiv="cache-control" content="no-cache">
16
<meta http-equiv="expires" content="0">
17
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18
<meta http-equiv="description" content="This is my page">
19
<!--
20
<link rel="stylesheet" type="text/css" href="styles.css">
21
-->
22
23
</head>
24
25
<body>
26
This is my JSP page. <br>
27
</body>
28
</html>
29