Servlet Verification Code Generation

Source: Internet
Author: User

Servlet

Package com.htpo.net;

Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
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 Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Testget extends HttpServlet {

/**
* Constructor of the object.
*/
Public Testget () {
Super ();
}

public static final char[] CHARS = {' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ',
' 9 ', ' 0 ', ' A ', ' Q ', ' w ', ' E ', ' R ', ' T ', ' g ', ' D ', ' S ', ' w ', ' G ',
' H '};//A dictionary of random characters
public static random random = new random ();//random number

public static String getrandomstring () {
Caching of characters
StringBuffer buf = new StringBuffer ();
for (int i = 0; i < 6; i++) {//Loop six times
Buf.append (Chars[random.nextint (chars.length));
}
return buf.tostring ();
}

public static Color Getrandomcolor () {
return new Color (Random.nextint (255), Random.nextint (255),
Random.nextint (255));
}

public static color Getreversecolor (color c) {
return new Color (255-c.getred (), 255-c.getgreen (),
255-c.getblue ());
}

/**
* 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 was called when a form had 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 {

/*
* Response.setcontenttype ("text/html"); String re =
* Request.getparameter ("username"); System.out.println (re); PrintWriter
* out = Response.getwriter ();
*
*//Response.sendredirect ("test.jsp"); String s =
* "[{id:1, pid:0, Name:" Pnode
* 1 ", open:true},{id:11, pid:1, Name:" Pnode
* One "},{id:111, Pid:11, Name:" Leaf node
* 111 "},{id:112, Pid:11, Name:" Leaf node
* "},{id:113, Pid:11, Name:" Leaf node
* 113 "},{id:114, Pid:11, Name:" Leaf node
* "},{id:12, Pid:1, Name:" Pnode "},{id:121, Pid:12, Name:" Leaf
* Node 121 "},{id:122, Pid:12, Name:" Leaf node
* 122 "},{id:123, Pid:12, Name:" Leaf node
* 123 "},{id:124, Pid:12, Name:" Leaf node
* 124 "},{id:13, Pid:1, Name:" Pnode 13-no
* Child ", isparent:true},{Id:2, pid:0, Name:" Pnode
* 2 "},{id:21, Pid:2, Name:" Pnode
* + ", open:true},{id:211, pid:21, Name:" Leaf node
* 211 "},{id:212, pid:21, Name:" Leaf node
* 212 "},{id:213, pid:21, Name:" Leaf node
* 213 "},{id:214, pid:21, Name:" Leaf node
* 214 "},{id:22, Pid:2, Name:" Pnode "},{id:221, pid:22, Name:" Leaf
* Node 221 "},{id:222, pid:22, Name:" Leaf node
* 222 "},{id:223, pid:22, Name:" Leaf node
* 223 "},{id:224, pid:22, Name:" Leaf node
* 224 "},{id:23, Pid:2, Name:" Pnode "},{id:231, pid:23, Name:" Leaf
* Node 231 "},{id:232, pid:23, Name:" Leaf node
* 232 "},{id:233, pid:23, Name:" Leaf node
* 233 "},{id:234, pid:23, Name:" Leaf node
* 234 "},{Id:3, pid:0, Name:" Pnode 3-no Child ", isparent:true}]";
* Out.write (s);
*
* OUT.PRINTLN (
* "<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ");
* OUT.PRINTLN ("<HTML>");
* OUT.PRINTLN ("* OUT.PRINTLN ("<BODY>"); Out.print ("This is");
* Out.print (This.getclass ()); Out.println (", using the GET method");
* OUT.PRINTLN ("</BODY>"); Out.println ("</HTML>");
*
* Out.flush (); Out.close ();
*/
String s = request.getparameter ("username");
System.out.println (s);
Response.setcontenttype ("Image/jpeg");//Set the type of output
String randomstring = getrandomstring ();//Gets the character set returned
Request.getsession (True). SetAttribute ("RandomString", randomstring);
int with = 100;
int hight = 30;//the size of the resulting picture
Color color = Getrandomcolor ();//For background color
Color reverse = getreversecolor (color);//For foreground color
BufferedImage bi = new BufferedImage (with, hight,
BUFFEREDIMAGE.TYPE_INT_RGB);//Create a color picture
Graphics2D g = bi.creategraphics ();//Get to drawing object
G.setfont (New Font (FONT.SANS_SERIF, Font.Bold, 16));//Set Font
G.setcolor (color);//Set color
G.fillrect (0, 0, with, hight);//Draw background
G.setcolor (reverse);//Set color
g.DrawString (RandomString, 18, 20);//Draw random characters
for (int i = 0, n = random.nextint (+), I < n; i++) {//Draw up to 100 noise points
G.drawrect (Random.nextint (with), Random.nextint (hight), 1, 1);//random noise point
}
Servletoutputstream out = Response.getoutputstream ();
Convert picture format
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (BI);//encode a picture
Out.flush ();//Output
}

/**
* The DoPost method of the servlet. <br>
*
* This method was called when a form have 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
*/

/**
* Initialization of the servlet. <br>
*
* @throws servletexception
* If an error occurs
*/
public void Init () throws Servletexception {
Put Your code here
}

}
---------------------------------------

Jsp

function Reloadimage () {
Document.getelementbyidx_x_x ("Btn"). Disabled=false;
Document.getelementbyidx_x_x ("Identity"). src= ' testget?ts= ' +new Date (). GetTime ();

}

function Yanzheng () {


}

</script>


<input type= "button" id= "btn" value= "Change Picture" onclick= "reloadimage ()"/>

Servlet Verification Code Generation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.