Introduction: Add validation code to the Java Web login interface
1, the front-end HTML page part
<form onsubmit= "return FormCheck ()" method= "POST" class= "Form-horizontal" id= "BaseForm" role= "form" name= " BaseForm "enctype=" Multipart/form-data "> <div style=" margin:0px auto;width:400px; > <div class= "Panel Panel-default" style= "opacity:0.8" > <div class= "panel-heading" > <span CLA ss= "Glyphicon glyphicon-lock" ></span> login </div> <div class= "Panel-body" > <div class= " Form-group "> <label for=" username "class=" col-sm-3 Control-label "> Username:</label> <di V class= "col-sm-9" > <input class= "Form-control" name= "username" value= "${username!"
Id= "username" placeholder= "username"/> </div> </div> <div class= "Form-group" > <label for= "Loginpass" class= "Col-sm-3 Control-label" > Secret code:</label> <div clas s= "col-sm-9" > <input class= "form-control" name= "password" type= "password"placeholder= "Password" id= "Loginpass"/> </div> </div> <div class= "Form-group" > <lab El class= "col-sm-3 control-label" > Authenticode:</label> <div class= "col-sm-9" > <input class= "Form-con" Trol "style=" width:163px "name= checkcode" type= "text" id= "Checkcode"/> <!--Internal automatic Send request, load verification code--> </div> </div> <div class= "Form-group Last" > <div class = "Col-sm-offset-3 col-sm-9" > <button type= "Submit" class= "btn btn-success btn-sm" > Login </button> <butt On type= "reset" class= "btn btn-success btn-sm" > Reset </button> </div> </div> </div> <div class= "Panel-footer" > < #if context.hasmessage??
> < #list context.errors as error> ${error} </#list > < #list context.infos as info> ${info} </#list > </#if; </div> </div> </div> </form>
2, back-end controller code
public void Check (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Long Time=system.currenttimemillis ();
Notifies the browser not to cache Response.setheader ("Content-type", "text/html;charset=utf-8");
Response.setheader ("Expires", "-1");
Response.setheader ("Cache-control", "No-cache");
Response.setheader ("Pragma", "-1");
Response.setdateheader ("last-modified", time);
Response.setdateheader ("Date", time);
Response.setdateheader ("Expires", 0);
Response.getwriter (). Write (///"Congratulations on your login success, will be in 3 seconds to jump to the home page, if not jump, please click <a href= ' > Hyperlink </a>");
int height=33;
int width=90;
Get a memory image bufferedimage BufferedImage img=new bufferedimage (width, height, bufferedimage.type_int_rgb);
Get a brush Graphics g=img.getgraphics ();
Draws a border drawrect the border of the specified rectangle.
G.drawrect (0, 0, width, height);
Fill Color G.setcolor (color.gray);
G.fillrect (1, 1, width-2, height-2); Draw disturbing line G.setcolor (Color.Black);
Random r=new Random ();
for (int i=0;i<20;i++) G.drawline (r.nextint (width), r.nextint (height), r.nextint (width), r.nextint (height));
Generating random digital G.setcolor (color.red); G.setfont (New Font ("Microsoft Ya Hei", font.bold|
Font.Italic);//bold bold, italic italic int d=15;
StringBuffer sb=new StringBuffer ();//The use of the variable string for (int j=0;j<4;j++) {String Code=r.nextint (10) + "";
Sb.append (code);
g.DrawString (code+ "", D, 20);
d+=20;
//Enter the validation code into the session to verify the Request.getsession (). setattribute ("Checkcode", sb.tostring ());
Output hits the Web page Imageio.write (img, "JPG", Response.getoutputstream ()); }
Show results