index.jsp
<%@ 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">
-->
<script type="text/javascript">
function show(obj)
{
//alert(obj.src);
obj.src = "tt?a="+Math.random();
//alert(obj.src);
}
</script>
</head>
<body>
<form name="form1" method="post" action="bb">
验证码:
<input name="randCode" type="text"/>
<img alt="sss" src="tt" height="25" width="60" οnclick="show(this);"/>
<input name="submit" type="submit" value="submit" />
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>tt</servlet-name>
<servlet-class>com.test.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>tt</servlet-name>
<url-pattern>/tt</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>bb</servlet-name>
<servlet-class>com.test.Ceshi</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bb</servlet-name>
<url-pattern>/bb</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Ceshi .java
package com.test;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Ceshi extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("bbbb");
ServletContext context = getServletContext();
//获得当前session中的验证码
Object codeCurrent = context.getAttribute("randomNum");
//获得用户输入的验证码
String codeInput = req.getParameter("randCode");
if (codeInput.equals(codeCurrent)) {
resp.sendRedirect("same.jsp");
} else {
resp.sendRedirect("different.jsp");
}
}
}
Test.java
package com.test;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//首先设置页面不缓存
resp.setHeader("Pragma", "No-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
//定义图片的宽度和高度
int width = 90, height = 40;
//创建一个图形对象
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_BGR);
//得到图像的环境对象
Graphics g = image.createGraphics();
//用随机颜色填充图形背景
Random random = new Random();
g.setColor(getRandColor(180, 250));
g.fillRect(0, 0, width, height);
for (int i = 0; i < 5; i++) {
g.setColor(getRandColor(50, 100));
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x, y, 4, 4);
}
//设置字体 Font(String name, int style, int size)
g.setFont(new Font("", Font.PLAIN,40));
String sRand = ""; //随机字符串
//生成四个随机字符
for (int i = 0; i < 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
//生成随机颜色
g.setColor(new Color(20 + random.nextInt(80),
20 + random.nextInt(100),
20 + random.nextInt(90)));
//将随机数字画在图像上
g.drawString(rand, (17 + random.nextInt(3)) * i + 8, 34);
//生成干扰线
for (int j = 0; j < 12; j++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(9);
int y1 = random.nextInt(9);
g.drawLine(x, y, x + x1, y + y1);
}
}
ServletContext context = getServletContext();
context.setAttribute("randomNum", sRand);
//将生成的随机字符串写入session
req.getSession().setAttribute("randCode", sRand);
//使图像生效
g.dispose();
//输出图像到页面
ImageIO.write(image, "jpeg", resp.getOutputStream());
}
public Color getRandColor (int fc, int bc){
Random random = new Random();
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
int red = fc + random.nextInt(bc - fc);
int green = fc + random.nextInt(bc - fc);
int blue = fc + random.nextInt(bc - fc);
return new Color(red, green, blue);
}
}