最近、私は要件を持つプロジェクトを作成しました。自動ログイン機能を実装したい。関連情報を確認することにより、セッション監視を使用してそれを行う予定です。以下は、モニターを構成する方法のリストです。
1.次のコードをプロジェクトのweb.xmlファイルに追加します。
<! - セッションリスナーの追加 - > <リスナー> <リスナークラス>リスナーパス</リスナークラス> </リスナー>
2。Javaクラスを書きます。
public class sessionlistenerはhttpssessionlistener {public void sessioncreated(httpssessionevent arg0){// simpledateformat simpleformat simpleformat = new simpledateformat( "mm-ss-ms"); string nowtimes = simpleformat.format(new null(); u = unul)時間: "+nowtimes+" _ "+u); httpsession ses = arg0.getsession(); string id = ses.getid()+" _ "+ses.getcreationtime();} public void sessiondestroyed(httpsessionevent arg0){//シンプルなファーフォンのシンプルな形式のsimpledateformatの実行SimpleDateFormat( "MM-SS-MS"); string nowtimes = simpleformat.format(new date()); //system.out.println("sessionが期限切れになりました。終了時間: "+nowtimes);}}構成が完了した後、セッションの有効期限が切れた後、SessionDestroyedメソッドを正常に入力しました。ページジャンプ操作を実行する準備ができていました。突然、ジャンプの書き方を見つけました。私はun然とし、マスターに尋ねるためにインターネットをサーフィンし続けました。このモニターは、背景統計処理の処理で行われ、ページジャンプの機能を実現できないことがわかりました。
この方法を放棄して、フィルターを使用して実装することしかできません
1. web.xmlにフィルター構成を追加します
<filter> <filter-name> sessionfilter </filter-name> <filter-class> com.orchestrall.web.helper.session.sessionfilter </filter-class> </filter> <filter-mapping> <filter-name> sessionfilter </filter-name> <url-pattern>/actions/*</url-perthers> </filter-mapping>
2.新しいSessionFilterクラスを作成して、フィルターインターフェイスを実装します。
public class sessionfilterはfilter {public void destroy(){// todo auto-denerated method stub} public void dofilter(servletResponse Responece、filterchainチェーン)IoException、servletexception {httpservletrequest httprequest =(httpservletreletreconse htppersperpret; =(httpservletResponse)応答; httpsession session = httprequest.getSession(); // login urlstring loginurl = httprequest.getContextPath() + "/admin/login.jsp"; string url = httprequest.getrequesturi =; url.substring(url.lastindexof( "/")); //タイムアウト処理、ajaxリクエストタイムアウトはタイムアウトステータスを設定し、ページリクエストタイムアウトはプロンプトを返し、path.indexof( "。アクション")!= -1 && session.getattribute( "login_success")= = null secienine) (httprequest.getheader( "x-requested-with")!= null && httprequest.getheader( "x-requested-with")。 "Timeout"); httpresponse.addheader( "loginpath"、loginurl); Chain.dofilter(request、response); // kinolt erter} else {string str = "<スクリプト言語= 'javascript'> alert( 'session expires、begain');"+ "windoc.href = '+ logonl "'; </script>"; respons.setContentType( "text/html; charset = utf-8"); //中国語の文字化けtry {printwriter writer = respons.getWriter(); writer.write(); writh.flush(); writer.close();} catch(Exceance e){e.printstacktrace(); {Chain.dofilter(request、response);}}@overridepublic void init(filterconfig arg0)servletexceptionをスローする{// todo auto-enerated method stub}}}3。AJAXリクエストセッションタイムアウトに使用されるクライアントJS
jquery用
<script type = "text/javascript"> $(document).ajaxcomplete(function(event、xhr、settings){if(xhr.getResponseHeader( "sessionstatus")== "Timeout"){if(xhr.getResponseheader( "loginpath")) windocy.leplace(xhr.getResponseheader( "loginpath")); </script>extjsのajaxリクエスト
ext.ajax.on( 'requestcomplete'、checkusersessessstatus、this); function checkusersessessStatus(conn、response、options){if(respons.getResponseHeader( "sessionstatus")== 'timeout'){if(response.getResponseheader( "loginpath")) Response.getResponseHeader( "LoginPath");} else {alert( "request timeout、bout nogin!");}}}}特定のAJAX要求がグローバルメソッドの影響を受けない場合、$ .ajax()メソッドを使用する場合、パラメーター内のグローバルをfalseに設定できます。 jQueryコードは次のとおりです。
$ .ajax({url: "test.html"、global:false //グローバルajaxイベントをトリガーしないでください})上記は、セッションが失効した後にログインページに自動的にジャンプすることを編集者が紹介したコードの例です。私はそれが誰にでも役立つことを願っています。もっと知りたい場合は、wulin.comのWebサイトに注意してください!