Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet filter & mapping  RSS feed

 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick question, just been looking at Servlet 3.0 and was wondering....

What is the difference plus/minus between mapping a Servlet and Filter a Servlet?

I've always used mappings, and by the looks of the examples I can find Filters are pretty much the same.

Cheers

KS
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They're not even remotely alike. A mapping specifies what URL patterns are to be applied to a Servlet, a filter is a type of listener class thats invoked before/after another resource is executed.

Are you asking what the difference is between servlet mappings and filter mappings?
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it was the mappings I was talking about sorry, what's the difference between

<servlet-mapping>
and
<filter-mapping>

What confussed me as well is the way Servlets 3.0 uses Annotations to call these, I was reading that a Filter and Servlet seemed to be the same thing. If it helps this is the tutorial I was looking at:

http://today.java.net/pub/a/today/2008/10/14/introduction-to-servlet-3.html




Then a Servlet class is:




 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet mappings define what URLs will trigger the servlet, while the filter mappings server the same purpose for what requests the filter will be invoked upon.

This is not a new concept with Servlets 3 -- just the annotations (in lieu of web.xml entries) are.
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So just to dumb it down...

The filter-mapping simply maps a URI pattern (/foo) to the filter like the servlet-mapping would map it (/foo) to the Servlet?

So if I was to do a Filter:



and a Servlet:



and executed the URI '/foo' it would call the ServletFilter which would in turn do some 'stuff' under the doFilter method, then call the Servlet then return to the Filter before going off to wherever I have set?

example:
JSP --> ServletFilter --> Servlet --> <wherever>


(If would appear that a quick browse would show that the website I was using is already slightly out of date in the annotations!)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!