zoukankan      html  css  js  c++  java
  • java动态生成验证码


    后台代码:RandomImageServlet.java
     
    package com.zhaoran.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.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
     
    import org.apache.commons.lang3.RandomStringUtils;
     
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
    @WebServlet("/randomImageServlet.action")
    public class RandomImageServlet extends HttpServlet {
        private static final long serialVersionUID = 1921126722013879767L;
        public void doTask(HttpServletRequest request,HttpServletResponse response) throws IOException{
            int len=4,width=70,height=30;
            String randomStr=null;
            HttpSession  session=request.getSession(true);
            if(randomStr==null){
                randomStr=RandomStringUtils.random(len, true, false).toLowerCase();
                session.setAttribute("vaildcode", randomStr);
            }
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/jpeg");
            ServletOutputStream out=response.getOutputStream();
            BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics gra=image.getGraphics();
            gra.setColor(new Color(228, 230, 229));
            gra.fillRect(0, 0, width, height);
            gra.setColor(Color.red);
            gra.setFont(new Font("Lucida Sans", Font.BOLD, 20));
            Random r=new Random();
            gra.drawString(randomStr, 10, 20);
            int red=0,green=0,blue=0;
            for(int i=0;i<30;i++){
                red=r.nextInt(255);
                green=r.nextInt(255);
                blue=r.nextInt(255);
                gra.setColor(new Color(red, green, blue));
                int x=r.nextInt(width);
                int y=r.nextInt(height);
                int x1=r.nextInt(12);
                int y1=r.nextInt(12);
                gra.drawLine(x, y, x+x1,y+y1);
            }
            JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
            encoder.encode(image);
            out.close();
        }
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doTask(request, response);
        }
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doTask(request, response);
        }
    }
     
    前台代码:index.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    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%>">
        <script src="js/jquery-1.9.1.js"></script>
        <script type="text/javascript">
            function changeCode(){
                $('#codeYZ').attr('src','randomImageServlet.action?'+Math.random());
            }
     
        </script>
        <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">
      </head>
     
      <body>
        <div class="div_validcode">
            验证码:<input type="text" class="validcode1" name="validcode" id="validcode"/>
        </div>
        <div class="div_validimg">
            <div>
                <img src="randomImageServlet.action" alt="验证码" id="codeYZ" class="validimg" onclick="changeCode();"/>
            </div>
        </div>
     
      </body>
    </html>
  • 相关阅读:
    JDBCj简介
    HttpStatus各种状态
    centos7安装docker并安装jdk和tomcat(常用命令)
    使用递归算法结合数据库解析成java树形结构
    大型网站系统架构的演化
    虚拟机下克隆3个centos系统并配置IP访问网络(转载)
    docker安装方法(常见安装出错问题汇总)
    Vmware虚拟机三种网络模式详解
    谈谈渲染,玩玩nginx——前后端分离,转发请求到Tomcat的尝试
    nginx实现wap移动端和PC端业务分离
  • 原文地址:https://www.cnblogs.com/zhaoran8775/p/5521472.html
Copyright © 2011-2022 走看看