filter not working in servlet - java

My application is served from this URL -http://t4.gav.com/gui
Now I have added a Servlet filter to filter all requests with /gui/* pattern.
<filter>
<filter-name>AccessCheckFilter</filter-name>
<filter-class>t4.AccessCheckFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AccessCheckFilter</filter-name>
<url-pattern>/gui/*</url-pattern>
</filter-mapping>
I'm not sure why mistake Im doing. If I just give root "/" its working.

If your application is served at http://t4.gav.com/gui most probably your context path of your application is /gui. Which means, whatever you configure on your Servlet Filter is relative to this context path. This is why / is working.
Based on your configuration the container will filter the requests on URL /gui/gui/*.

Related

How do I allow a servlet filter in the root context to work on other contexts?

I have a WAR file deployed to the root context (/) on Tomcat 8, which has a servlet filter attached to it. The filter checks to see if the user is logged in and if not redirects them to the login page. I also have other contexts deployed in the same Tomcat instance at /app1 and /app2. I want the filter in the root context to also apply to the other contexts, so I don't have to maintain three nearly identical filters (one for each context).
The problem is any URL that falls in the other contexts is not being handled by the filter in the root context. For example, the URI /foo/bar is caught by the filter (because it doesn't correspond to one of the other contexts), but /app1/foo/bar is not caught.
I've tried configuring the filter to catch everything by modifying the filter mapping in the web.xml file:
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
and I've also tried explicitly adding each context:
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
<url-pattern>/app1/*</url-pattern>
<url-pattern>/app2/*</url-pattern>
</filter-mapping>
but neither approach worked. I assume it's a security issue--the other contexts need to explicitly allow the root filter. How do I get this to work?
You can't get this to work. Web applications are independent. Requests are mapped to web applications before any Filters or Servlets are mapped.
You best bet with Tomcat (assuming you don't want multiple filter definitions) would be to re-implement your filter as a Tomcat Valve and configure the Valve on the Host.

google app engine java - redirecting based on domain

I have 2 domains for my site which is hosted in google app engine. I want to 301 redirect any request on the less desirable domain to the more desirable domain.
I have tried adding the following to my web.xml
<servlet>
<servlet-name>WebServlet</servlet-name>
<servlet-class>com.wmar.api.WebServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>redirectFilter</filter-name>
<filter-class>com.wmar.api.WebFilterRedirect</filter-class>
</filter>
<filter-mapping>
<filter-name>redirectFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
For the servlet it hits it, but then I don't know how to just load the static resource (ideally just passing onto some default static file handler) if the domain is correct.
For the filter it doesn't hit it.
What is the best way to achieve what I want with servlet 2.5 / google app engine java?
Thanks
I realize now that the current ideal solution is to create a separate app engine project and point non-canonical domains at it which has a simple servlet that just 301 redirects to the canonical domain.
For Http to https 301s app.yaml can be used with secure: always.
As noted by konqi there are undesirable side effects by trying to do it all in the one app engine project/module.

make servlet filter applicable for all web applications in EAR

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.
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>
If I put above code in Tomcat Home-> conf-> Web.xml then the filter was applicable for all the applications deployed in Tomcat.(This is what I want to achieve in Glassfish for a EAR file)
But now for some reasons I have decided to shift to Glassfish. And now I have created A EAR file which contains two applications(WAR) 1.Login and 2. Profiles. Filter is in com.filter package which is in Login Application and not present in Profiles.
My question is that where should I put above XML code, which will make sure that the filter is applicable for All applications(WARs) in the EAR?
Update1
If I put above XML code in defualt-web.xml which is under the directory glassfish\domains\domain1\config\ then it is applicable for Login Applicaion but not for the Profiles application which is in the same EAR file.
There is Error in the log:
Exception starting filter MyFilterName java.lang.InstantiationException
.....
.....
Caused by: java.lang.ClassNotFoundException: com.myfilter.MyFilterName
[Note: Filter is in the Login application(WAR) and not in the Profiles application(WAR)]
So how should I make this applicable to Profiles(all WARs in EAR).
Put the jar contains your filter into glassfish\domains\domain1\lib folder

How to create url mapping in web.xml

i want to create url pattern that lead to filter in jsf2.
I tried this code
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>www.mysite.com</url-pattern>
</filter-mapping>
but i don't get to my filter.
can you help?
thanks
You are expecting to map the URL path, i.e. the part of URL that follows host and your web application name.
This is how full URL looks like:
http://www.mysite.com:8080/myapp/path1/path2/path3
where:
8080 is a port - optional - default 80
myapp - the context path of your web application. It is empty if your application is default web application on your app server.
path1/path2/path2 the path. This is what you are mapping using <filter-mapping> tag.
So, if for example you want to pass through your filter all requests to JSP pages say:
<url-pattern>*.jsp</url-pattern>
If your UI is under directory ui and you want to filter such requests say:
<url-pattern>/ui/*</url-pattern>
etc.
I hope I get this right:
You want to point an URL to your already created ServletFilter in your JSF 2.0 web application?
You have to register your filter in your webapps web.xml file and map it to your desired URL, e.g.
<!-- register your filter -->
<filter>
<filter-name>YourFilterName</filter-name>
<filter-class>com.your.filter.class</filter-class>
</filter>
<filter-mapping>
<filter-name>YourFilterName</filter-name>
<!-- Mapped to any URL -->
<url-pattern>/*</url-pattern>
</filter-mapping>

Servlet filter url mapping

How can a filter be mapped to the root of a URL? I'm using Tomcat 7.0.2 and deploying an application as ROOT.war. The welcome page is sign_in.xhtml. I would like to run a filter whenever the client sends a request for the root of the site (i.e. the domain name only), or when the the client requests sign_in.xhtml. Here is what I have so far:
<filter>
<filter-name>My filter</filter-name>
<filter-class>com.myApp.myFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>My filter</filter-name>
<url-pattern>/sign_in.xhtml</url-pattern>
</filter-mapping>
Requests for sign_in.xhtml directly, successfully invoke the filter, but I'm not sure how to get requests for the root to invoke the filter. According to the Servlet spec (version 3.0)
<url-pattern>/</url-pattern>
maps to the default servlet, and an empty string maps to the root. Here's the relevant section from the spec:
"The empty string ("") is a special URL pattern that exactly maps to the
application's context root, i.e., requests of the form http://host:port//. In this case the path info is ’/’ and the servlet path and context path is
empty string (““)."
However, both of the following url patterns cause Tomcat to throw an exception.
<url-pattern></url-pattern>
<url-pattern>""</url-pattern>
I would really appreciate it if someone could shed some light on this. Thank You.
Andrew
The <url-pattern>/</url-pattern> should work for requests on the root. Did you try it?
If your intent is more to filter all requests, then you should use <url-pattern>/*</url-pattern>.
Update: To exclude one and other, I tested the url-pattern of / at Tomcat 7 (using both web.xml and #WebFilter(urlPatterns={"/"})) and it indeed didn't work as expected. The url-pattern of / however works as expected on Tomcat 6 (Servlet 2.5) and also on Glassfish v3 (Servlet 3.0). I suspect a bug in Tomcat 7, so I've reported issue 49914 about this.

Categories