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.
Related
I have the following problem:
In my web.xml I define how to serve pictures like so:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
On Tomcat this works fine but on IBM Websphere 8.5.5.9 I get the following error:
"SRVE0303E: Servlet name for the servlet mapping *.css could not be found."
On another site I already found a solution for this issue (https://www.ibm.com/developerworks/community/forums/html/topic?id=5f4420ba-0754-43fe-8c87-91acc588d9fc) so I also created the ibm-web-ext.xml exactly the same as in their solution but the error still persists.
Does anyone know what I could do differently?
I found the answer to my problem, thanks to #MigratedPigeon because he got me thinking about the class of my default servlet.
A tomcat server has a default servlet, the class for tomcats default servlet is
org.apache.catalina.servlets.DefaultServlet
Websphere on the other hand does not have a default servlet, thats why I get the error "Servlet Name could not be found".
As in the answer I linked in the original question, static file serving can be activated by websphere by using the web-ext.xml file but that still did not solve the issue of my web.xml file having a "default" servlet.
In our application we use spring, so in the end I replaced the default servlet in web.xml with springs dispatcher servlet and now my web.xml file is valid for both, tomcat and websphere.
you should also mention this in your web.xml
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>your servlet class</servlet-class>
</servlet>
Servlet mapping is done based on the value mentioned in the "" tags and respective servlet will be called.
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.
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/*.
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>
I have a JavaEE 1.4 web application running on WebSphere Application Server 6.0. In web.xml, there is a servlet configured to intercept all server requests:
<servlet-mapping>
<servlet-name>name</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
This works fine until I try to request something ending with *.jsp. In this case, server tries to find JSP with this name and fails with the error:
java.io.FileNotFoundException: JSPG0036E: Failed to find resource /cfvct/search_criteria.jsp
at com.ibm.ws.jsp.webcontainerext.JSPExtensionProcessor.findWrapper(JSPExtensionProcessor.java:279)
at com.ibm.ws.jsp.webcontainerext.JSPExtensionProcessor.handleRequest(JSPExtensionProcessor.java:261)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3226)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:253)
at com.ibm.ws.webcontainer.VirtualHost.handleRequest(VirtualHost.java:229)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1970)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:120)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:434)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:373)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:253)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminaters(NewConnectionInitialReadCallback.java:207)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:109)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:566)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java:619)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:952)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1039)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1475)
I need to have this request processed by the servlet, but seems server uses some JSPExtensionProcessor to process all paths ending with .jsp. Is there any way to change this behaviour?
Yes, you'll need to map your servlet to *.jsp in order to get *.jsp support redirected to your servlet.
<servlet-mapping>
<servlet-name>name</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
It is normally a bad idea to have jsps accessible directly, however. Placing them in WEB-INF in some directory, then mapping an appropriate url (.do, .action, etc) to a servlet that then redirects internally to that JSP is the better practice.
So instead of typing thisUrl.jsp, the user would type thisUrl.do or thisUrl.action, and it would then get hit by the servlet to redirect to thisUrl.jsp.