我正在使用AspectJ开发项目,我使用过滤器来处理http请求和响应。基本上我将这个项目变成了一个.jar,并在我执行一些安全测试的基准应用程序中使用这个jar。当我在基准测试应用程序及其web.xml上声明过滤器时它工作正常,但如果我把它放在我正在开发的项目(jar文件)中,那么基准测试应用程序就不会检测到它...我已被告知如果您使用最新版本的web.xml,则不需要在web.xml上声明过滤器,它应该自动检测到它,但它不是工作。我怎样才能让它发挥作用?
我在web.xml中使用的版本是:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
过滤类:
package main.java.filter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import main.java.Configuration;
import main.java.HashTableCreation;
/**
* Servlet Filter implementation class MyFilter
*/
@WebFilter("/MyService/*")
public class MyFilter implements Filter
{
/**
* Default constructor.
*/
public MyFilter() {
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("###### FILTER before NO DRIVER...");
// HttpServletRequest req = (HttpServletRequest) request;
//
// String loggedIn = (String) req.getSession().getAttribute("login");
//
// if (loggedIn == null)
// {
// req.getRequestDispatcher("notLoggedIn.jsp").forward(request, response);;
// }*/
chain.doFilter(request, response);
System.out.println("###### FILTER after...");
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
System.out.println("Initializing filter...");
}
}
答案 0 :(得分:0)
在这种情况下,当你有一个jar
文件时,它只是一个库。 servlet容器没有在jar
文件中查找web.xml
文件。这根本不是它的工作原理。如您所述,您的web.xml
文件必须显式引用过滤器,否则servlet容器将不知道加载它。
如果您尝试使用基于注释的加载,则根据this post中引用的规范,它必须位于WEB-INF/classes
或WEB-INF/lib
中作为jar