JSP验证码。
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验证码。的更多相关文章
- JSP验证码
ImageServlet.java package cn.hist.test.servlet; import java.awt.Color; import java.awt.Font; import ...
- jsp验证码 (通过单击验证码或超链接换验证码)
#code.jsp <%@ page language="java" import="java.util.*" import="java.awt ...
- jsp验证码页面笔记
首先在网上搜了下jsp生成验证码的代码,如下: package com.servlet; import java.awt.Color; import java.awt.Font; import jav ...
- jsp验证码点击刷新
<img src="<%=basePath%>manage/code" alt="验证码" height="20" ali ...
- 生成jsp验证码的代码详解(servlet版)
package util; import java.util.*; import java.io.*; import java.awt.*; import java.awt.image.*; impo ...
- jsp 验证码
<%@page import="java.awt.Graphics2D"%> <%@page import="java.util.Random" ...
- web项目 验证码 *** 最爱那水货
1. jsp代码 : <Script> function changeImg(){ document.getElementById("certImg").src =&q ...
- 手把手教你做JavaWeb项目:登录模块
现如今,无论是客户端还是移动端,无论是游戏登陆还是社交平台登陆,无处不在的“登陆”.那么你知道怎么制作吗?今天就为你娓娓道来: 用户登录 在各大信息管理系统中,登录功能是必不可少的,他的作用就是验证用 ...
- jsp页面验证码(完整实例)
项目结构如下,MyEclipse中新建一个Web Project,取名servlet 1.src下new一个servlet类 package com.servlet; import java.awt. ...
随机推荐
- vs2017 C4996 错误
严重性 代码 说明 项目 文件 行 禁止显示状态错误 C4996 'strcpy': This function or variable may be ...
- Nginx多进程高并发、低时延、高可靠机制在缓存(redis、memcache)twemproxy代理中的应用
1. 开发背景 现有开源缓存代理中间件有twemproxy.codis等,其中twemproxy为单进程单线程模型,只支持memcache单机版和redis单机版,都不支持集群版功能. 由于twemp ...
- android 开发中 sdk 无法更新
现在用到android 的多个版本适配 , 换了个新环境 , 重新配置了android 的开发环境,哪想到遇到了很多小问题. 今天又遇到了 android sdk manager 无法更新的问题. ...
- numpy random
np.random.seed()用法: np.random.seed(5) print(np.random.permutation(np.array([i for i in range(9)]))) ...
- Android在初始化时弹出popwindow的方法
http://blog.csdn.net/sxsboat/article/details/7340759 Android中在onCreate()时弹出popwindow,很多人都有过类似的需求吧,但 ...
- A1078. Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- Flash:使用FileReference上传在Firefox上遇到的问题终于解决了
以前使用的是这样的一句话:var uploadURL:URLRequest = new URLRequest();uploadURL.url = "upload.asp"; 测试发 ...
- 关于pascal退出acm/icpc历史
应该是很久之前的事情了,现在我才知道,有些信息滞后了,本还以为这是最近的事情呢.而2019年noip也将取消pascal,一切成为历史, 想想初中/高中阶段整片地区的使用pascal,c/c++/ja ...
- 发送邮件时Unable to relay错误的解决方法 转
使用python的smtplib发送邮件,使用新浪的smtp服务,在本地测试正常.但把程序部署到服务器后,却连不上新浪的smtp服务器,可以ping通,但telnet 25端口失败,导致程序无法正常运 ...
- 【maven】依赖、继承、聚合
依赖: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId&g ...