util包
package www.csdn.net.news.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.servlet.http.HttpSession;
public class RandomImage {
private final static int WIDTH = 100;
private final static int HEIGHT = 30;
public static Random rd = new Random();
public static BufferedImage creatImage(HttpSession session) {
// 在内存中创建图片
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_BGR);
// 得到图形上下文
Graphics g = image.getGraphics();
// 设置背景
drawBackground(g);
// 绘制边框
drawBorder(g);
// 绘制内容
// 将验证码信息发送到当前页的session中
String code=String.valueOf(drawContent(g));
System.out.println("产生验证码======="+code);
session.setAttribute("code", code);
// 绘制干扰
drawDisturb(g);
return image;
}
private static String drawContent(Graphics g) {
// TODO Auto-generated method stub
String con = "qwertyuiopasdfgjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
//用于显示
StringBuffer sb = new StringBuffer();
//用于验证
StringBuffer st=new StringBuffer();
for (int i = 0; i < 4; i++) {
int index = rd.nextInt(con.length());
char str = con.charAt(index);
sb.append(str+" ");
st.append(str);
}
g.setColor(getRandomColor(10, 100));
g.setFont(new Font("Gungsuh", Font.BOLD, 20));
g.drawString(sb.toString(), 15, 20);
System.out.println(st+"=====================");
return st.toString();
}
private static void drawDisturb(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.blue);
for (int i = 0; i < 2 * (WIDTH + HEIGHT); i++) {
int x1 = rd.nextInt(WIDTH - 10);
int y1 = rd.nextInt(HEIGHT - 10);
/*
* int x2 = rd.nextInt(WIDTH); int y2 = rd.nextInt(HEIGHT);
*/
// /g.drawLine(x1+5, y1+5, x2-5, y2-5);
g.setColor(getRandomColor(1, 255));
g.drawOval(x1 + 5, y1 + 5, 1, 1);
}
}
private static void drawBorder(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.yellow);
g.drawRect(0, 0, WIDTH - 2, HEIGHT - 2);
}
private static void drawBackground(Graphics g) {
// TODO Auto-generated method stub
g.setColor(getRandomColor(100, 255));
g.fillRect(0, 0, WIDTH, HEIGHT);
}
// 生成随机图片颜色
private static Color getRandomColor(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);
}
}
servlet代码
package www.csdn.net.news.servlet;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import www.csdn.net.news.util.RandomImage;
public class ImageServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("imgservlet第一次被执行");
// 通知浏览器以图片的方式打开
response.setContentType("image/jpeg");
// 设置浏览器不要缓存
response.setIntHeader("expires", 0);
response.setHeader("cache-control", "no-cache");
response.setHeader("pragma", "no-cache");
System.out.println("imgservlet第二次被执行");
ImageIO.write(RandomImage.creatImage(request.getSession()), "jpeg", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
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>用户登录界面</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" src="../js/jquery-2.0.3.js"></script>
<script type="text/javascript" src="./js/getXmlRequest.js"></script>
<script type="text/javascript" src="./js/login.js"></script>
</head>
<body>
<div align="center">
<div align="center">
<form action="./login.do" method="post">
<table border="1px" cellspacing="0" cellpadding="0">
<tr>
<td>用户名</td>
<td><input type="text" id="name" name="name" value=""></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" id="pass" name="pass" value=""></td>
</tr>
<tr>
<td>身份</td>
<td><input type="radio" name="permission" value="admin"
checked="checked" />管理员<input type="radio" name="permission"
value="oper" />发布员</td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" id="inputCheck" name="inputCheck"
value=""></td>
</tr>
<tr>
<td colspan="2" align="center"><img alt="验证码信息" id="code"
src="./creatImage.do" /></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit"
value="登录" id="login"> <input type="hidden" name="oper"
value="login" /></td>
</tr>
<tr>
<td colspan="3"><span id="errorMessage" style="font-size: 12px;color: red;"></span></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
web.xml文件
<?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>newsManager</display-name>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>www.csdn.net.news.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>www.csdn.net.news.servlet.ImageServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>NewsMainServlet</servlet-name>
<servlet-class>www.csdn.net.news.servlet.NewsMainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/creatImage.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>NewsMainServlet</servlet-name>
<url-pattern>/initData.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>