package com;

 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', '5', '6', '7', '8',
'9', '0', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'K', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z' }; public static Random random = new Random(); public static String getRandomString() {// 随机获取6位
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 6; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);// 随机获取一个字符 }
return buffer.toString();
} public static Color getRandomColor() {
return new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
} public static Color getReverseColor(Color c) {// 取反色
return new Color(255 - c.getRed(), 255 - c.getGreen(),
255 - c.getBlue()); } public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
String randomString = getRandomString();
request.getSession(true).setAttribute("randomString", randomString);
int width = 100;
int height = 30;
Color color = getRandomColor();
Color reverseColor = getReverseColor(color);
BufferedImage bImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bImage.createGraphics();
graphics2d.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
graphics2d.setColor(color);
graphics2d.fillRect(0, 0, width, height);
graphics2d.setColor(reverseColor);
graphics2d.drawString(randomString, 18, 20);
for (int i = 0, n = random.nextInt(100); i < n; i++) {
graphics2d.drawRect(random.nextInt(width), random.nextInt(height),
1, 1); }
ServletOutputStream outputStream = response.getOutputStream();
JPEGImageEncoder coder = JPEGCodec.createJPEGEncoder(outputStream);
coder.encode(bImage);
outputStream.flush();
} }

需要注意的是在myEclipse中处理图片,需要引入两个包:
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
报错:
Access restriction: The type JPEGImageEncoder is not accessible due to
restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar

此时解决办法:
MyEclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated
 and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。

<%@ 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%>"> <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> <body> <img src = "servlet/IdentityServlet" id = "identity" onload = "btn.disabled=false;"/>
<input type = button value="next" onclick="reloadImage()" id = "btn"/>
</body> <script>
function reloadImage(){
document.getElementById('btn').disabled = true;
document.getElementById('identity').src = 'servlet/IdentityServlet?ts='+new Date().getTime(); }
</script>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servlet</display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>IdentityServlet</servlet-name>
<servlet-class>com.IdentityServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>IdentityServlet</servlet-name>
<url-pattern>/servlet/IdentityServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

JSP验证码。的更多相关文章

  1. JSP验证码

    ImageServlet.java package cn.hist.test.servlet; import java.awt.Color; import java.awt.Font; import ...

  2. jsp验证码 (通过单击验证码或超链接换验证码)

    #code.jsp <%@ page language="java" import="java.util.*" import="java.awt ...

  3. jsp验证码页面笔记

    首先在网上搜了下jsp生成验证码的代码,如下: package com.servlet; import java.awt.Color; import java.awt.Font; import jav ...

  4. jsp验证码点击刷新

    <img src="<%=basePath%>manage/code" alt="验证码" height="20" ali ...

  5. 生成jsp验证码的代码详解(servlet版)

    package util; import java.util.*; import java.io.*; import java.awt.*; import java.awt.image.*; impo ...

  6. jsp 验证码

    <%@page import="java.awt.Graphics2D"%> <%@page import="java.util.Random" ...

  7. web项目 验证码 *** 最爱那水货

    1. jsp代码 : <Script> function changeImg(){ document.getElementById("certImg").src =&q ...

  8. 手把手教你做JavaWeb项目:登录模块

    现如今,无论是客户端还是移动端,无论是游戏登陆还是社交平台登陆,无处不在的“登陆”.那么你知道怎么制作吗?今天就为你娓娓道来: 用户登录 在各大信息管理系统中,登录功能是必不可少的,他的作用就是验证用 ...

  9. jsp页面验证码(完整实例)

    项目结构如下,MyEclipse中新建一个Web Project,取名servlet 1.src下new一个servlet类 package com.servlet; import java.awt. ...

随机推荐

  1. MT【12】三点坐标求面积

    $L_1,L_2$是O发出的两条射线,C是一个常数,一条动直线$l$分别与$L_1,L_2$交于A,B两点.$S_{\Delta ABC}=C$,求A,B的中点D的轨迹方程.(2012北大自主招生) ...

  2. Android 视频 教程 源码 电子书 网址

    资源名称 资源地址 下载量 好评率8天快速掌握Android视频教程67集(附源码)http://down.51cto.com/zt/2197 32157Android开发入门之实战技巧和源码 htt ...

  3. BZOJ3730 震波 | 动态点分治

    #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...

  4. [hdu3466]Proud Merchants

    题目描述 Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and po ...

  5. numpy 从入门到遗忘

    不常用的函数总是遗忘,很是困扰啊.于是痛下时间,做一个系统的总结,纯原创,都是些实际项目中常用的函数和方法,当然还有一些这边也是没有记录的,因为我在实际数据处理过程中也没有遇到过(如字符串处理等等). ...

  6. [luogu3197][越狱]

    luogu3197 思路 看了很久没思路,看了题解发现自己好zz.用全部的情况减去不合法的情况就行了.全部的情况就是每个人随便选,总共有\(m^n\)种情况,然后考虑不合法的情况,也就是任意相邻的两个 ...

  7. ModuleNotFoundError: No module named 'Crypto.Cipher'

    ModuleNotFoundError: No module named 'Crypto.Cipher'报错问题 原因及处理: 在使用python3是经常会用到import一个第三方库,但是有时候会提 ...

  8. 什么是 Spring?

    感想: 我在写这个东西的时候看了不同的视频,和不同的书,关于对于spring的讲解,感觉黑马的培训视频,是讲的更加的让人容易理解. 这段时间因为各种的事情,没有写过博客了,曾经做的笔记有的在有道云笔记 ...

  9. 原生ajax写法

    ajax1.0,ie9及以下,ie10+支持ajax2.0 ajax2.0比ajax1.0: 1.多了FormData对象,xhr.send(formData)  //不能用GET 2.文件上传,文件 ...

  10. 多目标遗传算法 ------ NSGA-II (部分源码解析)两个个体支配判断 dominance.c

    /* Domination checking routines */ # include <stdio.h> # include <stdlib.h> # include &l ...