jsp servlet 验证码

package jspImageServlet;

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 java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class getImg extends HttpServlet {
 
 private Font mFont=new Font("宋体",Font.PLAIN,12);
 
 /**
  * Constructor of the object.
  */
 public getImg() {
  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 {

  String s="";
  int intCount=0;
  intCount=(new Random()).nextInt(9999);
  if(intCount<1000) intCount+=1000;
  s=intCount+"";
  
  HttpSession session=request.getSession(true);
  session.setAttribute("getImg", s);
  response.setContentType("image/gif");
  
  ServletOutputStream out=response.getOutputStream();
  BufferedImage image=new BufferedImage(35,14,BufferedImage.TYPE_INT_RGB);
  Graphics gra=image.getGraphics();
  gra.setColor(Color.yellow);
  gra.fillRect(1,1,33,12);
  gra.setColor(Color.black);
  gra.setFont(mFont);
  
  char c;
  for(int i=0;i<4;i++) {

   c=s.charAt(i);

   gra.drawString(c+"",i*7+4,11);

   }
  
  JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);

  encoder.encode(image);

  out.close();
 }

 /**
  * 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 {

  doGet(request,response);
  
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

在 Jsp 页面<img src="<url-pattern>URL</url-pattern>"> 调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值