验证码图片生成以及页面取得
2015-06-22 16:15
507 查看
package cn.owntt.web.servlet;
import java.awt.Color;
import java.awt.Font;
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 javax.servlet.http.HttpSession;
import javax.swing.JPopupMenu;
import cn.owntt.tool.StrTool;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 验证码图片生产类
* @author leaf
*
*/
public class ValiImgServlet extends HttpServlet{
int width=60;
int height=20;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//带有缓冲的图形对象
BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取画笔工具
Graphics g=bufferedImage.getGraphics();
//绘制背景
g.setColor(new Color(0x800080));
g.fillRect(0, 0, width, height);
//绘制边框
g.setColor(Color.blue);
g.drawRect(0, 0, width-1, height-1);
//获取随机验证码
String valiCode=StrTool.createRDM(4);
Random random=new Random();
//绘制验证码
g.setColor(Color.green);
g.setFont(new Font("Tekton Pro",Font.PLAIN,18));
String str=valiCode.substring(0,1);
g.drawString(str,8,17);
str=valiCode.substring(1,2);
g.drawString(str,20,15);
str=valiCode.substring(2,3);
g.drawString(str,35,18);
str=valiCode.substring(3,4);
g.drawString(str,45,15);
//随机产生15个干扰点
for (int i = 0; i < 15; i++) {
int x=random.nextInt(width);
int y=random.nextInt(height);
g.drawOval(x, y, 1, 1);
}
//释放画笔
g.dispose();
//将验证码存入session
HttpSession session=req.getSession(true);
session.setAttribute("valiCode", valiCode);
//将图形输出到客户端
ServletOutputStream sos=resp.getOutputStream();
//使用图形编码工具
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(sos);
encoder.encode(bufferedImage);
}
}
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>管理员登陆-同途房产</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function changeValiImg(){
var img=document.getElementById("valiImg");
img.src="${pageContext.request.contextPath}/valiImg.jpg?x="+new Date().getTime();
}
</script>
</head>
<body onload="changeValiImg()">
<centr>
<h1>管理员登陆</h1>
<form action="account.do?m=login" method="post">
账户:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
验证码:<input type="text" name="valicode" style="width:60px"/><img onclick="changeValiImg()" id="valiImg" src="" title="点击换一张" style="cursor:pointer"/><br/>
<input type="submit" value="登陆"/>
</form>
<a href="account.do?username=xxx">请求</a>
</centr>
</body>
</html>
import java.awt.Color;
import java.awt.Font;
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 javax.servlet.http.HttpSession;
import javax.swing.JPopupMenu;
import cn.owntt.tool.StrTool;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 验证码图片生产类
* @author leaf
*
*/
public class ValiImgServlet extends HttpServlet{
int width=60;
int height=20;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//带有缓冲的图形对象
BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取画笔工具
Graphics g=bufferedImage.getGraphics();
//绘制背景
g.setColor(new Color(0x800080));
g.fillRect(0, 0, width, height);
//绘制边框
g.setColor(Color.blue);
g.drawRect(0, 0, width-1, height-1);
//获取随机验证码
String valiCode=StrTool.createRDM(4);
Random random=new Random();
//绘制验证码
g.setColor(Color.green);
g.setFont(new Font("Tekton Pro",Font.PLAIN,18));
String str=valiCode.substring(0,1);
g.drawString(str,8,17);
str=valiCode.substring(1,2);
g.drawString(str,20,15);
str=valiCode.substring(2,3);
g.drawString(str,35,18);
str=valiCode.substring(3,4);
g.drawString(str,45,15);
//随机产生15个干扰点
for (int i = 0; i < 15; i++) {
int x=random.nextInt(width);
int y=random.nextInt(height);
g.drawOval(x, y, 1, 1);
}
//释放画笔
g.dispose();
//将验证码存入session
HttpSession session=req.getSession(true);
session.setAttribute("valiCode", valiCode);
//将图形输出到客户端
ServletOutputStream sos=resp.getOutputStream();
//使用图形编码工具
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(sos);
encoder.encode(bufferedImage);
}
}
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>管理员登陆-同途房产</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function changeValiImg(){
var img=document.getElementById("valiImg");
img.src="${pageContext.request.contextPath}/valiImg.jpg?x="+new Date().getTime();
}
</script>
</head>
<body onload="changeValiImg()">
<centr>
<h1>管理员登陆</h1>
<form action="account.do?m=login" method="post">
账户:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
验证码:<input type="text" name="valicode" style="width:60px"/><img onclick="changeValiImg()" id="valiImg" src="" title="点击换一张" style="cursor:pointer"/><br/>
<input type="submit" value="登陆"/>
</form>
<a href="account.do?username=xxx">请求</a>
</centr>
</body>
</html>
相关文章推荐
- 两种方式实现类似qq搜索的切换
- C++静态局部对象
- linux sysfs下创建文件
- 上传excel文件
- PL/SQL Developer 导出csv文件,用excel打开中文显示乱码
- 散列表的查找代码实现
- C语言源字符集与执行字符集
- C#中as是什么含义?is是什么含义? is与as运算符
- java,数据类型
- 两个乒乓球队进行比赛,各出3人。
- HDU 5273 Dylans loves sequence
- ZABBIX作集中式NGINX性能监控的注意要点
- 这题实在不知道起啥名好了
- 各种集合遍历
- 为ThinkPad T420增加一根4G内存
- leetcode - Divide Two Integers
- PHP获取音频文件的相关信息
- NSNotificationCenter和消息推送的学习
- iOS UICollectionViewContoller相关
- Bootloader