Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between servlet/servlet-mapping and filter/filter-mapping?

As part of exploring/learning Struts2, JSP and Servlets, I see from here and there that servlets and servlets-mapping can be used in web.xml. However, Struts2 mentions filters and filter-mapping too for web.xml.

What is the difference between both? Are these mutually exclusive? When should I use which and why? Can someone clarify the concepts? Thanks.

CLARIFICATION

I just got to understand that I needed to understand how Struts2 and Servlets are related: http://www.coderanch.com/t/57899/Struts/Difference-between-servlet-struts

like image 989
Jérôme Verstrynge Avatar asked Sep 26 '11 14:09

Jérôme Verstrynge


People also ask

What is the difference between filter and servlet?

Filter provides functionality which can be “attached” to any web resource. Servlet used for performing action which needs for particular request as user login, get response based on user role, interacts with database for getting data, business logic execution, and more.

What is servlet and servlet mapping?

What is servlet mapping? Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets. When there is a request from a client, servlet container decides to which application it should forward to.

What is filter mapping?

A filter mapping matches a filter to a web component by name, or to web resources by URL pattern. The filters are invoked in the order in which filter mappings appear in the filter mapping list of a WAR.

What is filter and filter mapping in web xml?

The filter-mapping element maps a URL pattern or servlet name to an instance of a filter. The filter-mapping always contains a filter-name element and a url-pattern element. The filter-name element must match a filter-name defined in a filter element elsewhere in the web. xml file.


2 Answers

Servlet filters implement intercepting filter pattern. While servlet is the ultimate target of web request, each request goes through a series of filters. Every filter can modify the request before passing it further or response after receiving it back from the servlet. It can even abstain from passing the request further and handle it completely just like servlet (not uncommon). For instance caching filter can return result without calling the actual servlet.

like image 197
Tomasz Nurkiewicz Avatar answered Sep 23 '22 11:09

Tomasz Nurkiewicz


Filters are used like Servlet Filters. For example, if you need to do security checks on certain URLs then you can add a filter for those pages. For instance, you can say /secure/pages/*.do needs to be intercepted by securityFilter. Then the doFilter() method of the SecurityFilter class (a class that implements the Filter interface) will handle the security audit before forwarding it to the actual requesting servlet.

Servlets are pretty much the standard stuff. You define a servlet and then let the servlet container know what type of requests needs to be mapped to that servlet.

They are not mutually exclusive. They both can be used at the same time. Think of filter like the way the word means - it "filters" things (logging, security,etc) before proceeding to the next servlet/action.

like image 40
CoolBeans Avatar answered Sep 23 '22 11:09

CoolBeans