wood burning stoves 2.0
The moose likes Servlets and the fly likes Using servlets params from filters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Using servlets params from filters" Watch "Using servlets params from filters" New topic

Using servlets params from filters

Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
Hi ranchers, I got a question for you.
in web.xml config file I configured a set of servlets with an init-param (let's call it OP_CODE). Each servlet has its own OP_CODE. Now, since the logic that handles this param is the same for each servlets, I'd like to move this logic to a filter. What I need to know is : is there a way to retrieve the configured value of the OP_CODE param from the filter code, starting from the requested URL ?
I'll try to be a bit more clear:

in web.xml :

Now in filter's code :

I hope I've been clear enough. Any suggestion ?
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73
see this http://www.coderanch.com/t/366066/Servlets/java/get-ServletConfig-reference-filter

You may need to put your param in the request or response. Or you may build an xml or property file and read it when the web application starts through ContextListener then put it into ServletContext. The map contains the Servlet name as key and the OP_CODE as the value.

You can get the ServletContext through the FilterConfig

Alan Mehio

Alan Mehio
London, UK
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

Move the value to a context parameter.

[Asking smart questions] [About Bear] [Books by Bear]
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
How can you move the value as a context parameter when there are more than just one value ?

Mine was just an example, but the real case is like this :

So, there can be n servlets, each with its own OP_CODE param. The check on the OP_CODE value is the same for every servlet, that's why I'm using a filter (by now I pass the OP_CODE value as a request variable, but it's kinda dirty in my opinion). I'm also thinking to remove the filter and let all my servlets to extend an unique one which will do the same logic in her service method. (although I kinda liked the idea of a filter).
I agree. Here's the link: http://aspose.com/file-tools
subject: Using servlets params from filters
jQuery in Action, 3rd edition