编写一个Servlet验证码
(1)自动生成4位数字的随机验证码。(2)将生成的验证码字符串保存到会话对象中。(3)向浏览器发送纯文本格式的验证码字符串。要求:(1).写出Servlet的java源代...
(1) 自动生成4位数字的随机验证码。
(2) 将生成的验证码字符串保存到会话对象中。
(3) 向浏览器发送纯文本格式的验证码字符串。
要求:
(1).写出Servlet的java源代码。
(2) 编写Servlet的映射配置代码。
(3) 手写到作业纸上,不能使用打印文档 展开
(2) 将生成的验证码字符串保存到会话对象中。
(3) 向浏览器发送纯文本格式的验证码字符串。
要求:
(1).写出Servlet的java源代码。
(2) 编写Servlet的映射配置代码。
(3) 手写到作业纸上,不能使用打印文档 展开
展开全部
你好,代码如下:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.imageio.*;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class AuthImg extends HttpServlet
{
private Font mFont = new Font("Arial Black", Font.PLAIN, 16);
public void init() throws ServletException
{
super.init();
}
Color getRandColor(int fc,int bc)
{
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
int width=100, height=18;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(1, 1, width-1, height-1);
g.setColor(new Color(102,102,102));
g.drawRect(0, 0, width-1, height-1);
g.setFont(mFont);
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x,y,x + xl,y + yl);
}
for (int i = 0;i < 70;i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x,y,x - xl,y - yl);
}
String sRand="";
for (int i=0;i<6;i++)
{
String tmp = getRandomChar();
sRand += tmp;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(tmp,15*i+10,15);
}
HttpSession session = request.getSession(true);
session.setAttribute("rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}
private String getRandomChar()
{
int rand = (int)Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '\u0000';
switch (rand)
{
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char)itmp;
return String.valueOf(ctmp);
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char)itmp;
return String.valueOf(ctmp);
default :
itmp = Math.round(Math.random() * 9);
return String.valueOf(itmp);
}
}
}
其中这是加了干扰线的了,并且用session来保存验证码了,
在登陆的jsp页面中,应该这样设置
验证码如图:<img src="authImg" id="authImg"/>看不清?<a href="#" onClick="refresh()">单击此处刷新</a>
然后refesh()函数如下写
<script>
function refresh()
{
document.getElementById("authImg").src='authImg?now=' + new Date();
}
</script>
之所以在最后加now,是防止浏览器的缓存
最后再在web.xml里配置下servlet
<servlet>
<servlet-name>img</servlet-name>
<servlet-class>org.yeeku.web.AuthImg</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>img</servlet-name>
<url-pattern>/authImg</url-pattern>
</servlet-mapping>
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.imageio.*;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class AuthImg extends HttpServlet
{
private Font mFont = new Font("Arial Black", Font.PLAIN, 16);
public void init() throws ServletException
{
super.init();
}
Color getRandColor(int fc,int bc)
{
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
int width=100, height=18;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(1, 1, width-1, height-1);
g.setColor(new Color(102,102,102));
g.drawRect(0, 0, width-1, height-1);
g.setFont(mFont);
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x,y,x + xl,y + yl);
}
for (int i = 0;i < 70;i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x,y,x - xl,y - yl);
}
String sRand="";
for (int i=0;i<6;i++)
{
String tmp = getRandomChar();
sRand += tmp;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(tmp,15*i+10,15);
}
HttpSession session = request.getSession(true);
session.setAttribute("rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}
private String getRandomChar()
{
int rand = (int)Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '\u0000';
switch (rand)
{
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char)itmp;
return String.valueOf(ctmp);
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char)itmp;
return String.valueOf(ctmp);
default :
itmp = Math.round(Math.random() * 9);
return String.valueOf(itmp);
}
}
}
其中这是加了干扰线的了,并且用session来保存验证码了,
在登陆的jsp页面中,应该这样设置
验证码如图:<img src="authImg" id="authImg"/>看不清?<a href="#" onClick="refresh()">单击此处刷新</a>
然后refesh()函数如下写
<script>
function refresh()
{
document.getElementById("authImg").src='authImg?now=' + new Date();
}
</script>
之所以在最后加now,是防止浏览器的缓存
最后再在web.xml里配置下servlet
<servlet>
<servlet-name>img</servlet-name>
<servlet-class>org.yeeku.web.AuthImg</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>img</servlet-name>
<url-pattern>/authImg</url-pattern>
</servlet-mapping>
参考资料: http://www.cnblogs.com/jackyrong/archive/2008/05/06/1185917.html
广东轻亿云软件
2024-05-14 广告
2024-05-14 广告
广东轻亿云软件科技有限公司在软件开发领域深耕多年,积累了丰富的经验和技术实力。我们深知API接口在现代软件开发中的重要性,因此,我们与多家业界领先的API接口提供商保持着紧密的合作关系,确保我们的产品和服务能够充分利用这些接口,为用户提供更...
点击进入详情页
本回答由广东轻亿云软件提供
展开全部
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
int width = 60; //设置图片宽度
int height = 20; // 设置图片高度
//创建缓存图像
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics(); // 获取图形
g.setColor(new Color(000, 102, 153)); // 设置背景色
g.fillRect(0, 0, width, height); // 填充背景
g.setColor(new Color(000, 000, 000)); // 设置边框颜色
g.drawRect(0, 0, width - 1, height - 1); // 绘制边框
g.setFont(new Font("Arial", Font.PLAIN, 16)); //设定字体
//生成全数字的验证码
Random random = new Random(); // 生成随机类
//随机产生4 位数字验证码
StringBuffer sbRan = new StringBuffer(); //保存验证码文本
for (int i = 0; i < 4; i++) {
String ranNum = String.valueOf(random.nextInt(10));
sbRan.append(ranNum);
//将验证码绘制到图像中
g.setColor(new Color(255, 255, 255));
g.drawString(ranNum, 10 * i + 5, 16);
}
g.dispose(); //部署图像
session.setAttribute("_CODE_", str.toString()); // 将验证码保存在session 对象中供对比
ImageIO.write(image, "PNG", response.getOutputStream()); //输出图像到页面
out.clear();
out = pageContext.pushBody();
上面是主代码!获取验证码并保存在session会话里!剩下的看你的要求了....
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
int width = 60; //设置图片宽度
int height = 20; // 设置图片高度
//创建缓存图像
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics(); // 获取图形
g.setColor(new Color(000, 102, 153)); // 设置背景色
g.fillRect(0, 0, width, height); // 填充背景
g.setColor(new Color(000, 000, 000)); // 设置边框颜色
g.drawRect(0, 0, width - 1, height - 1); // 绘制边框
g.setFont(new Font("Arial", Font.PLAIN, 16)); //设定字体
//生成全数字的验证码
Random random = new Random(); // 生成随机类
//随机产生4 位数字验证码
StringBuffer sbRan = new StringBuffer(); //保存验证码文本
for (int i = 0; i < 4; i++) {
String ranNum = String.valueOf(random.nextInt(10));
sbRan.append(ranNum);
//将验证码绘制到图像中
g.setColor(new Color(255, 255, 255));
g.drawString(ranNum, 10 * i + 5, 16);
}
g.dispose(); //部署图像
session.setAttribute("_CODE_", str.toString()); // 将验证码保存在session 对象中供对比
ImageIO.write(image, "PNG", response.getOutputStream()); //输出图像到页面
out.clear();
out = pageContext.pushBody();
上面是主代码!获取验证码并保存在session会话里!剩下的看你的要求了....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询