Servlet Filter Questions

A

Aquila Deus

1.How to set the order of filters? Any method other than the order of
<filter-mapping> in web.xml?

2.Since filters can also modify the response, why use "servlet"? Why
not simply use all-filters and let the last filter to output contents?
I think this is more flexible because you may also need to "filter"
things (such as debug info) after the page output.

3.Why not send to all requests to one servlet and let it decide who and
how to filter and output the contents? I do this all of time in php
(all links point to /index.php?page=XXX...)
 
R

Ryan Stewart

Aquila Deus said:
1.How to set the order of filters? Any method other than the order of
<filter-mapping> in web.xml?
No, that's it basically. See section 6.2.4 of the Servlet spec for details.
2.Since filters can also modify the response, why use "servlet"? Why
not simply use all-filters and let the last filter to output contents?
I think this is more flexible because you may also need to "filter"
things (such as debug info) after the page output.
Because they are two different things, and filters do not output things. They
filter things. Why would you need to "filter" debug info? And Filters can be
applied that will do their work after the servlet(s) is/are invoked.
3.Why not send to all requests to one servlet and let it decide who and
how to filter and output the contents? I do this all of time in php
(all links point to /index.php?page=XXX...)
Because that would be a kludgy, short-sighted solution in most applications.
Word of the day for today: abstraction.
 
A

Aquila Deus

Ryan said:
No, that's it basically. See section 6.2.4 of the Servlet spec for details.

Aww

Because they are two different things, and filters do not output things. They
filter things.

But they can still output, right?
Why would you need to "filter" debug info? And Filters can

"filter" -> output

output debug info, convert hyperlink and encoding, etc.
be applied that will do their work after the servlet(s) is/are
invoked.

Um, but why not just let one of filters to output the page?

For example, a request is being processed as:

auth -> pre-process jsp -> process jsp -> validate the outputed xml ->
XSLT transform -> append debug and trace info -> convert encoding to
utf8 -> compress.

Then which step should be put into servlet? I know most ppl would
answer the "process jsp", but the definition is vague, since each of
all steps may output or filter the request.
Because that would be a kludgy, short-sighted solution in most applications.
Word of the day for today: abstraction.

But it's not abstraction to separate one thing to differnt ones which
function almost the same :)
 
R

Ryan Stewart

Aquila Deus said:
But they can still output, right?
Since it is passed the ServletResponse, you can make changes to the response
just as well in a Filter as anywhere else. Would it be good form? No.
"filter" -> output

output debug info, convert hyperlink and encoding, etc.
I'm still not sure what you're saying here.
invoked.

Um, but why not just let one of filters to output the page?
Because, again, that is not what they are for.
For example, a request is being processed as:

auth -> pre-process jsp -> process jsp -> validate the outputed xml ->
XSLT transform -> append debug and trace info -> convert encoding to
utf8 -> compress.

Then which step should be put into servlet? I know most ppl would
answer the "process jsp", but the definition is vague, since each of
all steps may output or filter the request.
I disagree. Without being familiar with the processes, one possibility would be:
auth: Filter
pre-process jsp: Servlet? (What does this mean?)
process jsp: Servlet? (Same question. Are you aware that a JSP *is* a servlet?)
validate output: Servlet indirectly, as an instance of a Validator class
XSLT transform: Same as above maybe, but I'm not so familiar with this
Append debug and trace info: Should be integral with the other parts of the
process, but if not, same as previous two steps
Convert encoding: Filter
Compress: Filter
But it's not abstraction to separate one thing to differnt ones which
function almost the same :)
If you mean Filters and Servlets, read their docs. They are not intended to
function the same. They have very distinct roles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top