1.リスナー
サーブレットイベントリスナーは、特定のインターフェイスを実装するJavaプログラムです。このプログラムは、WebアプリケーションでのServletContext、HttpSession、ServletRequestなどのオブジェクトの作成と破棄のプロセスを監視し、これらのドメインの属性の変更を監視するために特に使用されます。オブジェクト、およびセンスバインディングHttpSessionドメイン内のオブジェクトの状態に設定します。
サーブレット仕様では、8種類のリスナー、およびそれらの種類と機能が定義されています。
HttpSessionActivationListenerの説明には、アクティブ化とパッシベーションの概念が含ま
れます。HttpSessionオブジェクトをメモリからハードディスクに転送するプロセスはパッシベーションと呼ばれ
ます。HttpSessionオブジェクトが永続状態から実行状態に変化するプロセスは、アクティベーションと呼ばれます。
さまざまな監視イベントに応じて、テーブル内のリスナーは次の3つのカテゴリに分類できます
。1。ドメインオブジェクトの作成と破棄のためのイベントリスナー(ServletContextListenerインターフェイス、HttpSessionListenerインターフェイス、ServletRequestListenerインターフェイス)。
2.ドメインオブジェクト属性の追加と削除を監視するために使用されるイベントリスナー(ServletContextAttributeListenerインターフェイス、HttpSessionAttributeListenerインターフェイス、ServletRequestAttributeListenerインターフェイス)。
3. HttpSessionドメインにバインドされたオブジェクトの状態を監視するために使用されるイベントリスナー(HttpSessionBindingListenerインターフェイス、HttpSessionActivationListenerインターフェイス)。
サーブレット仕様では、これら3つのタイプのイベントリスナーが対応するインターフェイスを定義しています。イベントリスナープログラムを作成する場合対応するインターフェースを実装するだけで済みます。リスナーを使用する場合、Webサーバーは、リスナーによって実装されたインターフェイスに従って、監視対象オブジェクトにリスナーを登録します。オブジェクトのリスナーイベントがトリガーされると、Webコンテナはそれに関連するサーブレットリスナーを呼び出します。メソッドは、リスナーを処理します。イベント。
2.HttpSessionListenerリスナーはオンラインの人数をカウントします
多くのウェブサイトやソフトウェアアプリケーションには、現在オンラインになっている人の数を数える機能があります。通常の状況では、ユーザーがWebサイトに入ると、HttpSessionオブジェクトが作成され、ユーザーがWebサイトを離れると、HttpSessionオブジェクトが破棄されます。サーブレットの8つのリスナーのうち、HttpSessionListenerリスナーは、WebアプリケーションでのHttpSessionオブジェクトの作成と破棄を監視できます。したがって、HttpSessionListenerインターフェイスを実装するリスナーを使用して、Webサイトで現在オンラインになっている人の数を数えることができます。
1.リスナーを作成します[HttpSessionListenerインターフェースを実装するための新しいJavaクラスを作成し、メソッドを書き直します]
package com.wangxing.listener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* HttpSessionListener接口用来监听HttpSession对象的创建和销毁
* @author Administrator
*
*/
public class OnlineListener implements HttpSessionListener{
private int count = 0; // 用于统计在线人数变量
//监听HttpSession对象的创建
@Override
public void sessionCreated(HttpSessionEvent hse) {
count++; // Session对象创建时count变量加1
ServletContext context = hse.getSession().getServletContext();
context.setAttribute("count", new Integer(count));
}
//监听HttpSession对象的销毁
@Override
public void sessionDestroyed(HttpSessionEvent hse) {
count++; // Session对象创建时count变量加1
ServletContext context = hse.getSession().getServletContext();
context.setAttribute("count", new Integer(count));
}
}
OnlineListener.javaでは、オンラインユーザーの数をカウントするためのメンバー変数カウントが最初に定義されます。HttpSessionオブジェクトの作成が監視されるたびに、カウントメンバー変数は1ずつ増加します。HttpSessionオブジェクトの破棄が監視されるたびに、カウントメンバー変数は1ずつデクリメントされます。カウントメンバー変数を変更するたびに、他のサーブレットプログラムが使用できるように、カウントメンバー変数の値もServletContextオブジェクトに保存する必要があります。 ServletContextオブジェクトのMember変数からカウントを取得して、ページ上のオンラインユーザーの現在の数を表示できます。
2. web.xmlファイルで、OnlineListenerイベントリスナーを構成します
<listener>
<listener-class>com.wangxing.listener.OnlineListener</listener-class>
</listener>
- ホームページとログアウトページを作成する
package com.wangxing.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HomeServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><meta charset=\"utf-8\"><title>HomeServlet</title></head>");
out.println("<body><center>");
Object countobj=req.getSession().getServletContext().getAttribute("count");
int count=(int)countobj;
out.println("<h1>当前在线人数:"+count+"</h1>");
out.println("<h2><a href=\"logout\">安全退出</a></h2>");
out.println("</center></body>");
out.println("</html>");
out.close();
}
}
package com.wangxing.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LogOutServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//销毁session对象
req.getSession().invalidate();
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><meta charset=\"utf-8\"><title>LogOutServlet</title></head>");
out.println("<body><center>");
out.println("<h1>你已经退出本系统!</h1>");
out.println("</center></body>");
out.println("</html>");
out.close();
}
}
<filter>
<filter-name>encodingfilter</filter-name>
<filter-class>com.wangxing.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.wangxing.listener.OnlineListener</listener-class>
</listener>
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>com.wangxing.servlet.HomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>logout</servlet-name>
<servlet-class>com.wangxing.servlet.LogOutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logout</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
と试http:// localhost:8080 / TestListener / home
安全な退出後の効果