package ch13_1;import java.sql.*;
public class Check {
private String user,password,rand;//获得用户输入的用户名,密码,验证码
private String crand;//获得系统产生的验证码
private String check_rand="";//检查验证码是否相同
private String user_login;//用户登陆返回信息
private Connection conn;//链接数据库
private PreparedStatement psm;
private ResultSet rs=null;//记录结果
public Check() {
}
// 进行验证码输入验证
public String Check_rand()
{
if(rand.equals(crand))
{
check_rand="验证码输入相同,认证成功!";
}
else
{
check_rand="验证码输入不同,认证失败!";
}
return check_rand;
} //连接数据库
public static Connection getConnection() throws SQLException{
try
{
Class.forName("com.mysql.jdbc.Driver") ;//加载驱动
}
catch (ClassNotFoundException e){
e.printStackTrace();
return null;
}
return DriverManager.getConnection("jdbc:mysql://localhost:3306/chapter13_1","root","root");//静态方法得到数据库链接
}
//判断用户信息
public String check_user_login(){
try
{
conn=Check.getConnection();
psm=conn.prepareStatement("select * from UserInfo where UserName=? and Password=?");
psm.setString(1,user);
psm.setString(2,password);
rs=psm.executeQuery();
if(rs.getString("UserName").equals("1")&&rs.getString("Password").equals("1"))
{
user_login="您输入的用户名为:"+user+"<br>"+"密码为:"+password+"<br>"+"登陆成功:你的权限是管理员";
}
else if(rs.getString("UserName").equals("a")&&rs.getString("Password").equals("a"))
{
user_login="您输入的用户名为:"+user+"<br>"+"密码为:"+password+"<br>"+"登陆成功:你的权限是用户组";
}
else
{
user_login="登陆失败!";
}
psm.close();
rs.close();
} catch (SQLException e) {
System.out.println("出现SQLException异常");
}
finally//关闭数据库
{
if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭数据库链接时出现异常");
}
}
}
return user_login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRand() {
return rand;
}
public void setRand(String rand) {
this.rand = rand;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user=user;
}
public String getCrand() {
return crand;
} public void setCrand(String crand) {
this.crand = crand;
}
// 获得验证信息
public String getCheck_rand() {
return check_rand;
} }调用javabean的页面————
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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>check.jsp</title>
<jsp:useBean id="check" scope="request" class="ch13_1.Check"/>
<jsp:setProperty name="check" property="user"/>
<jsp:setProperty name="check" property="password"/>
<jsp:setProperty name="check" property="rand"/>
</head>
<body>
<%
try
{
check.setCrand((String)session.getAttribute("rand"));
%>
系统产生的认证码为:<%=check.getCrand() %><br>
您输入的认证码为:<%=check.getRand() %><br>
<%=check.Check_rand() %><br><%=check.check_user_login() %>
<%}
catch(NullPointerException e)
{
e.printStackTrace();
}
%>
</body>
</html>登陆页面———<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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>login.jsp</title>
</head>
<body>
<center><b>登录</b></center>
<form action="check.jsp" method="get">
<center>用户名:<input type="text" name="user"></center><br>
<center>密码:<input type="text" name="password"></center><br>
<center>认证码:<input type=text name="rand" maxlength=4 value="" size=6>
<img border=1 src="image.jsp"></center><br>
<center><input type="submit" name="submit" value="登陆">
<input type="reset" name="reset" value="重置"></center> </form></body>
</html>
验证码——<%@ page contentType="image/jpeg; charset=gb2312" 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(new Color());
//g.drawRect(0,0,width-1,height-1);// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
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);
}// 取随机产生的认证码(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("rand",sRand);// 图象生效
g.dispose();// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());%>
注:数据库验证信息时候貌似不起作用~~不知道为什么吖!
public class Check {
private String user,password,rand;//获得用户输入的用户名,密码,验证码
private String crand;//获得系统产生的验证码
private String check_rand="";//检查验证码是否相同
private String user_login;//用户登陆返回信息
private Connection conn;//链接数据库
private PreparedStatement psm;
private ResultSet rs=null;//记录结果
public Check() {
}
// 进行验证码输入验证
public String Check_rand()
{
if(rand.equals(crand))
{
check_rand="验证码输入相同,认证成功!";
}
else
{
check_rand="验证码输入不同,认证失败!";
}
return check_rand;
} //连接数据库
public static Connection getConnection() throws SQLException{
try
{
Class.forName("com.mysql.jdbc.Driver") ;//加载驱动
}
catch (ClassNotFoundException e){
e.printStackTrace();
return null;
}
return DriverManager.getConnection("jdbc:mysql://localhost:3306/chapter13_1","root","root");//静态方法得到数据库链接
}
//判断用户信息
public String check_user_login(){
try
{
conn=Check.getConnection();
psm=conn.prepareStatement("select * from UserInfo where UserName=? and Password=?");
psm.setString(1,user);
psm.setString(2,password);
rs=psm.executeQuery();
if(rs.getString("UserName").equals("1")&&rs.getString("Password").equals("1"))
{
user_login="您输入的用户名为:"+user+"<br>"+"密码为:"+password+"<br>"+"登陆成功:你的权限是管理员";
}
else if(rs.getString("UserName").equals("a")&&rs.getString("Password").equals("a"))
{
user_login="您输入的用户名为:"+user+"<br>"+"密码为:"+password+"<br>"+"登陆成功:你的权限是用户组";
}
else
{
user_login="登陆失败!";
}
psm.close();
rs.close();
} catch (SQLException e) {
System.out.println("出现SQLException异常");
}
finally//关闭数据库
{
if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭数据库链接时出现异常");
}
}
}
return user_login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRand() {
return rand;
}
public void setRand(String rand) {
this.rand = rand;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user=user;
}
public String getCrand() {
return crand;
} public void setCrand(String crand) {
this.crand = crand;
}
// 获得验证信息
public String getCheck_rand() {
return check_rand;
} }调用javabean的页面————
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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>check.jsp</title>
<jsp:useBean id="check" scope="request" class="ch13_1.Check"/>
<jsp:setProperty name="check" property="user"/>
<jsp:setProperty name="check" property="password"/>
<jsp:setProperty name="check" property="rand"/>
</head>
<body>
<%
try
{
check.setCrand((String)session.getAttribute("rand"));
%>
系统产生的认证码为:<%=check.getCrand() %><br>
您输入的认证码为:<%=check.getRand() %><br>
<%=check.Check_rand() %><br><%=check.check_user_login() %>
<%}
catch(NullPointerException e)
{
e.printStackTrace();
}
%>
</body>
</html>登陆页面———<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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>login.jsp</title>
</head>
<body>
<center><b>登录</b></center>
<form action="check.jsp" method="get">
<center>用户名:<input type="text" name="user"></center><br>
<center>密码:<input type="text" name="password"></center><br>
<center>认证码:<input type=text name="rand" maxlength=4 value="" size=6>
<img border=1 src="image.jsp"></center><br>
<center><input type="submit" name="submit" value="登陆">
<input type="reset" name="reset" value="重置"></center> </form></body>
</html>
验证码——<%@ page contentType="image/jpeg; charset=gb2312" 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(new Color());
//g.drawRect(0,0,width-1,height-1);// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
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);
}// 取随机产生的认证码(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("rand",sRand);// 图象生效
g.dispose();// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());%>
注:数据库验证信息时候貌似不起作用~~不知道为什么吖!
if(rs!=null&&rs.next()){
把下面的一部分包起来,关键是需要rs.next()if(rs.getString("UserName").equals("1")&&rs.getString("Password").equals("1"))
{
user_login="您输入的用户名为:"+user+"<br>"+"密码为:"+password+"<br>"+"登陆成功:你的权限是管理员";
}
else if(rs.getString("UserName").equals("a")&&rs.getString("Password").equals("a"))
{
user_login="您输入的用户名为:"+user+"<br>"+"密码为:"+password+"<br>"+"登陆成功:你的权限是用户组";
}
else
{
user_login="登陆失败!";
}