Userservlet
1 //Login Function2 PublicString Login (httpservletrequest req, HttpServletResponse resp)throwsunsupportedencodingexception {3 /*4 * Encapsulate form data to user//Verify form data using service query If there is no Save error message user name or password error saving user data in order to echo5 * If there is a view status status of False save error message you did not activate save form data in order to echo forward to login.jsp login that success6 * Save the current query out of the user to the session to save the name of the current users to the cookie note Chinese to encode processing7 */8 //encapsulate form data to user9User Formuser = Commonutils.tobean (Req.getparametermap (), user.class);Ten //Verifying form data Onemap<string, string> errors =Validatelogin (Formuser, Req.getsession ()); A if(Errors.size () > 0) { -Req.setattribute ("Errors", errors); -Req.setattribute ("form", formuser); the return"F:/jsps/user/regist.jsp"; - } - //to UserService's LOGINFF. -User User =Userservice.login (formuser); + if(User = =NULL) { -Req.setattribute ("msg", "User name or password error"); +Req.setattribute ("User", formuser); A return"F:/jsps/user/regist.jsp"; at}Else { - if(!User.isstatus ()) { -Req.setattribute ("msg", "You have not activated"); -Req.setattribute ("User", formuser); - return"F:/jsps/user/regist.jsp"; - in}Else { - //Save the current query out of user to session to save the name of the current users to the cookie note Chinese to encode processing toReq.getsession (). SetAttribute ("Sessionuser", user); +String loginname=user.getloginname (); -Loginname=urlencoder.encode (LoginName, "Utf-8"); theCookie cookie=NewCookie ("LoginName"), loginname); * //set the age of the cookie otherwise cookies only live in the browser memory $Cookie.setmaxage (60*60*24*10);//Save 10 daysPanax Notoginseng Resp.addcookie (cookie); - return"R:/index.jsp"; the + } A } the +}
login.jsp The local cookie to determine whether the echo is consistent with the user received in the cookie to echo the user's priority higher
1<script type= "Text/javascript" >2$(function(){//Map<string<cookie name >,cookie<cookie itself >>3 //El expressions are executed first, so if you do not add double quotes before you execute JavaScript, you are going to be King eight.4 //get the user name in the cookie5 varLoginname=window.decodeuri ("${cookie.loginname.value}");6 //gets the echo user name7 if("${requestscope.user.loginname}")8 {9Loginname= "${requestscope.user.loginname}"; Ten } One$ ("#loginame"). Val (loginname); A - }); -</script>
top.jsp User Login Judgment
1 <C:choose>2 <C:whenTest= "${empty Sessionscope.sessionuser}">3 <ahref= "<c:url value= '/jsps/user/login.jsp '/>"Target= "_parent">Member Login</a>| 4 <ahref= "<c:url value= '/jsps/user/regist.jsp '/>"Target= "_parent">Registered Members</a>5 </C:when>6 <c:otherwise>7Member: ${sessionscope.sessionuser.loginname} | 8 <ahref= "<c:url value= '/jsps/cart/list.jsp '/>"Target= "Body">My Shopping Cart</a> | 9 <ahref= "<c:url value= '/jsps/order/list.jsp '/>"Target= "Body">My order</a> | Ten <ahref= "<c:url value= '/jsps/user/pwd.jsp '/>"Target= "Body">Change Password</a> | One <ahref= "<c:url value= '/jsps/user/login.jsp '/>"Target= "_parent">Exit</a> A </c:otherwise> - </C:choose>
Goods: Login page Login function implementation