java网页验证码代码

来源:互联网 发布:网络视频广告表现形式 编辑:程序博客网 时间:2024/05/17 22:20

在先前做java验证码时,是直接用代码生成图片,并把图片放在文件夹下再用网页调用图片,但其实是可以直接把图片输入到网页中的。代码如下:

package com.Servlet;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;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 IdentityServlet extends HttpServlet {public static final char[] CHARS={'2','3','4','A','B','C'};public static Random random = new Random();public static String getRandomString(){StringBuffer buffer = new StringBuffer();for(int i=0;i<4;i++){buffer.append(CHARS[random.nextInt(CHARS.length)]);}return buffer.toString();}public static Color getRandomColor(){return new Color(random.nextInt(225),random.nextInt(225),random.nextInt(225));}public static Color getReverseColor(Color c){return new Color(255-c.getRed(),255-c.getGreen(),255-c.getBlue());}public void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubresp.setContentType("image/jpeg");String randomString = getRandomString();req.getSession(true).setAttribute("randomString", randomString);int width = 100;int height = 30;Color color = getRandomColor();Color reveser = getReverseColor(color);BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = bi.createGraphics();g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));g.setColor(color);g.fillRect(0, 0, width, height);g.setColor(reveser);g.drawString(randomString, 18, 20);for(int i=0,n=random.nextInt(100);i<n;i++){g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);}ServletOutputStream out = resp.getOutputStream();JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(bi);out.flush();}}

在web.xml中配置,这部分代码就不贴出来了。格式都是那样。

jsp的代码如下,也比较简单:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title><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">-->  </head>  <script type="text/javascript">  function reloadImage(){  document.getElementById('iden').src='my/Servlet?ts=' + new Date().getTime();  }  </script>    <body>  <img src="my/Servlet" id="iden">    <button id="btn" onclick="reloadImage()">test</button>  </body></html>



0 0
原创粉丝点击