Servlet和Image如何做出一个验证码程序的完整版的源码?

马克-to-win: 根据上面程序,我做了一个验证码程序的大概。所有的技术点都已经涵盖了。有兴趣的同学可以自己把完整版的验证码程序做出来。

例:4.4.4

<%@ page contentType="text/html; charset=GBK"%>
    <script  type="text/javascript">
        function flushcode(){
            var fName=document.getElementById("code").alt;
   //         alert(fName);
            document.getElementById("code").src="MarkToWinServlet"+"?Name="+fName+Math.random();
        }
    </script>
<img id="code" src="MarkToWinServlet" alt="马克-to-win"/>
<button οnclick="flushcode()">刷新</button>

 

package com;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
public class ServletHello1 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        BufferedImage image = new BufferedImage(350, 30,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, 350, 30);
        Random r = new Random();
        g.setColor(Color.red);
        for (int i = 0; i < 200; i++) {
            /* @param n the bound on the random number to be returned. Must be
             * positive.
             */
            int x = r.nextInt(350);
            int y = r.nextInt(30);
            /* the following draw a point instead of a line. */
            g.drawLine(x, y, x, y);
        }     

更多内容请见原文,原文转载自:https://blog.csdn.net/qq_44594371/article/details/103167165

 

posted @   malala  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示