サーブレットの前にフィルターを実行しています。
public class UserFilter implements Filter {
List<String> sessionIdsList;
WebContentDAOIF webContentDAOIF;
public void setWebContentDAOIF(WebContentDAOIF webContentDAOIF) {
this.webContentDAOIF = webContentDAOIF;
}
@Override
public void init(FilterConfig arg0) throws ServletException {
sessionIdsList = new ArrayList<String>();
sessionIdsList = webContentDAOIF.fetchAllSessionIds();
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
System.out.println("List size: " + sessionIdsList.size());
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpSession session = httpRequest.getSession();
String userSessionId = session.getId();
System.out.println("Filter: " + userSessionId);
if(sessionIdsList.size() !=0 && sessionIdsList.contains(userSessionId)) {
System.out.println("There is same sessionID");
} else {
System.out.println("Hello anonim");
}
RequestDispatcher dispatcher = request.getRequestDispatcher("main");
dispatcher.forward(request, response);
}
@Override
public void destroy() {
}
}
そして、すべての Bean を宣言するスプリング コンテキスト xml ファイルがあります。
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/contentDB</value></property>
<property name="username"><value>root</value></property>
<property name="password" ><value>25051988</value></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>user.hbm.xml</value>
<value>userEvents.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">15</prop>
</props>
</property>
</bean>
<bean id="webContentDAOImpl" class="demidov.pkg.persistence.WebContentDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userFilter" class="demidov.pkg.web.UserFilter">
<property name="webContentDAOIF" ref="webContentDAOImpl"/>
</bean>
また、web.xml でフィルターを宣言しています。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/*-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>userFilter</filter-name>
<filter-class>demidov.pkg.web.UserFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>userFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>initialContentRendering</servlet-name>
<servlet-class>demidov.pkg.web.InitialContentRendering</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>initialContentRendering</servlet-name>
<url-pattern>/main</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>orderEventServlet</servlet-name>
<servlet-class>demidov.pkg.web.OrderEventServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>orderEventServlet</servlet-name>
<url-pattern>/event</url-pattern>
</servlet-mapping>
しかし、アプリケーションをロードすると、次のように表示されます。
SEVERE: Exception starting filter userFilter
java.lang.NullPointerException
at demidov.pkg.web.UserFilter.init(UserFilter.java:35)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:273)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:254)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:372)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:98)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4562)
at org.apache.catalina.core.StandardContext$2.call(StandardContext.java:5240)
at org.apache.catalina.core.StandardContext$2.call(StandardContext.java:5235)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
私が間違っていることを助けてください。ありがとうございます。