Reputation: 6181
I am developing a web application using JSP & Servlets.
I have developed a servlet filter
for Login
purpose. Which checks whether user is logged in or not, if user is logged in then it allows access to the requested resource, else it redirects request to the Login page
. And it works perfectly.
Filter
public class MyFilter implements Filter
{
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
//code here
}
}
web.xml
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.myfilter.MyFilter</filter-class>
<init-param>
<param-name>PARAM_NAME_HERE</param-name>
<param-value>PARAM_VALUES_HERE</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Now I have deployed this application named Login
on Tomcat 7
. This servlet filter
works perfectly for the resources (JSPs, Servltes, .css, .js) in this web application.
So now in Tomcat
there are other web applications like Profiles
, Distribution
etc. So now what I want is apply the Filter
in Login
application Applicable for all other applications deployed in the Tomcat
. Means every request to other application resources should go through the Filter
which I have developed.
So any suggestions will be appreciated!
Edit1
I dont want to put package which contains Filter
in other applications. Because in Tomcat server there are more than 10 applications are deployed, and some more are going to be deployed. So suppose if I put the package which contains Filter
in all other applications and do servlet mapping , but if some days after I want to make some changes in the Filter then I will need to make changes in all applications, and redeploying them will be needed.
Upvotes: 5
Views: 2876
Reputation: 19000
Put your <filter>
and <filter-mapping>
definition inside $CATALINA_HOME/conf/web.xml
These definitions are shared by all you web applications.
You will have to package all your login related logic in a jar and put it under $CATALINA_HOME\lib
so it will be accessible to all of your web applications.
Upvotes: 3
Reputation: 26518
Add this your code into the web.xml of other two applications.
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.myfilter.MyFilter</filter-class>
<init-param>
<param-name>PARAM_NAME_HERE</param-name>
<param-value>PARAM_VALUES_HERE</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Filter
public class MyFilter implements Filter
{
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
//code here
}
}
Upvotes: 1