0

我正在研究开发一个 servlet/过滤器以在 Web 应用程序中使用,过滤器必须记录 Web 应用程序的所有用户在网站上的每个请求。在我的脑海中,过滤器必须以这种方式工作

过滤器 -> 请求 -> 保存请求 -> do.chain

public class ServletFilter implements Filter {
  private Application fApp;
 StringWriter ResponseRECORDER; 
 StringWriter RequestRECORDER;
  @Override
  public void init(FilterConfig config) throws ServletException {
    fApp = (Application)config.getServletContext().getAttribute("application");
  }

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

    chain.doFilter(request,response);
    // Ignore non-http requests.
    if (!(request instanceof HttpServletRequest))
    {
        chain.doFilter(request,response);
        return;
    }

    ((HttpServletRequest)request).getSession();


    // Write the request out to the recording file.
    recordReqResHTTP((HttpServletRequest) request,
            (HttpServletResponse) response); 
    StringBuilder Fixed = new StringBuilder();
    Fixed.append("[Message]");
    Fixed.append("[time]");  
    Fixed.append(System.currentTimeMillis());
    Fixed.append("[/time]");
    Fixed.append("[Request]");
    Fixed.append(RequestRECORDER);
    Fixed.append("[/Request]");
    Fixed.append("[Response]");
    Fixed.append(ResponseRECORDER);
    Fixed.append("[/Response]");
    Fixed.append("[/Message]");

    MessagingService s = (MessagingService)fApp
        .getService("it.interprise.core.workflow.MessagingService");    
    try {
      s.send("recorder", Fixed.toString());
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  @Override
  public void destroy() {
  }

  public void recordReqResHTTP(HttpServletRequest request,
     HttpServletResponse response)
{
  //HttpSession session = request.getSession();

  //costruisco una stringa per la raccolta dati
  StringWriter ResponseRECORDER = new StringWriter();  
  StringWriter RequestRECORDER = new StringWriter();

  try
  {

    //Registro la Request 

      PrintWriter out = new PrintWriter(RequestRECORDER);
      out.println("<request>");
      out.print("<uri>");
      out.print(request.getRequestURI());
      out.println("</uri>");
      Enumeration e = request.getParameterNames();
      while (e.hasMoreElements())
      {
          String paramName = (String) e.nextElement();
          String[] values = request.getParameterValues(paramName);
          for (int i=0; i < values.length; i++)
          {
              out.print("<param><name>");
              out.print(paramName);
              out.print("</name><value>");
              out.print(values[i]);
              out.println("</value></param>");
          }
      }
      out.println("</request>");
      out.close();
    //Registro la Response

      PrintWriter res = new PrintWriter(ResponseRECORDER);
      res.println("<request>");
      res.print("<uri>");
      res.print(request.getRequestURI());
      res.println("</uri>");
      Enumeration f = request.getParameterNames();
      while (f.hasMoreElements())
      {
          String paramName = (String) f.nextElement();
          String[] values = request.getParameterValues(paramName);
          for (int i=0; i < values.length; i++)
          {
              res.print("<param><name>");
              res.print(paramName);
              res.print("</name><value>");
              res.print(values[i]);
              res.println("</value></param>");
          }
      }
      out.println("</request>");
      out.close();
  }

  catch (Exception exc)
  {

  }
}
}

和响应一样,你知道我该如何解决这个问题吗?使用此过滤器,Web 应用程序停止响应...

谢谢你

4

1 回答 1

1

您应该做的第一件事是至少在示例的底部记录。Exception也许有一个Exception无法通过检查代码来识别的凸起。

关闭两次之前的几行PrintWriter,这可能导致IOException.

第二:您声明了实例变量ResponseRECORDERRequestRECORDER,但后来您声明了两个同名的局部变量。删除实例变量 -filter必须实现线程安全。

完成后,我猜我们会看到一个隐藏的NullPointer.

于 2012-07-30T13:33:41.140 回答