您的位置:首页 > 其它

验证码图片生成以及页面取得

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: