FilterとListener

Filter

Chain of Resp.デザインパターン

ある程度でServletと同じ

リクエストが来る際に実行される

応用:

・Strutsのコントロール

・文字化け対応 Localization

Targeting the request and response to a particular locale

・権限管理 User Authentication

Blocking requests based on user identity

・Accepting or rejecting requests based on security requirements

Calling HttpServletRequest#getUserPrincipal()

Or examining attributes of the HttpSession to see if authenticated

・Logging access to certain resources and auditing

Tracking users and the actions performed

Also be done with a Request Listener

・Caching responses

Wrapping the response's output stream in a ByteArrayOutputStream on cache miss

Replaying saved responses on cache hit

・Performing compression on request or response data

Wrapping the request's input stream or the response's output stream

Making the download easier

・Image conversion

Scaling, squeezing etc

<filter>

<filter-name>MyFilter</filter>

<filter-class>MyFilter</filter-class>

<init-param>

<param-name>blackList</param-name>

<param-value>Mike,John,Tom</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>MyFilter</filter-name>

<url-pattern>/note</url-pattern>

<dispatcher>REQUEST</dispatcher> デフォルト

<dispatcher>INCLUDE</dispatcher> RequestDispatcher#include()

<dispatcher>FORWARD</dispatcher> RequestDispatcher#forward()

<dispatcher>ERROR</dispatcher> <@page error="error.jsp">

</filter-mapping>

Listener

Observerデザインパターン

アプリケーションが起動する際に実行される

応用:

・プラグイン、例えばspring、log4j

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import javax.servlet.ServletContext;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

import javax.servlet.annotation.WebListener;

@WebListener

public class MyListener implements ServletContextListener {

public MyListener() {

}

public void contextInitialized(ServletContextEvent sce) {

ServletContext app = sce.getServletContext();

String driver = app.getInitParameter("driver");

String url = app.getInitParameter("url");

String user = app.getInitParameter("user");

String passwd = app.getInitParameter("passwd");

try{

Class.forName(driver);

Connection conn = DriverManager.getConnection(url, user, passwd);

app.setAttribute("conn", conn);

}catch(SQLException e){

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

public void contextDestroyed(ServletContextEvent sce) {

ServletContext app = sce.getServletContext();

Connection conn = (Connection)app.getAttribute("conn");

if(conn != null){

try {

conn.close();

conn = null;

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

<listener>

<listener-class>com.MyListener</listener-class>

</listener>

※HttpSessionBindingListener除外

Listener分類

1、ServletContextListener ServletContextを監視

public void contextInitialized(ServletContextEvent event);

public void contextDestoryed(ServletContextEvent event);

2、HttpSessionListener Sessionを監視

public void sessionCreated(HttpSessionEvent event);

public void sessionDestoryed(HttpSessionEvent event);

3、HttpRequestListener Requestを監視

public void requestinitialized(ServletRequestEvent event);

public void requestDestoryed(ServletRequestEvent event);

Sessionについて

public class MySessionListener implements HttpSessionListener{

public void sessionCreated(HttpSessionEvent event) {

HttpSession session = event.getSession();

String id = session.getId() + session.getCreationTime();

XXX.UserMap.put(id, Boolean.TRUE);

}

public void sessionDestroyed(HttpSessionEvent event) {

HttpSession session = event.getSession();

String id = session.getId() + session.getCreationTime();

synchronized (this) {

XXX.UserMap.remove(id);

}

}

}