jsp中生成图形验证码 方法2利用在servlet里面来生成验证码
2008-06-07 00:08
1311 查看
jsp页面代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<form action="LoginServlet" method="post">
用 户 名:<input type=text name=username><br>
密 码:<input type=password name=pwd><br>
请输入验证码:<input type=text name=check><img src="ImgServlet" id="img"><a href="javaScript.:change()">看不清,换张</a><br>
<input type=submit value="登录"> <input type=reset value="重置">
</form>
<script type="text/javascript">
function change()
{
document.all.img.src="ImgServlet";
}
</script>
下面便是在servlet里面来生成验证码了.代码如下:
package www.wnetw.net.img;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImgServlet extends HttpServlet {
//得到随机字符的方法
public char[] getRand()
{
char[] rand=new char[4];
String str="0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
for(int i=0;i<4;i++)
{
Random rd=new Random();
int index=rd.nextInt(str.length());
//通过下标获取字符
rand=str.charAt(index);
}
return rand;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收图片类型
response.setContentType("image/jepg");
//创建字节流
ServletOutputStream sos=response.getOutputStream();
//设置缓冲区
BufferedImage img=new BufferedImage(60,30,4);
//在缓冲区中绘图
Graphics g=img.getGraphics();
//调方法,得到随机字符
char[] rand=this.getRand();
//保存于session中,以便其它页面可用
request.getSession().setAttribute("rand", new String(rand));
//干拢线
g.setColor(Color.red);
g.drawLine(new Random().nextInt(60), new Random().nextInt(60),
new Random().nextInt(20), new Random().nextInt(20));
g.drawLine(new Random().nextInt(60), new Random().nextInt(60),
new Random().nextInt(60), new Random().nextInt(20));
g.drawLine(new Random().nextInt(80), new Random().nextInt(60),
new Random().nextInt(50), new Random().nextInt(20));
//画背景色
g.drawRect(0, 0, 60, 30);
g.setColor(Color.blue);
//设置前景色
g.setColor(Color.green);
g.drawString(""+rand[0], 5, 20);
g.drawString(""+rand[1], 20, 10);
g.drawString(""+rand[2], 30, 15);
g.drawString(""+rand[3], 50, 20);
//关闭
g.dispose();
JPEGImageEncoder code=JPEGCodec.createJPEGEncoder(sos);
code.encode(img);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
好,验证码生成.
先写个数据库***作类,理所当然有两个字段,用户名和密码咯.(name,pwd)
数据库简单代码如下:
package www.wnetw.net.db;
import java.sql.*;
public class DataBase {
private Connection con;
private Statement st;
private ResultSet rs;
//获得连结
public Connection getCon()
{
String classStr="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=t74";
try {
Class.forName(classStr);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url,"sa","");
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
//判断用户名与密码是否正确
public boolean isLogin(String name,String pwd) throws SQLException
{
if(con==null)
con=this.getCon();
String sql="select *from Login";
st=con.createStatement();
rs=st.executeQuery(sql);
while(rs.next())
{
String dbname=rs.getString("name");
String dbpwd=rs.getString("pwd");
if(dbname.equals(name)&&dbpwd.equals(pwd))
{
return true;
}
}
return false;
}
}
接下来该写什么?当然又是servlet咯,不信,看看代码便知道,如下:
package www.wnetw.net.img;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
// 得到值
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
// 得到验证码
String check = request.getParameter("check");//输入
String rand = (String) request.getSession().getAttribute("rand");// 随机产生
// 实例化数据库***作类
com.db.DataBase db = new com.db.DataBase();
boolean mark = false;
try {
mark = db.isLogin(name, pwd);
} catch (SQLException e) {
e.printStackTrace();
}
if (mark == true && check.equals(rand)) {
out.print("成功");
} else {
out.print("失败");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<form action="LoginServlet" method="post">
用 户 名:<input type=text name=username><br>
密 码:<input type=password name=pwd><br>
请输入验证码:<input type=text name=check><img src="ImgServlet" id="img"><a href="javaScript.:change()">看不清,换张</a><br>
<input type=submit value="登录"> <input type=reset value="重置">
</form>
<script type="text/javascript">
function change()
{
document.all.img.src="ImgServlet";
}
</script>
下面便是在servlet里面来生成验证码了.代码如下:
package www.wnetw.net.img;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImgServlet extends HttpServlet {
//得到随机字符的方法
public char[] getRand()
{
char[] rand=new char[4];
String str="0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
for(int i=0;i<4;i++)
{
Random rd=new Random();
int index=rd.nextInt(str.length());
//通过下标获取字符
rand=str.charAt(index);
}
return rand;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收图片类型
response.setContentType("image/jepg");
//创建字节流
ServletOutputStream sos=response.getOutputStream();
//设置缓冲区
BufferedImage img=new BufferedImage(60,30,4);
//在缓冲区中绘图
Graphics g=img.getGraphics();
//调方法,得到随机字符
char[] rand=this.getRand();
//保存于session中,以便其它页面可用
request.getSession().setAttribute("rand", new String(rand));
//干拢线
g.setColor(Color.red);
g.drawLine(new Random().nextInt(60), new Random().nextInt(60),
new Random().nextInt(20), new Random().nextInt(20));
g.drawLine(new Random().nextInt(60), new Random().nextInt(60),
new Random().nextInt(60), new Random().nextInt(20));
g.drawLine(new Random().nextInt(80), new Random().nextInt(60),
new Random().nextInt(50), new Random().nextInt(20));
//画背景色
g.drawRect(0, 0, 60, 30);
g.setColor(Color.blue);
//设置前景色
g.setColor(Color.green);
g.drawString(""+rand[0], 5, 20);
g.drawString(""+rand[1], 20, 10);
g.drawString(""+rand[2], 30, 15);
g.drawString(""+rand[3], 50, 20);
//关闭
g.dispose();
JPEGImageEncoder code=JPEGCodec.createJPEGEncoder(sos);
code.encode(img);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
好,验证码生成.
先写个数据库***作类,理所当然有两个字段,用户名和密码咯.(name,pwd)
数据库简单代码如下:
package www.wnetw.net.db;
import java.sql.*;
public class DataBase {
private Connection con;
private Statement st;
private ResultSet rs;
//获得连结
public Connection getCon()
{
String classStr="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=t74";
try {
Class.forName(classStr);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url,"sa","");
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
//判断用户名与密码是否正确
public boolean isLogin(String name,String pwd) throws SQLException
{
if(con==null)
con=this.getCon();
String sql="select *from Login";
st=con.createStatement();
rs=st.executeQuery(sql);
while(rs.next())
{
String dbname=rs.getString("name");
String dbpwd=rs.getString("pwd");
if(dbname.equals(name)&&dbpwd.equals(pwd))
{
return true;
}
}
return false;
}
}
接下来该写什么?当然又是servlet咯,不信,看看代码便知道,如下:
package www.wnetw.net.img;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
// 得到值
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
// 得到验证码
String check = request.getParameter("check");//输入
String rand = (String) request.getSession().getAttribute("rand");// 随机产生
// 实例化数据库***作类
com.db.DataBase db = new com.db.DataBase();
boolean mark = false;
try {
mark = db.isLogin(name, pwd);
} catch (SQLException e) {
e.printStackTrace();
}
if (mark == true && check.equals(rand)) {
out.print("成功");
} else {
out.print("失败");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
相关文章推荐
- JSP中利用Servlet生成验证码图片
- jsp引用servlet生成的验证码..
- servlet生成图形验证码
- Java Web开发之图形验证码的生成与使用方法
- java web验证码生成总结(包括servlet、jsp和struts2实现)
- 练习--JSP+Servlet动态生成验证码
- 最简单的验证码(利用JSP生成验证码)
- JSP + Servlet实现生成登录验证码示例
- jsp引用servlet生成的验证码代码演示
- 图形验证码(JSP+Servlet)
- JSP通过Servlet生成验证码
- 利用Servlet生成验证码
- jsp+Servlet编程实现验证码的方法
- 图形验证码生成servlet
- 用ServletJSP动态生成图像验证码
- 使用Servlet生成图形验证码
- 利用Servlet生成验证码
- ASP.NET生成图形验证码的方法详解
- 使用Servlet 生成随即图形验证码(1)
- 在servlet里面使用printWriter输出ireport到jsp显示乱码的解决方法