Tomcat服务器将不会使用筛选器启动

1l5u6lss  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(164)

下面是web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.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">
<display-name>Projeto_joas</display-name>

<resource-ref>
<res-ref-name>jdbc/DiaDiaDev</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<servlet>
<display-name>FacesServlet</display-name>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
 </context-param>

<context-param>
  <param-name>com.sun.faces.expressionFactory</param-name>
     <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>

<!-- <filter> -->

<!--<filter-name>FilterHibernate</filter-name> -->
<!--<filter-class>util.filter.ConexaoHibernateFilter</filter-class> -->
<!--</filter> -->

<!--<filter-mapping> -->
<!-- <filter-name>FilterHibernate</filter-name> -->
<!--<url-pattern>*.jsf</url-pattern> -->

<!--</filter-mapping> -->
</web-app>

这样服务器运行得很好,如果我删除了过滤器上的注解,它将不会启动,并将抛出那些异常:
严重:启动java.util.concurrent时子容器失败。执行异常:org.apache.catalina.LifecycleException:无法启动组件[标准引擎[ Catalina ].标准主机[本地主机]]原因:org.apache.catalina.LifecycleException:无法启动组件[标准引擎[ Catalina ].标准主机[本地主机]]原因:org.apache.catalina.LifecycleException:启动期间子容器失败

警告:无法检索容器[StandardEngine[ Catalina ].StandardHost[localhost].StandardContext[/Projeto_joas]]的JNDI命名上下文,因此不会对该容器javax执行任何清理。naming.NamingException:没有绑定到此类加载器的命名上下文
过滤器类:

package util.filter;

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 org.hibernate.SessionFactory;

import util.HibernateUtil;

public class ConexaoHibernateFilter implements Filter {

    private SessionFactory sf;

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        this.sf = HibernateUtil.getSessionFactory();

    }



    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain chain) throws IOException, ServletException {

        try {

            this.sf.getCurrentSession().beginTransaction();
            chain.doFilter(servletRequest, servletResponse);
            this.sf.getCurrentSession().getTransaction().commit();
            this.sf.getCurrentSession().close();

        } catch (Throwable ex) {
            try {
                if (this.sf.getCurrentSession().getTransaction().isActive()) {
                    this.sf.getCurrentSession().getTransaction().rollback();
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
            throw new ServletException(ex);
        }

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}
rt4zxlrg

rt4zxlrg1#

我也犯了同样的错误。我犯的错误是我没有在destroy和init方法中添加任何主体。所以对我来说,它只需要在destroy和init方法的主体中添加一些print语句就可以了。

@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        pw.println("Error 404 bad request");
        // chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        System.out.println("destroyed"); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("initilized"); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

相关问题