wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to execute Multiple filters? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to execute Multiple filters?" Watch "How to execute Multiple filters?" New topic
Author

How to execute Multiple filters?

prerit datta
Greenhorn

Joined: Jun 26, 2013
Posts: 16

I'm learning filters and I had a doubt. Say I have 4 filter classes f1,f2...f4. I want them to execute in sequence before the control being finally passed on to MyServlet servlet. I'm unable a create a proper mapping in web.xml.
Here's my code.

index.html The welcome page



f1.java



f2.java
//Has the same coding as above except the line pw.println("Filter2 is working....................");

The same goes for f3 and f4

Here is my MyServlet.java:-




Please help how to do this. Specially the web.xml file mapping.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Your form action will have nothing at all to do with the filters; it should be the URL of the servlet.

When properly configured, the filters will automatically be executed when the servlet is invoked. You do not need to address the filters in the action URL.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
prerit datta
Greenhorn

Joined: Jun 26, 2013
Posts: 16

So can you correct the above code so that it will work??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41137
    
  45
The way to make it work is to set up the servlet and the filter properly in the web.xml. How have you done that?


Ping & DNS - my free Android networking tools app
prerit datta
Greenhorn

Joined: Jun 26, 2013
Posts: 16

Can you help me create it using the code above in my first post?
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Certainly we can help you create, tell us what have you done as of now.
What is in your web.xml?


SCJP, SCWCD.
|Asking Good Questions|
prerit datta
Greenhorn

Joined: Jun 26, 2013
Posts: 16

Here's what I have done:


I kept url pattern same for all the filters so that when I map my Servlet to it, it can execute all the filters. Somehow this approach doesn't work.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9953
    
161

What URL do you use to test this and what isn't working? What behaviour are you seeing?

[My Blog] [JavaRanch Journal]
Balaji Vankadaru
Ranch Hand

Joined: May 31, 2013
Posts: 47


You need to define the complete class name along with the package.


The Execution of filters happen as per their declaration in web.xml file

and the order of filter mapping in a proper way to have fruit full execution
prerit datta
Greenhorn

Joined: Jun 26, 2013
Posts: 16

If you have read the posts above this is what I need help for.... Now if you will be kind enough to create web.xml file for the above....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

prerit datta wrote:Now if you will be kind enough to create web.xml file for the above....

It is not anyone else's job to write your code for you. You write the web.xml, and we'll help you with it.
prerit datta
Greenhorn

Joined: Jun 26, 2013
Posts: 16

Again I would like to point out PLEASE READ THE POST'S ABOVE because I have already written what I have in my web.xml.

But your's sake here it is again:

web.xml

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

It does not look like you have acted upon the suggestion to place classes in a package and to be sure that those package names are reflected in the deployment descriptor.
prerit datta
Greenhorn

Joined: Jun 26, 2013
Posts: 16

Bear Bibeault wrote:It does not look like you have acted upon the suggestion to place classes in a package and to be sure that those package names are reflected in the deployment descriptor.


You mean the Servlet class or the Filter classes??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41137
    
  45
Both. Any JSP beans as well, if our app uses those. Basically, put all your classes in packages.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9953
    
161

prerit datta wrote:Again I would like to point out PLEASE READ THE POST'S ABOVE

I don't know what you are shouting about. You haven't yet told us what exact problem you are running into and you haven't even replied to the questions that have been asked - like what URL are you using.
prerit datta
Greenhorn

Joined: Jun 26, 2013
Posts: 16

Jaikiran Pai wrote:
prerit datta wrote:Again I would like to point out PLEASE READ THE POST'S ABOVE

I don't know what you are shouting about. You haven't yet told us what exact problem you are running into and you haven't even replied to the questions that have been asked - like what URL are you using.


I simply want to know how can I execute four filters f1 to f4 as above in sequence before I finally execute my servlet. I'm having problem in coding the web.xml to solve this problem. Please help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Have you put the classes into packages? Yes or no?
prerit datta
Greenhorn

Joined: Jun 26, 2013
Posts: 16

Bear Bibeault wrote:Have you put the classes into packages? Yes or no?


As you said I have put all my servlets and filters under a folder called "pack" which is in /WEB-INF/classes/pack. And here is my xml file:



I hope it's ok. Now how should i proceed?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

When I want the filters to "wrap" a servlet, I use <servlet-name> in the filter mapping rather than a URL pattern. E.g.
prerit datta
Greenhorn

Joined: Jun 26, 2013
Posts: 16

Bear Bibeault wrote:When I want the filters to "wrap" a servlet, I use <servlet-name> in the filter mapping rather than a URL pattern. E.g.


I followed your advice and made the following changes:



Still confused about the <servlet-mapping> <url-pattern> section above and one more thing what to write in form action=" ?? " in index.html??

Thanks you again for your patience and time sir.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

What does the servlet do? Name the url pattern according to that. If it "saves trees", you could name it "/save-tree". If it "displays rabbits", you could use "/display-rabbit".

In the form action you would use that mapping, preceded by the context path: action="${pageContext.request.contextPath}/save-tree"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to execute Multiple filters?
 
Similar Threads
HTTP Status 404 - /HelloWorld
javascript and servlet
program for displaying request details
filters vs RequestDispatcher's include() method
Displaying using constructor