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>