我的代码如下:
//一个servlet 映射为package com.shop.servlet;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class CodeServlet extends HttpServlet {
private int width = 60;//宽度
private int height = 20;//高度
private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables
public void init() throws ServletException {
}
public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics2D g = buffImg.createGraphics();
Random random = new Random();
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
Font font=new Font("Dialog",Font.PLAIN,18);
g.setFont(font);
g.setColor(Color.BLACK);
g.drawRect(0,0,width-1,height-1);
//产生160条干扰线,图像中的认证码,不被看到
g.setColor(Color.CYAN);
for(int i = 0;i < 160;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x,y,x1,y1);
}
StringBuffer randomCode = new StringBuffer();//保存验证码
int red = 0,green = 0,blue = 0;
for(int i = 0;i < 4;i++){
String strRand = String.valueOf(random.nextInt(10));
red = random.nextInt(110);
green = random.nextInt(50);
blue = random.nextInt(50);
g.setColor(new Color(red,green,blue));
g.drawString(strRand,13*i+6,16);
randomCode.append(strRand);
}
HttpSession session = request.getSession();
session.setAttribute("code",randomCode.toString());
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
response.setContentType("image/jpeg");
//将图像输出到Servlet输出 流中
ServletOutputStream sos=response.getOutputStream();
ImageIO.write(buffImg,"jpeg",sos);
sos.close();
}
}JSp代码:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page session="false"%>
<html>
<head>
<title>
登录界面
</title>
</head>
<body bgcolor="#ffffff">
<div align="center">
<h1>
登录界面
</h1>
<form name="myform" method="POST" action="doLogin.jsp">
<table >
<tr>
<td>用户名:</td>
<td><input type="text" name="loginName" size="23"/></td>
<td></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password"name="loginPwd" size="23"/></td>
<td></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="code" size="23" /></td>
<td><img src="codeservlet" alt="验证码" id="code" onclick=""/></td>// 在这改也可以
</tr>
<tr>
<td><input type="submit" name="Submit" value=" 登 录 "><br/></td>
<td><a href="newUser.jsp">注 册 用 户</a></td>
<!-- <td><a href="codeservlet">看不清,换一个</a></td>-->//在这,怎么改..............才能单击就改变..
</tr>
</table>
</form>
</div>
</body>
</html>
//一个servlet 映射为package com.shop.servlet;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class CodeServlet extends HttpServlet {
private int width = 60;//宽度
private int height = 20;//高度
private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables
public void init() throws ServletException {
}
public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics2D g = buffImg.createGraphics();
Random random = new Random();
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
Font font=new Font("Dialog",Font.PLAIN,18);
g.setFont(font);
g.setColor(Color.BLACK);
g.drawRect(0,0,width-1,height-1);
//产生160条干扰线,图像中的认证码,不被看到
g.setColor(Color.CYAN);
for(int i = 0;i < 160;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x,y,x1,y1);
}
StringBuffer randomCode = new StringBuffer();//保存验证码
int red = 0,green = 0,blue = 0;
for(int i = 0;i < 4;i++){
String strRand = String.valueOf(random.nextInt(10));
red = random.nextInt(110);
green = random.nextInt(50);
blue = random.nextInt(50);
g.setColor(new Color(red,green,blue));
g.drawString(strRand,13*i+6,16);
randomCode.append(strRand);
}
HttpSession session = request.getSession();
session.setAttribute("code",randomCode.toString());
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
response.setContentType("image/jpeg");
//将图像输出到Servlet输出 流中
ServletOutputStream sos=response.getOutputStream();
ImageIO.write(buffImg,"jpeg",sos);
sos.close();
}
}JSp代码:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page session="false"%>
<html>
<head>
<title>
登录界面
</title>
</head>
<body bgcolor="#ffffff">
<div align="center">
<h1>
登录界面
</h1>
<form name="myform" method="POST" action="doLogin.jsp">
<table >
<tr>
<td>用户名:</td>
<td><input type="text" name="loginName" size="23"/></td>
<td></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password"name="loginPwd" size="23"/></td>
<td></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="code" size="23" /></td>
<td><img src="codeservlet" alt="验证码" id="code" onclick=""/></td>// 在这改也可以
</tr>
<tr>
<td><input type="submit" name="Submit" value=" 登 录 "><br/></td>
<td><a href="newUser.jsp">注 册 用 户</a></td>
<!-- <td><a href="codeservlet">看不清,换一个</a></td>-->//在这,怎么改..............才能单击就改变..
</tr>
</table>
</form>
</div>
</body>
</html>
楼主【East271536394】截止到2008-07-07 22:03:40的历史汇总数据(不包括此帖):
发帖的总数量:17 发帖的总分数:340
结贴的总数量:1 结贴的总分数:20
无满意结贴数:0 无满意结贴分:0
未结的帖子数:16 未结的总分数:320
结贴的百分比:5.88 % 结分的百分比:5.88 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
<img id="myimg" src="code.jsp">
<a href="javascript:changeimg()">看不清,换一张</a> <script>
function changeimg()
{
var myimg = document.getElementById("myimg");
myimg.src="code.jsp?"+Math.random();//随机生成一个数字,让图片缓冲区认为不是同一个缓冲区
}
</script>
{
document.images["rand"].src="Image" ;
} onclick="javascript:change();"