import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import java.io.IOException; import javax.servlet.http.HttpSession; import java.awt.image.BufferedImage; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.util.Random; import javax.imageio.ImageIO; import com.sun.image .codec.jpeg.jpegimageEncoder; import com.sun.image.codec.jpeg.jpegcodec; import javax.servlet.servletOutputStream;
Public class checkcode extends httpservlet {private font imgfont = new font ("Song", font.bold, 16); // Set font
Public void doget (httpservletRequest request, httpservletResponse response) throws servletexception, oException {dopost (request, response);}
Public void dopost (httpservletRequest request, httpservletResponse response) throws servletexception, ioException {string session_name = request.getParameter ("session_name");
String vcode = ""; int tent inTcode = 0;
INTCODE = (new random ()). Nextint (9999); if (intcode <1000) INTCODE = 1000; Vcode = INTCODE "
/ * * Drawing * / int width = 50; int height = 20; response.setContentType ( "image / gif"); ServletOutputStream vout = response.getOutputStream (); BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB) Graphics graph = image.getgraphics (); // Get graphics context
// Set background color graph.setColor (color.White); graph.FillRect (1, 1, 48, 18);
// Display the authentication code to the image // set font color graph.setColor (color.black); graph.setfont (imgfont); graph.drawstring (vcode, 10, 15); // char C; // for (INT i = 0; i <4; i ) // {// c = vcode.Charat (i); // graph.drawstring (c "", 9 * i 4, 16); //} // Image takes effect //graph.dispose (); // Output image to page //Imageio.write (Simage, "JPEG", response.getOutputStream ()); JPEGIMAGEENCODER Encoder = JPEGCODEC.CREATEJPEGENCODER (VOUT); Encoder.Encode (image);
// Assignment IF to the session (session_name == null ||) {session_name = "checkcode";} system.out.println ("session_name:" session_name); httpsession session = request.getSession True); session.removettribute (session_name); session.setttribute (session_name, vcode);
Vout.close ();
}