后台验证码
来源:互联网 发布:直播音效软件下载 编辑:程序博客网 时间:2024/06/07 00:29
废话不多说,直接上代码。先看看文件的位置
ValidateCodeServlet.java
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.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class ValidataCoed */@WebServlet("/ValidataCoed")public class ValidateCodeServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ValidateCodeServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } public Color getRandomColor(int fc,int bc){ Random random = new Random(); Color randomColor = null; if(fc>255) fc=255; if(bc>255) bc=255; //设置个0-255之间的随机颜色值 int r=fc+random.nextInt(bc-fc); int g=fc+random.nextInt(bc-fc); int b=fc+random.nextInt(bc-fc); randomColor = new Color(r,g,b); return randomColor;//返回具有指定红色、绿色和蓝色值的不透明的 sRGB 颜色 } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //禁止页面缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "No-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); //设置响应正文的MIME类型为图片 int width=60, height=20; /**创建一个位于缓存中的图像,宽度60,高度20 */ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); //获取用于处理图形上下文的对象,相当于画笔 Random random = new Random(); //创建生成随机数的对象 g.setColor(getRandomColor(200,250)); //设置图像的背景色 g.fillRect(0, 0, width, height); //画一个矩形 ,坐标(0,0),宽度60,高度20 g.setFont(new Font("Times New Roman",Font.PLAIN,18)); //设定字体格式 g.setColor(getRandomColor(160,200)); for(int i=0;i<130;i++){ //产生130条随机干扰线 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); //在图象的坐标(x,y)和坐标(x+x1,y+y1)之间画干扰线 } String strCode=""; for (int i=0;i<4;i++){ String strNumber=String.valueOf(random.nextInt(10)); //把随机数转换成String字符串 strCode=strCode+strNumber; //设置字体的颜色 g.setColor(new Color(15+random.nextInt(120),15+random.nextInt(120),15+random.nextInt(120))); g.drawString(strNumber,13*i+6,16); //将验证码依次画到图像上,坐标(x=13*i+6,y=16) } request.getSession().setAttribute("Code",strCode); //把验证码保存到Session中 g.dispose(); //释放此图像的上下文以及它使用的所有系统资源 ImageIO.write(image, "JPEG", response.getOutputStream()); //输出JPEG格式的图像 response.getOutputStream().flush(); //刷新输出流 response.getOutputStream().close(); //关闭输出流 }}
配置的XML是在上一篇的基础上进行的配置:
<?xml version="1.0" encoding="GBK"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>MyBlog</display-name> <servlet> <servlet-name>RegServlet</servlet-name> <servlet-class>servlet.RegServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RegServlet</servlet-name> <url-pattern>/RegServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>ValidateCodeServlet</servlet-name> <servlet-class>servlet.ValidateCodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ValidateCodeServlet</servlet-name> //这里的url将会是后面验证码的src的来源 <url-pattern>/ValidateCodeServlet</url-pattern> </servlet-mapping></web-app>
index.jsp
//在上次的基础上添加了两行代码:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!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=UTF-8"><title>府城的博客</title></head><body> <form action ="RegServlet" method="Post" onsubmit="return reg(this);"> <table align="center" border="0", width="500"> <tr> <td align ="right" width ="30%">用户名:</td> <td><input type ="text" name ="username" class="box"></td> </tr> <tr> <td align ="right" width ="30%">密码:</td> <td><input type ="text" name ="password" class="box"></td> </tr> <tr> <td align ="right" width ="30%">性别:</td> <td><input type ="radio" name ="sex" value="男" checked="checked">男 <input type ="radio" name ="sex" value="女" >女</td> </tr> <tr> <td align ="right" width ="30%">密码找回问题:</td> <td><input type ="text" name ="question" class="box"></td> </tr> <tr> <td align ="right" width ="30%">密码找回答案:</td> <td><input type ="text" name ="answer" class="box"></td> </tr> <tr> <td align ="right" width ="30%">邮箱:</td> <td><input type ="text" name ="email" class="box"></td> </tr> <tr> <td>验证码:</td> <td> <img alt="" src="ValidateCodeServlet" > </td> </tr> <tr> <td>输入验证码:</td> <td> <input type="text" name="code"/> </td> </tr> <tr> <td colspan="2" align="center" height="40"> <input type ="submit" value="注册"> <input type ="reset" value="重置"> </td> </tr> </body></html>
然后就成功了
这里我没有加验证输入的正确性,很简单的逻辑就不说了。以及如何将四位数字验证码变成字母加数字,无非是ASCLL码的范围。
0 0
- java后台产生验证码后台验证
- 验证码后台调用
- 后台验证码
- 后台验证码生成
- 后台生成验证码
- php生成验证码、后台验证方法
- 验证码的后台代码
- dedecms后台验证码问题
- PHPCMS后台验证码问题
- C#后台生成验证码
- 验证码 web服务器后台报错
- 管理后台不显示验证码功能
- dede 后台验证码错误解决方法
- c# web 后台生成验证码
- 关闭 PHPCMS 后台登录验证码
- dede后台登陆无法显示验证码
- dedecms 后台登陆去掉验证码
- 织梦后台验证码不显示
- nw-git配合beyond的使用
- 经典js问题
- Exception in thread "main" org.apache.hadoop.mapred.InvalidJobConfException: Output directory not se
- 安卓自定义控件收集整理
- 字符串计数-求复合条件字典序个数
- 后台验证码
- 小米考试(电话号码)
- c
- UE4 Texture Streaming Pool 运行时红色显示关闭
- Leetcode 312. Burst Balloons[hard]
- FFmpeg视频转码技巧之二次编码
- 小甲鱼python视频教程053~055论一只爬虫的自我修养
- ds1302通信
- 原始的分享-菌-003--软件系列的说