Do like this:
MDC.put("xxx", ...);
MDC.put("yyy", ...);

try {
   return handler.service(request, response);
} finally {
  MDC.remove("xxx")
  MDC.remove("yyy")
}

or else you will have problems when your request throws an exception....

Olle Hallin
Senior Java Developer and Architect
olle.hal...@crisp.se
www.crisp.se




2009/10/9 <dirk.latterm...@bgs-ag.de>

> Hi!
>
> Borut Bolčina <borut.bolc...@gmail.com> schrieb am 09.10.2009 14:55:32:
>
> > this is what I did:
> >
> > public class RequestLoggingFilter implements HttpServletRequestFilter {
> >     public final RequestGlobals requestGlobals;
> >
> >     public RequestLoggingFilter(final RequestGlobals requestGlobalss) {
> >         this.requestGlobals = requestGlobalss;
> >     }
> >
> >     @Override
> >     public boolean service(HttpServletRequest request,
> HttpServletResponse
> > response, HttpServletRequestHandler handler)
> >             throws IOException {
> >         MDC.put("remoteIP",
> > this.requestGlobals.getHTTPServletRequest().getRemoteAddr());
> >
> >         String s =
> > this.requestGlobals.getHTTPServletRequest().getRequestedSessionId();
> >         if (s == null) {
> >             s = "";
> >         }
> >         MDC.put("sessionID", s);
> >         return handler.service(request, response);
> >     }
> > }
>
> >
> > but I feel I am missing something. Where do I put the code:
> > MDC.remove("remoteIP");
> > MDC.remove("sessionID");
> >
>
> In a similar situation, I used something like
> --------------------
>     @Override
>    public boolean service(HttpServletRequest request, HttpServletResponse
> response, HttpServletRequestHandler handler)
>            throws IOException {
>        MDC.put("remoteIP",
> this.requestGlobals.getHTTPServletRequest().getRemoteAddr());
>
>        String s =
> this.requestGlobals.getHTTPServletRequest().getRequestedSessionId();
>        if (s == null) {
>            s = "";
>        }
>        MDC.put("sessionID", s);
>         boolean result = handler.service(request, response);
>
>        MDC.remove("remoteIP");
>        MDC.remove("sessionID");
>
>         return result;
>    }
> }
> --------------------
>
> but I'm not sure if it does the right thing in all situations.
>
> Dirk
>
>
>
> BGS Beratungsgesellschaft
> Software Systemplanung AG
>
>
>
>
> Niederlassung Köln/Bonn
> Grantham-Allee 2-8
> 53757 Sankt Augustin
> Fon: +49 (0) 2241 / 166-500
> Fax: +49 (0) 2241 / 166-680
> www.bgs-ag.de
> Geschäftssitz Mainz
> Registergericht
> Amtsgericht Mainz
> HRB 62 50
>
> Aufsichtsratsvorsitzender
> Klaus Hellwig
> Vorstand
> Hermann Kiefer
> Nils Manegold
> Thomas Reitz
>
>

Reply via email to