Random.jsp file code: <% @ Page Autoflush = "false" import = "java.awt. *, java.awt.codec.jpeg. *, java.util. *" %> <% @ page import = "org.apache.commons.lang.RandomStringUtils"%> <% RandomStringUtils rs = new RandomStringUtils (); String random = rs.randomAlphanumeric (4); session.setAttribute ( "random", random );%> <%
Out.clear (); response.setContentType ("image / jpeg"); response.addheader ("prgma", "no-cache"); response.addheader ("cache-control", "no-cache"); response .addDateHeader ( "Expries", 0); int width = 100, height = 40; BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics ();? // fill the background color G.SetColor (color.gray); font defont = new font ("Sansserif", Font.Plain, 32); G.SetFont (Defont); g.FillRect (0, 0, width, height); //? ? font color g.setColor (Color.RED); g.drawString (random, 3,30); g.dispose (); ServletOutputStream outStream = response.getOutputStream (); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (outStream); encoder. Encode (image); outstream.close ();%> Test.jsp file code as follows: <% @ Page ContentType = "text / html; charset = UTF-8" Language = "Java" Import = "java.sql. * "ErrorPage =" "%>
Import java.util.random;
Public class randomstringutils {
Private static final random random = new random ();
Public randomstringutils () {}
Public Static String Random (Int Count) {Return Random (Count, False, False);
Public Static String Randomascii (Int Count) {Return Random (count, 32, 127, false, false);}
Public Static String Randomalphabetic (int count) {Return Random (count, true, false);}
Public Static String RandomalPhanumeric (Int Count) {Return Random (count, true, true);}
Public Static String Randomnumeric (int count) {Return Random (count, false, true);}
Public Static String Random (Int Count, Boolean Letters, Boolean Numbers) {Return Random (count, 0, 0, letters, numbers);
Public Static String Random (int COUNT, INT Start, Int End, Boolean Letters, Boolean Numbers) {Return Random (Count, Start, End, Letters, NumBers, NULL)
Public Static String Random (int COUNT, INT Start, INT End, Boolean Letters, Boolean Numbers, Char Set []) {IF (START == 0 && End == 0) {end = 122; start = 32; if (! Letters &&! Numbers) {start = 0; end = 0x7fffffff;}} StringBuffer buffff;}} StringBuffer buffer = new stringbuffer (); int gaple = end - start; while (count--! = 0) {char ch; if (set == null CH = (CHAR); Else Ch = set [Random.Nextint (GAP) Start]; IF (Letters && Numbers && Character.isletterOrdigit (CH) || Letters && Character. Isletter (CH) || NumBers && Character.Indigit (CH) ||! letters &&! Numbers) Buffer.Append (ch); else count ;} return buffer.tostring ();} public static string random (int count, string Set) {Return Random (Count, SET.TOCHARARRA y ());
Public Static String Random (int count, char set []) {Return Random (count, 0, set.length - 1, false, false, set)
}