Just now the Internet bar has no machine, I put a pupil under the computer, I forced on the aircraft. , he said to put my head on the keyboard, tease, hahaha haha, on the basis of
Shsjxhjsnwndkkclswkwjksjhskxkckxkskwnnncmcbsb
The following code can be pasted and copied to any project used.
Login + Verification code.
The verification code generation is a separate servlet that is intercepted via web. Xml.
It is easy to paste the Web. XML Configuration Code First.
<servlet>
<servlet-name>/CheckCode</servlet-name>
<servlet-class> com.wswhr.loginserver.servlet.checkcode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>/CheckCode</servlet-name>
<url-pattern>/CheckCode</url-pattern>
</servlet-mapping>
Then, this is the Checkcode code.
Package com.wswhr.LoginServer.servlet;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Javax.imageio.ImageIO;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletOutputStream;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import Cn.com.daxtech.framework.security.UidPwdAuthenticationFilter;
@SuppressWarnings ("Serial") public class Checkcode extends HttpServlet {private static int WIDTH = 60;
private static int HEIGHT = 20; public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {Ht
Tpsession session = Request.getsession ();
Response.setcontenttype ("Image/jpeg");
Servletoutputstream SOS = Response.getoutputstream (); Set browser do not cache this picture responsE.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
Creates a memory image and obtains its graphical context bufferedimage image = new BufferedImage (WIDTH, HEIGHT, Bufferedimage.type_int_rgb);
Graphics g = image.getgraphics ();
Generate random authentication code char[] Rands = Generatecheckcode ();
Generate Image Drawbackground (g);
Drawrands (g, rands);
End the drawing process of the image and complete the image g.dispose ();
Output the image to the client bytearrayoutputstream BOS = new Bytearrayoutputstream ();
Imageio.write (Image, "JPEG", BOS);
byte[] buf = Bos.tobytearray ();
Response.setcontentlength (buf.length);
The following statement can also be written as: Bos.writeto (SOS);
Sos.write (BUF);
Bos.close ();
Sos.close ();
Deposit the current verification code into Session session.setattribute ("Validatecode", New String (Rands));
Session.setattribute (Uidpwdauthenticationfilter.validate_code, New String (Rands));
Using the following code directly will have a problem, the Session object must get//request.getsession () before submitting the response. SetAttribute ("Check_code", New String (Rands)); } private char[] GEneratecheckcode () {//defines the character descriptor of the captcha string chars = "2345678abcdefghjkmnprstuwxz";
char[] Rands = new Char[4];
for (int i = 0; i < 4; i++) {int rand = (int) (Math.random () * chars.length ());
Rands[i] = Chars.charat (rand);
} return rands;
} private void Drawrands (Graphics g, char[] rands) {g.setcolor (color.black); G.setfont (new Font (NULL, Font.Italic |
Font.Bold, 18));
Each character g.drawstring ("" + rands[0], 1, 17) of the output verification code at different heights;
g.DrawString ("" + rands[1], 16, 15);
g.DrawString ("" + rands[2], 31, 18);
g.DrawString ("" + rands[3], 46, 16);
System.out.println (Rands);
} private void Drawbackground (Graphics g) {//Draw background G.setcolor (new Color (0xDCDCDC));
G.fillrect (0, 0, WIDTH, HEIGHT);
Randomly generates 120 interference points for (int i = 0; i < i++) {int x = (int) (Math.random () * WIDTH);
int y = (int) (Math.random () * HEIGHT);
int red = (int) (Math.random () * 255);
int green = (int) (Math.random () * 255); int blue = (int) (Math. Random () * 255);
G.setcolor (New Color (red, green, blue));
G.drawoval (x, y, 1, 0);
}
}
}
Finally, the JSP code that uses the CAPTCHA, and the JS code
<label>
<span>
<span class= "lbl input-icon input-icon-right" >
<input id= " Validatecode "Name=" Validatecode "type=" text "class=" Span12 "maxlength=" "placeholder=" Verification Code "/>
<i class=" Icon-retweet "></i>
</span>
<span class=" LBL ">
<a href= "#" onclick= " Reloadcheckcode () "data-action=" Reload ">
<i class=" Icon-refresh "></i>
</a>
</span>
</span>
</label>
The JS code is as follows:
function Reloadcheckcode () {
document.getElementById ("Yzmpic"). src = $ ("#ctx"). Val () + "/checkcode?r=" + New Date () . GetTime ();
}
The last r= new Date (). GetTime (); Equivalent, generate a random number, avoid caching, otherwise, in the servlet, will always generate the same verification code (not without a build, but always generate the same.) )
<input type= "hidden" id= "CTX" value= "${pagecontext.request.contextpath}"/>