Meaningless Drivel is fun!*
The moose likes Tomcat and the fly likes Global Filter for All Web Applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Global Filter for All Web Applications" Watch "Global Filter for All Web Applications" New topic
Author

Global Filter for All Web Applications

shaki shabez
Greenhorn

Joined: Oct 05, 2011
Posts: 7
hi there ,

i was just wondering , how do i use a custom filter for all web applications and not for a specific webapp ?

i have edited the conf\web.xml just like a normal web.xml .
but i have no clue where to put the classes\filters\*.java,class files . because normally it will be under WEB-INF\classes\filters
so i tried putting the files in every possible folder under conf folder , still doesnt work .

any ideas please ?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

Don't try to do run-time sharing. At build time, give the jar file to each web app.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
If this is something that you need done for all web apps, AND if you know that you will be using Tomcat for the foreseeable future, then writing a Tomcat Valve -instead of a servlet filter- might be a better solution.
shaki shabez
Greenhorn

Joined: Oct 05, 2011
Posts: 7
Don't try to do run-time sharing. At build time, give the jar file to each web app.


it should run on the server as a global filter , the idea is making it a global filter to all webapps without interfering with the webapps .

If this is something that you need done for all web apps, AND if you know that you will be using Tomcat for the foreseeable future, then writing a Tomcat Valve -instead of a servlet filter- might be a better solution.


yes i have read the some guides about valves and filters , the problem is that the filter already been implemented as a class that extends Filter class
and it shouldnt be change .



thanks for both answers ,
can you think of a way on running a filter on a all web apps with just messing around with the conf\web.xml file ?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Changing something without changing something (either the config files or the Java class) will be difficult :-)
shaki shabez
Greenhorn

Joined: Oct 05, 2011
Posts: 7
Tim Moores wrote:Changing something without changing something (either the config files or the Java class) will be difficult :-)


ah theres no problem changing the conf\web.xml file , just cant change each web.xml for each web app .


maybe if i clarify what are my intentions , then maybe you can understand better why im asking this .

i want to build a web app , similar to the tomcat "App Manager" . meaning , when ull enter the webapp , you will get a list of all the
webapps on the server , if you will pick one web app , ull get a list of all servlets / JSP is running , each servlet and JSP
ull pick from that list , you will get some stats about that servlet/jsp .

so for conclusion , i want to build a web app which monitors all the web applications on the server , and their servlets / jsp .

im wondering if is it possible to config the confg\web.xml so that my app will run first , and maybe some how configuring myapp\WEB-INF\web.xml
so that my filter will work for all web apps ? maybe if ill use <url-mapping>/*<url-mapping> ?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Are you certain that you need to run code as part of the other web apps? Something like http://code.google.com/p/psi-probe/ shows a whole lot of information despite being a separate web app.
shaki shabez
Greenhorn

Joined: Oct 05, 2011
Posts: 7
Tim Moores wrote:Are you certain that you need to run code as part of the other web apps? Something like http://code.google.com/p/psi-probe/ shows a whole lot of information despite being a separate web app.



exactly !
just not the same features as the PSI-Probe , i just need basic monitoring of all requests done on the server .
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Maybe the (Extended) Access Log Valve can do that for you: http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html
shaki shabez
Greenhorn

Joined: Oct 05, 2011
Posts: 7
Tim Moores wrote:Maybe the (Extended) Access Log Valve can do that for you: http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html


i really appreciate the help .
altho , im not seeking for an alternative , i really do whish to use my custom made filter for all web apps , alternative one web app with a filter , to filter requests for all web apps.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
OK. Have you tried setting up the filter in conf\web.xml ?
shaki shabez
Greenhorn

Joined: Oct 05, 2011
Posts: 7
Tim Moores wrote:OK. Have you tried setting up the filter in conf\web.xml ?


yes , i have tried this :


but , i dont know where to put the filter files .
or , how to configure the above code to work on a filter on a specific web app .

i tried creating conf\classes\filters , and to put the files there , but it wont work . nither does putting the files under conf dir .

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
The class files must be somewhere where Tomcat can find them - for example, in TOMCAT_HOME/lib in a jar file. You can't just create directories and expect Tomcat to include those in its classpath.

how to configure the above code to work on a filter on a specific web app

I thought the whole point was to use these classes for *all* web apps? If you need to do app-specific setup then you might as well include the filter in each web-xml file.
shaki shabez
Greenhorn

Joined: Oct 05, 2011
Posts: 7
**EDIT :


The class files must be somewhere where Tomcat can find them - for example, in TOMCAT_HOME/lib in a jar file. You can't just create directories and expect Tomcat to include those in its classpath.


okie , apparently the server crashed or something i dont know . it seems that it wouldnt run at all .
i reinstalled the apache server , and now everything works including the filter .


altho i have just one little bug , i dont know if its my filter or just configuration problem .
when i try localhost:8080 , i can see my filter proccessing the request and finishing it . but i get a blank page instead of the tomcat home page .
and if i try localhost:8080/manager for instance .. then everything works as it should be , including my filter .

and idea ?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16160
    
  21

The Tomcat Manager app isn't some sort of magical construct that stomps on the J2EE spec and expects everything to work its way just because it doesn't want to do things the right way.

The Tomcat Manager app is, in fact, a perfectly normal J2EE-compliant webapp. The only difference between it and any other webapp is that it queries the Tomcat server's internal monitoring and control facilities, which themselves are J2EE and/or JRE compliant resources.

There are at least 2 ways to introspect Tomcat: The Management EJB (MEJB) and Java Management Extensions (JMX). In point of fact, there are more than 2 ways - maybe even more than 5 ways, depending on what you want to monitor. Stuff like the JSR-88 API.

None of these services require abnormal patches into Tomcat's internals, warping standard processes, misuse of classpaths or other arcane and over-complex attempts to create something that can break anytime a new Tomcat release comes out.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Global Filter for All Web Applications