The servlet generates the verification code and outputs:
@WebServlet (name = "Yzmservlet") public class Yzmservlet extends HttpServlet {private static final int WIDTH = 100; private static final int HEIGHT = 40; protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (Request,response); } protected void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); Request.setcharacterencoding ("Utf-8"); BufferedImage image=new bufferedimage (width,height, BUFFEREDIMAGE.TYPE_INT_RGB); Graphics G=image.getgraphics (); Set Background color g.setcolor (Color.cyan); Fill background color g.fillrect (0,0,width,height); Set foreground color G.setcolor (color.black); Set the font for Fonts font=new font ("imitation", font.bold,20); G.setfont (font); Get Verification code int len=4; String ResULt= ""; for (int x=0;x<len;x++) {char c= (char) randomutils.nextint (65,91); Result=result+c; } g.drawstring (result,20,20); Set the interference line for (int i=0;i<10;i++) {int x1= randomutils.nextint (0,width); int x2= randomutils.nextint (0,width); int y1= randomutils.nextint (0,height); int y2= randomutils.nextint (0,height); Color Color=new Color (randomutils.nextint (0,255), Randomutils.nextint (0,255), Randomutils.nextint (0,255)); G.setcolor (color); Set line G.drawline (X1,Y1,X2,Y2); }//Save the acquired verification code to the session HttpSession Session=request.getsession (); Session.setattribute ("Identifycode", result); Output Image Bufferedoutputstream Bos=new Bufferedoutputstream (Response.getoutputstream ()); Imageio.write (image, "JPG", BOS); Imageio.write (image, "JPG", New File ("e:\\b.jpg")); Bos.flush (); Bos.close (); }}
The servlet validates the nickname/password/verification code entered in the login interface:
@WebServlet (name = "Identifyyzmservlet") public class Identifyyzmservlet extends HttpServlet {protected void DoPost (HTT Pservletrequest request, HttpServletResponse response) throws Servletexception, IOException {doget (request,respon SE); } protected void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); Request.setcharacterencoding ("Utf-8"); Gets the user name entered as String Username=request.getparameter ("username"); Gets the input password String password=request.getparameter ("password"); Gets the input verification code String yzm2=request.getparameter ("yzm01"); Gets the data accessed in the Session object HttpSession session1=request.getsession (); String yzm1= (String) session1.getattribute ("Identifycode"); if (!username.equals ("") &&!password.equals ("") &&!yzm2.equals ("")) {if (Username.equals ("Bighead")) {if (Password.equals ("4214963")) {if (Yzm1.equals (yzm 2)) {Response.getwriter (). println ("Login Successful"); The page//request.getrequestdispatcher ("form01.html") that jumps to after successful login. Forward (request,response); } else {Response.getwriter (). println ("The Verification code is wrong, please re-enter"); }} else {Response.getwriter (). println ("Password is incorrect, please re-enter"); }}else{Response.getwriter (). println ("Wrong nickname, please re-enter"); }}else{Response.getwriter (). println ("the nickname or password or verification code cannot be blank, please enter"); } }}
Web-xml To configure:
-<servlet><servlet-name>identifyYzmServlet</servlet-name><servlet-class>com.westos.identifyYzmServlet</servlet-class></servlet>-<servlet-mapping><servlet-name>identifyYzmServlet</servlet-name><url-pattern>/ident</url-pattern></servlet-mapping>-<servlet><servlet-name>yzmServlet</servlet-name><servlet-class>com.westos.untitle2.yzmServlet</servlet-class></servlet>-<servlet-mapping><servlet-name>yzmServlet</servlet-name><url-pattern>/yzm01</url-pattern></servlet-mapping>
HTML Implementation Login Interface:
<! DOCTYPE html>
Servlet combined with HTML for login verification (including CAPTCHA verification) functionality