• 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>
  • 相关阅读:
    27、深入理解计算机系统笔记,网络编程
    29、深入理解计算机系统笔记,并发编程(concurrent)(1)
    28、深入理解计算机系统笔记,Web服务器简介
    18、Linux下编程风格
    26、深入理解计算机系统笔记,系统级I/O
    32、深入理解计算机系统笔记,Unix下处理错误的风格
    31、深入理解计算机系统笔记,并发编程(concurrent)(3)
    19、Linux编码规范
    ASP.NET 中 DropDownList+DataList(数据列表)的使用前台绑定
    ASP.NET中 DetailsView(详细视图)的使用前台绑定
  • 原文地址:https://www.cnblogs.com/zhaoran8775/p/5521472.html
Copyright © 2020-2023  润新知