JSP code generation program and calling method

Source: Internet
Author: User

JSP tutorial Verification Code generation program and calling method
Package com.city.sxzlc.servlet;

Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import Java.util.Random;

Import Javax.imageio.ImageIO;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Authenticationcode extends HttpServlet {


Public Authenticationcode () {
Super ();
}


public void Destroy () {
Super.destroy (); Just puts "destroy" string in log
Put your code here
}


public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

Verification Code
int width=70;
int height=20;
Integer[] A = new integer[4];
String randstr= "";
Random Random = new Random ();
Color c = new color (34,56,78);
for (int i=0;i<4;i++)
A[i] = Random.nextint (10);
for (Integer ele:a)
Randstr+=ele;
System.out.println (RANDSTR);


try{
Response.setcontenttype ("Image/jpeg");
OutputStream out = Response.getoutputstream ();
BufferedImage image = new BufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_3BYTE_BGR); Feredimage (int width, int height, int imagetype)
Graphics g = image.getgraphics ();
G.fillrect (1, 1, width, height);
G.setcolor (c);

int i=5;
I=5+random.nextint (20);
for (Integer ele:a) {//one painting
g.DrawString (Ele.tostring (), I+random.nextint (5), 10+random.nextint (10));
I=i+8;
}

Imageio.write (image, "JPEG", out);
}catch (Exception e) {e.printstacktrace ();}
Request.getsession (). setattribute ("Authcode", randstr);
}


public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

This.doget (request, response);
}


public void Init () throws Servletexception {

}

}


%>

<servlet>
<description>this is the description i-Java component</description>
<display-name>this is the display name of my Java EE component</display-name>
<servlet-name>AuthenticationCode</servlet-name>
<servlet-class>com.city.sxzlc.servlet.AuthenticationCode</servlet-class>
</servlet>

<!--Verification Code mapping-->
<servlet-mapping>
<servlet-name>AuthenticationCode</servlet-name>
<url-pattern>/login/AuthenticationCode</url-pattern>
</servlet-mapping>

Call method

<tr><td> Verification Code </td><td><input name= "Authcode" size=5></td><td></td></tr>

Related Article

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.