在IntelliJ IDEA的web.xml中的过滤器映射中使用/ *导致无法打开URL

时间:2019-05-24 10:29:42

标签: java

我在IntelliJ Idea中有一个Web项目。一切正常,但是当我在web.xml中添加除/ SomeServletAnnotation(例如/ *)之外的任何url模式的过滤器映射时,运行Tomcat(7.0.94)时浏览器停止打开。 这是我的过滤器类:

@WebFilter(filterName="loginFilter")
public class LoginFilter implements Filter {
private static final Logger LOGGER = Logger.getLogger(LoginFilter.class);
private static final String CHANGE_LANGUAGE = "CHANGE_LANGUAGE";
private static final String LOGIN = "LOGIN";

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse 
servletResponse, FilterChain filterChain) throws IOException, 
ServletException {
    HttpServletRequest req = (HttpServletRequest) servletRequest;
    HttpServletResponse resp = (HttpServletResponse) servletResponse;
    HttpSession session = req.getSession();
    String command = req.getParameter("command");

    if(session != null && session.getAttribute("user")!= null || 
 isDoFilter(req)) {
        filterChain.doFilter(req, resp);
    }
    else {
        resp.sendRedirect(req.getContextPath()+"/");
    }
}

private boolean isDoFilter(HttpServletRequest req) {

    String command = req.getParameter("command");
    return CHANGE_LANGUAGE.equals(command) || LOGIN.equals(command);
}

@Override
public void destroy() {

}
}

这是我的Servlet:

@WebServlet(urlPatterns = {"/tests"})
public class TestServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse 
response)
        throws ServletException, IOException {
    process(request, response);

}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse 
response)
        throws ServletException, IOException {
    process(request, response);
}

private void process(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
    String command = request.getParameter("command");
    CommandsFactory factory = new CommandsFactory();
    CommandResult result = factory.makeCommand(command).execute(request, 
response);
    String page = result.getPage();
    request.getRequestDispatcher(page).forward(request, response);
}
}

当我在web.xml中编码为:

<welcome-file-list>
    <welcome-file>login_page.jsp</welcome-file>
</welcome-file-list>

<filter-mapping>
    <filter-name>loginFilter</filter-name>
    <url-pattern>/tests</url-pattern>
</filter-mapping>

它工作正常。但是当我做类似的事情时:

<welcome-file-list>
    <welcome-file>login_page.jsp</welcome-file>
</welcome-file-list>


<filter-mapping>
    <filter-name>loginFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

在运行Tomcat时,它显示“无法打开URL。请检查此URL是否正确”。 https://cdn1.savepice.ru/uploads/2019/5/24/7b3109a727cad8b25d8e7829ac0b57a3-full.png

我当然尝试在不使用od @WebFilter注释的情况下重写web.xml中的过滤器映射

<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>controller.LoginFilter</filter-class> 
</filter>

<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

但这没有帮助。创建新项目也无法解决问题。

我将非常感谢您的帮助!

0 个答案:

没有答案