アカウントのパスワードを記憶する機能は、卒業設計で必要です。私はオンラインで解決策を見つけました、そして私はそれを自分で変更することができ、次の方法は次のものです。
まず、ログインページ。ユーザーがパスワードをチェックしたら、コントローラー(使用するSSMフレームワーク)に渡し、Cookieの値をバックグラウンドで設定すると、次回ログインするときにアカウントとパスワードを再度入力する必要はありません。
login.jspコード:
<%@ページインポート= "org.apache.commons.lang.stringutils"%> <%@page言語= "java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <%@include file = "public/nocache.jsp <スクリプトタイプ= "text/javascript" src = "$ {pagecontext.request.contextpath} /js/jquery-validation-1.14.0/dist/jquery.validate.min.js"> </script> <style>ボディ{マージン:0px;パディング:0px; } .wrapper {width:100%;高さ:100%;位置:修正; } .content {width:100%;高さ:100%;位置:相対;テキストアライグ:センター; } .login {width:1050px;高さ:450px;位置:絶対;トップ:50%;左:50%;マージントップ:-225px;マージン左:-525px; } </style> <script type = "text/javascript"> window.history.forward(); window.onbeforeunload = function(){} </script> <%@include file = "public/headertop.jsp"%> <! - リソースファイルを入力 - > <body> <% - cookie-%> <%string name = "";文字列password = ""; try {cookie [] cookies = request.getCookies(); if(cookies!= null){for(int i = 0; i <cookies.length; i ++){if(cookies [i] .getname()。 //値フィールドが空でない場合(stringutils.isnotblank(values)){string [] elements = values.split( " - "); //アカウント名またはパスワードを取得しますif(stringutils.isnotblank(elements [0])){name = elements [0]; } if(stringutils.isnotblank(elements [1])){password = elements [1]; } } } } } } } } } } } } } } catch(Exception e){ } %> <div style=""> <div> <div> <!-- The main content part begins--> <div fit="true"> <div region="west"> <div fit="true"> <div region="west"> </div> <div region="center" style = "font-family: 'microsoft yahei';"> <p style = "position:" position:retary; magne-top:200px; padding-left:20px; "> <span style =" font-size:30px; font-weight:800; "> auto maintenation management system </span <br/> region = "center"> <div fit = "true"> <div region = "north" style = "height;"> </div> <div region = "west"> <img src = "$ {pagecontext.request.contextpath} /img/split.png" style = "" style = ""> div:0px; " <div iconcls = "icon-user" style = "text-align:center; width:300px; height:260px; padding-top:50px;"> <form id = "ff" method = "post"> <div> <入力id = "name =" accountsnumber "data-options =" pext "" Icon-man '"pept" "Icon-man'" pext "in prets" flued "pept" fult "pept" flue %> "> <a id =" dd "href ="# "> </a> </div> <div style =" margin-top:20px; "> <input id =" passwords "names" type = "password" data-options = "iconcls: 'icon-lock'、comption: '" persped " 10px; " style = "text-align:left;" > <Span style = "float:left; padding-left:30px; font-size:12px;"> <input id = "flag" name = "flag" type = "checkboce" balue = "1" checked = "checked"/>覚えてください</span> </div> <div style = "clear:clear:clear:of;"> id = "submitbtn" iconcls = "icon-accept"> login </a> <a style = "margin-left:30px; width:80px; height:30px;" href = "#" iconcls = "icon-arrow_undo"> cancel </a> </p> </div> </form> </div> </div> <div region = "east"> </div> <div> <div "stiled =" height:height; "> </div> </div> div> </div> </div> <script type = "text/javascript"> $(function(){console.log( "[automotive menationing management system/n codeby:pengchan/n email:[email protected]/n csdn:http://blog.csdn.net/w3chhhh/]") ")") ")") ") $( "#submitbtn")。クリック(function(){//空のかどうかを決定するif($( "#アカウント")。val()== ""){$ .messager.alert( 'login message prompt'、 'user'sアカウントは空になりません'、 'info'); return;} if($( "#passwords")プロンプト '、ユーザーのパスワードは' nection '); if(data.iserror){$ .messager.alert( 'login message prompt'、data.errormsg、 'info')}}; //成功した場合は、メインページにジャンプします//ここでは、最初に歴史的要求があるかどうかを判断します。ある場合は、前のページにもう一度アクセスしてくださいvar hisurl = '$ {hisurl}'; if(hisurl!= null && hisurl.length> 0){window.location.href = "$ {pagecontext.request.contextpath}"+hisurl; } else {window.location.href = "$ {pagecontext.request.contextpath} /index/main.html"; }}}} catch(e){$ .Messager.Alert( "ログインメッセージプロンプト"、 'サーバーは例外を返します。後でもう一度試してください!'、 'info'); }}、error:function(error){$( "#ff")。form( "clear"); }}); }); </script> </body> </html>バックグラウンドで処理されたJavaコードの一部:
パッケージcom.javaweb.controller; javax.servlet.http.cookieをインポートします。 javax.servlet.http.httpservletrequestをインポートします。 javax.servlet.http.httpservletResponseをインポートします。 org.apache.commons.lang.stringutilsをインポートします。 org.slf4j.loggerをインポートします。 org.slf4j.loggeractoryをインポートします。 Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.context.annotation.scope; org.springframework.stereotype.controllerをインポートします。 Import org.springframework.ui.model; org.springframework.web.bind.annotation.pathvariableをインポートします。 org.springframework.web.bind.annotation.requestmappingをインポートします。 Import org.springframework.web.bind.annotation.responsebody; com.alibaba.fastjson.jsonをインポートします。 com.javaweb.entity.accountをインポートします。 com.javaweb.service.impl.serviceFactoryをインポートします。 com.javaweb.utils.basecontrollerをインポートします。 com.javaweb.views.loginbeanをインポートします。 /** *ユーザー情報コントローラー * @Author CP * */@Controller @Scope( "Prototype")@RequestMapping( "/users")Public class usersutroller extends Basecontroller {private static final logger logger = oggeractory.getLolgger(userininfocontroller.class); @Autowired Private ServiceFactoryFurtyFactory; /*** @paramリクエストリクエスト* @paramモデルモデル* @paramアカウントアカウント情報* @return*/@requestmapping( "/login")@responsebody public string login(httpservletrequest request、httpservletresponse応答、モデルモデル、モデル、アカウントアカウント){logger.info( "ユーザーの試行) if(account == null){return responsefail( "送信パラメーターは空です!"); } else {if(stringutils.isblank(account.getAccountNumber())){return responsefail( "ユーザーのアカウントは空"); } if(stringutils.isblank(account.getPasswords())){return responsefail( "ユーザーのパスワードは空"); } loginbean loginbean = null; loginbean = servicefactory.getuservalidateservice()。userislawable(account); if(loginbean == null){return responsefail( "usernameまたはパスワード入力が正しくない"); } else {//成功した場合// loginbeanをセッションにrequest.getSession()。setAttribute( "user"、loginbean); // cookie string flag = request.getParameter( "flag")に入れます。 //アカウントを覚えておく必要がある場合は、アカウントとパスワードを保存するif(flag!= null && flag.equals( "1")){cookie cookie = new cookie( "cookie_user"、loginbean.getaccountnumber()+" - "+loginbean.getpasswords(); cookie.setmaxage(60*60*24*3); // response.addcookie(cookie)を保存; logger.info( "ストレージユーザーのCookie:"+loginbean.getAccountNumber()+" - "+login.getPasswords()); } else {//アカウントのパスワードを覚えておく必要がない場合は、アカウントCookie cookie = new cookie( "cookie_user"、loginbean.getaccountnumber())を保存します。 cookie.setmaxage(60*60*24*30); Response.AddCookie(Cookie); logger.info( "ユーザーのCookieの保存:"+loginbean.getAccountNumber()); } //ホームページにジャンプしてくださいlogger.info( "user:"+loginbean.getaccountNumber()+"システムに正常に入力されました"); RESPOSSESUCSES(loginbean、 "loginsuccessly"); }}}/***システムからログアウト* @param request request* @paramモデルモデル* @paramアカウント番号* @return*/@RequestMapping( "/{countnum}/logout.html")public string logout(httpservletrequestリクエスト、モデルモデル、 @pathvariable(QUANCNUM ")STRING STRINGRIABLE(")StringNum) logger.info( "user"+accountnum+"、システムからログアウト..."); //セッションを空のrequest.getSession()。setAttribute( "user"、null)に設定します。 //ページジャンプリターン「ログイン」; }}ランニング効果:
アカウントのパスワードを入力した後、ログインしてください。
ログアウトした後、もう一度ログインします。
上記は、編集者の紹介です。 Java WebでCookieを使用して、ユーザーのアカウントとパスワードを思い出します。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!