我这里有一套你应该能够用得上 但是附件上不来 我直接把Servlet发上来你看看package com.myt.servlet;import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;public class CheckCodeServlet extends HttpServlet { /** * Constructor of the object. */ public CheckCodeServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 设置页面不缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0);
//画验证码 int width=65; int height=20; java.awt.image.BufferedImage image=new java.awt.image.BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//最后一步,把图片输出到页面 javax.imageio.ImageIO.write(image, "jpeg", response.getOutputStream()); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }}
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;public class CheckCodeServlet extends HttpServlet { /**
* Constructor of the object.
*/
public CheckCodeServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {// 设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
//画验证码
int width=65;
int height=20;
java.awt.image.BufferedImage image=new java.awt.image.BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g.setColor(Color.yellow);
g.fillRect(0, 0, width, height);
//生成随机的验证码
java.util.Random random=new java.util.Random();
String code="";
for(int i=0;i<4;i++)
{
char c=(char)('A'+random.nextInt(26));
code+=c;
}
//存session中
HttpSession session=request.getSession();
session.setAttribute("checkcode", code);
g.setColor(Color.blue);
g.setFont(new Font("隶书",Font.BOLD,20));
g.drawString(code, 0, 18);
//添加干扰线
for(int i=0;i<10;i++)
{
g.setColor(new Color(125,125,125));
g.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height));
}
g.setColor(Color.green);
g.drawRect(0, 0, width-1, height-1);
//最后一步,把图片输出到页面
javax.imageio.ImageIO.write(image, "jpeg", response.getOutputStream());
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}}