2) Use JavaBean to process
1, set up user class, encapsulate data.
1 Package Com.po;2 //User Class3 Public classUsers {4 PrivateString username;5 PrivateString password;6 Publicusers () {}7 PublicString GetUserName () {8 returnusername;9 }Ten Public voidSetusername (String username) { One This. Username =username; A } - PublicString GetPassword () { - returnpassword; the } - Public voidSetPassword (String password) { - This. Password =password; - } + -}
2, establish Userdao class, realize the logic judgment of user login
1 Package Com.dao;2 3 import com.po.users;4 5 //User's business logic class6 Public classUsersdao {7 PublicBoolean Userslogin (Users u) {8 if(U.getusername (). Equals ("Admin") &&u.getpassword (). Equals ("Admin")){9 return true;Ten}Else One return false; A } -}
3, in the login interface, the establishment of JavaBean two, set the form attribute matching is completed, the original if statement to modify.
1<body>2<jsp:usebean id="Loginuser" class="com.po.users"Scope="page"></jsp:useBean>3<jsp:usebean id="Usersdao" class="Com.dao.usersdao"Scope="page"></jsp:useBean>4<jsp:setproperty property="*"Name="Loginuser"/>5 6<%7Request.setcharacterencoding ("Utf-8");8 9 if(Usersdao.userslogin (Loginuser)) {TenString Submit=request.getparameter ("Submit"); One if(submit!=NULL) A { -Session.setattribute ("sessionname", Loginuser.getusername ()); - } the //request.getrequestdispatcher ("success.jsp"). Forward (request, response); -Response.sendredirect ("success.jsp"); - - + } - Else{ +Response.sendredirect ("failure.jsp"); A at } -%> -</body>
3) Cookies enable login and save user name
1<body>2<%3Cookie a[]=request.getcookies ();4 for(intI=0; i<a.length;i++){5String user=a[i].getname ();6 if(User.equals ("Cookieid")){7String name=A[i].getvalue ();8Session.setattribute ("sessionname", name);9Response.sendredirect ("success.jsp");Ten } One } A%> -<Center> - the<form method="Post"action="login.jsp"> -<label for="username"> User name:</label> -<input type="text"Name="username"><br><br> -<label for="Password"> Password:</label> +<input type="Password"Name="Password"><br><br> -Do not save user name: <input type="Radio"Name="Myrad"Value="0"> +Save user name: <input type="Radio"Name="Myrad"Value=" -">a minute A<input type="Radio"Name="Myrad"Value="3600">an hour at<input type="Radio"Name="Myrad"Value="86400">Day -<input type="Radio"Name="Myrad"Value="604800">a week -<input type="Radio"Name="Myrad"Value="2592000">January -<input type="Radio"Name="Myrad"Value="31536000"> One year <br><br> -<input type="Submit"Name="Submit"Value="Submit"> - in</form> - to</Center> +</body> - the<body> *<% $Request.setcharacterencoding ("Utf-8");Panax NotoginsengString Name=request.getparameter ("username"); -String Pw=request.getparameter ("Password"); the if(name!=NULL&&pw!=NULL&&name.equals ("Admin") &&pw.equals ("123456")){ +Session.setattribute ("sessionname", name); AString Ch=request.getparameter ("Myrad"); the if(ch!=NULL){//If a radio button is selected + intTime=integer.parseint (CH); - if(time>0){//If you chose to save the user name $ //Deposit Cookies $Cookie cc=NewCookies ("Cookieid", name);//deposit name into Cookie - Cc.setmaxage (time); - Response.addcookie (cc); the } - }WuyiResponse.sendredirect ("success.jsp"); the } - Else WuResponse.sendredirect ("index.jsp"); -%> About</body>
Javaweb Small test Sledgehammer (i)