Servlet过滤器出错

时间:2012-07-05 07:48:31

标签: java servlets servlet-filters

chain.doFilter(request, response);执行时出现以下错误。此错误有时会发生。

堆栈跟踪位于::

之下
enter code here

<i>javax.servlet.ServletException: For input string: "NULL"
    at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:423)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:835)
    at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:341)
    at com.evermind.server.http.ServletRequestDispatcher.forward(ServletRequestDispatcher.java:261)
    at com.evermind.server.http.GetParametersRequestDispatcher.forward(GetParametersRequestDispatcher.java:257)
    at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1056)
    at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:388)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:231)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:65)
    at my.SampleFilter.doFilter(SampleFilter.java:120)
    at com.evermind.server.http.EvermindFilterChain.doFilter(EvermindFilterChain.java:16)
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
    at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:670)
    at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:341)
    at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:816)
    at com.evermind.server.http.AJPRequestHandler.run(AJPRequestHandler.java:231)
    at com.evermind.server.http.AJPRequestHandler.run(AJPRequestHandler.java:136)
    at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:186)
    at java.lang.Thread.run(Thread.java:534)</i>

我的过滤器代码如下:

<i>public class SampleFilter
implements Filter
{

private transient FilterConfig filterConfig;
Context initialContext;

public SampleFilter()
{
    filterConfig = null;
    initialContext = null;
}

public void destroy()
{
}



public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws ServletException, IOException
{



    Connection conn=getJNDIConnection();

    request.setAttribute("CONNECTION", conn);
    try
    {
        System.out.println("Filter start GOOG Website");
        chain.doFilter(request, response);
    }
    catch(Exception e)
    {
        //System.out.println("@@@@@@@@@@@@@@@@@@@@@ Exception occured in doFilter @@@@@@@@@@@@@@@");
        closeConnection(initialContext, conn);
        System.out.println("@@@@@@@@@@@@@@@@@@@@@ Connection Closed @@@@@@@@@@@@@@@");

        String fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e);



        e.printStackTrace();
    }
    closeConnection(initialContext, conn);
    System.out.println("Filter end ");
}

Connection getJNDIConnection()
{
    Connection conn = null;
    try
    {
        initialContext = new InitialContext();
        if(initialContext == null)
        {
            System.out.println("JNDI problem. Cannot get InitialContext.");
        }
        DataSource datasource = (DataSource)initialContext.lookup("jdbc/MY_DS");
        if(datasource != null)
        {
            conn = datasource.getConnection();
        } else
        {
            System.out.println("Failed to lookup datasource.");
        }
    }
    catch(NamingException ex)
    {
        System.out.println("Cannot get connection: " + ex);
    }
    catch(SQLException ex)
    {
        System.out.println("Cannot get connection: " + ex);
    }
    return conn;
}



void closeConnection(Context ctx, Connection conn) {
    try {
        if (ctx != null) {
            ctx.close();
            ctx = null;
        }

        if (conn != null && !conn.isClosed()) {
            conn.close();
            conn = null;
        }
    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException sqe) {
        sqe.printStackTrace();
    } finally {
        conn = null;
    }
}

public void init(FilterConfig filterConfig)
    throws ServletException
{
    this.filterConfig = filterConfig;
}
}
</i>

我的web.xml内容位于::

之下
<i><?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>



  <filter>
     <filter-name>UrlRewriteFilter</filter-name>
     <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
  </filter>

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


    <filter>
        <filter-name>SampleFilter</filter-name>
        <filter-class> my.SampleFilter
        </filter-class>
    </filter>

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

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

     <session-config>
      <session-timeout>720</session-timeout> 
    </session-config>

</web-app>
</i>

由于

1 个答案:

答案 0 :(得分:0)

这里有一些误解。

javax.servlet.ServletException: For input string: "NULL"
    at org.apache.struts.action.RequestProcessor.processException

此异常不是由您的过滤器引起的,但是它会在其他地方引发并抛出,并且在您以不正确的方式处理异常的过滤器中的try-catch块之前永远不会被捕获。您应该抓住 那些您可以合理处理的例外情况。由于显然没有任何例外情况可以合理处理,因此您应该让这些例外情况完全删除整个catch块并将其替换为finally

Connection conn = getJNDIConnection();
request.setAttribute("CONNECTION", conn);

try {
    chain.doFilter(request, response);
} finally {
    closeConnection(initialContext, conn);
}

回到特定的例外,正如所说的那样是在其他地方造成的。由于堆栈跟踪不完整,我会做一个疯狂的猜测:你在某处输入一个字符串值为"NULL"的数字字段,并在其上抛出NumberFormatException 问题的解决方案应该足够明显:要么不输入字符串值"NULL",要么使其成为字符串字段而不是数字字段。