前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Servlet和Image如何做出一个验证码程序的完整版的源码?

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

作者头像
马克java社区
修改2021-07-09 10:51:57
2730
修改2021-07-09 10:51:57
举报
文章被收录于专栏:java大数据

马克-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

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档