Generate authentication code Servlet
Abstract: generate authentication code Servlet
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="476" height="86" align="center" valign="top">
Servlet generate authentication code, and Figure:
/ / Fig. 5.6_02: ImageCodeMakerServlet.java / / read image files and generate the code of Servlet package com.fatcat.webchart; import javax.servlet .*; import javax.servlet.http .*; import java.io. *; import java.util .*; import java.awt.image .*; import java.awt .*; import javax.imageio .*; public class ImageCodeMakerServlet extends HttpServlet (String codeNumbers = ""; int width = 240, height = 60; / / HTTP get requests public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException (/ / empty buffer response.reset (); / / MIME type of attention here response.setContentType ( "image / png "); / / Set the page does not cache response.setHeader (" Pragma "," No-cache "); response.setHeader (" Cache-Control "," no-cache "); response.setDateHeader (" Expires ", 0) / / create a 240 X60 Image BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); / / graphics environment be targeted g Graphics g = image.getGraphics (); / / fill background g.setColor (Color.WHITE); g.fillRect (0, 0, width, height); for (int i = 0; i <4; i + +) (drawCode (g, i, request);) / / drawNoise (g, 30 ); / / Draw borders g.setColor (Color.black); g.drawRect (0, 0, width - 1, height - 1) / / Save the code into session in the HttpSession session = request.getSession (true ); session.setAttribute ( "codeNumbers" codeNumbers); / / Reset codeNumbers string = "" / / write use ImageIO category of image coding method ServletOutputStream sos = response.getOutputStream (); ImageIO.write (image , "PNG" sos); sos.close ();) / / loading image files and the mapping of the code public void drawCode (Graphics graphics, int i, HttpServletRequest request) (int number = (int) (Math.random ( ) * 10); String imageFilePath =
Request.getRealPath ( "\ \ images \ \" + + number. "Gif"; File imageFile = new File (imageFilePath); Image gifFile = null; try gifFile = (ImageIO.read (imageFile);) catch (Exception e) (System.out.println (e);) graphics.drawImage (gifFile, i * 60, 0, null); codeNumbers + = number;) / / Line Drawing interference public void drawNoise (Graphics graphics, int lineNumber) (graphics. setColor (Color.YELLOW) for (int i = 0; i <lineNumber; i + +) (int pointX1 = 1 + (int) (Math.random () * width); int pointY1 = 1 + (int) (Math. random () * height); int pointX2 = 1 + (int) (Math.random () * width); int pointY2 = 1 + (int) (Math.random () * height); graphics.drawLine (pointX1, pointY1 , pointX2, pointY2);)) / / HTTP post request, and the same public void doPost doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException (doGet (request, response);)) /******* ************************************************** ***************** * (C) Copyright 2004-2005 by Jingkui Zhong (Beijing Zhong Kui) and Huan Tang (Tanghuan) * All Rights Reserved. * * * * DISCLAIMER: The authors of this code have used their * * best efforts in preparing the code. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these codes. The authors * * shall not be liable in any event for incidental or consequential * * damages in connection with, or arising out of, and the furnishing, * * performance, or use of these programs. * The following ******************************************/
Please download other documents.
</ Td> <td width="208" valign="top"> </ td> </ tr> <tr> <td height="20" colspan="2">
</ Td> </ tr> </ table>
</ Td> </ tr> <tr>
↑ Back
Tags: java code






