我在servlet之前执行过滤器:
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() {
}
}
我有弹簧上下文xml文件,我声明了所有的bean:
<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)
请帮助我,我做错了什么。 谢谢大家。
答案 0 :(得分:3)
您将获得UserFilter
的两个不同实例,一个由Servlet容器管理,另一个由Spring管理 - 由Servlet容器创建的UserFilter当然具有空WebContentDAOIF。
解决方案是让Spring管理您的UserFilter(并正确注入WebContentDAOIF)。然后使用Spring的DelegatingFilterProxy来处理最终委托给UserFilter的过滤。
在您的web.xml中,删除您的UserFilter声明并声明DelegatingFilterProxy。
<filter>
<filter-name>userFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>userFilter</param-value>
</init-param>
</filter>