Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get ServletConfig reference in a filter

 
Rene Falzarano
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written a filter that creates a response wrapper. All works well, except that I need to be able to access init properties for individual servlets in the filter. The FilterConfig object only has a reference to the ServletContext object, and not the ServletConfig, even when executing the Servlet in the filter chain. Here's a sample:

public class LogFilter implements Filter{
protected FilterConfig config;
String filterName;

CharArrayWrapper responseWrapper = new CharArrayWrapper((HttpServletResponse) resp);

//Pass wrapper
chain.doFilter(req,responseWrapper);

String param1 = getServletConfig().getInitParameter("param1");

... REST OF CODE
}

The part in bold is where I need a reference to the ServletConfig object. In this case, Filter init-params will not work, I need access to the servlet's init params. Is there any way to force a method on the serlvet to pass this to the request or some other work around?

Thnanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Architecturally a filter is independent of the resources that it wraps. After all, the filter could redirect or forward anywhere and never even invoke the target resource.

I'd consider what you are trying to do a design flaw -- trying to strongly bind the filter to the target resource with which it should be independent.

So why are you doing this? Should not servlet-dependent actions wait until (and if) the servlet is actually invoked?

Or perhaps the data isn't servlet-specific and should be in context parameters rather than init parameters?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<HACK-ALERT>
I only mention this because someone is bound to bring it up. Since you need this info after the servlet is invoked by the filter, the servlet could bind the data as request-scoped variables that the filter can access.

Again, I would consider this an egregious violation of good programming constructs, and would never actually do this in real code.
</HACK-ALERT>
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35753
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rene,
Welcome to JavaRanch!

Do you have to use a filter? It sounds like a regular Java object called from the relevant servlets would be better. Then you can pass in the appropriate parameters.
 
Chip E McCormick
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get init params with the filterConfig:



And then in web.xml:


 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's kind of you that you try to help others here. But it usually doesn't make sense to reply in old and dead topics. The topicstarter may for instance already have solved the problem and thus you're just wasting your effort and time. Please reply to active topics in the listing only. This way you can ensure that you're actually helping others
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, please read this for more information.

Moreover, you didn't answer the question. He wasn't asking how to get the filter params, but how to get the servlet init params of unrelated servlets.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!