• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to execute Multiple filters?

 
Greenhorn
Posts: 17
Tomcat Server Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
prerit datta
Greenhorn
Posts: 17
Tomcat Server Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So can you correct the above code so that it will work??
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
prerit datta
Greenhorn
Posts: 17
Tomcat Server Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you help me create it using the code above in my first post?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Certainly we can help you create, tell us what have you done as of now.
What is in your web.xml?
 
prerit datta
Greenhorn
Posts: 17
Tomcat Server Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What URL do you use to test this and what isn't working? What behaviour are you seeing?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 17
Tomcat Server Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
Tomcat Server Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
Tomcat Server Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both. Any JSP beans as well, if our app uses those. Basically, put all your classes in packages.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
Tomcat Server Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you put the classes into packages? Yes or no?
 
prerit datta
Greenhorn
Posts: 17
Tomcat Server Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
Tomcat Server Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic