Tomcat, web.xml, exclude path - tomcat6

In my application, i need to process all the requests from users in a single servlet, however, i happen to have a folder with static content, which i would like to also serve statically.
In my web.xml file, i have the following:
<servlet>
<servlet-name>all</servlet-name>
<servlet-class>a.b.c.WidgetlistXml</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>all</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Is there a way i can exclude, say, all .zip files from this?
PS. I know this has been asked on StackOverflow before, but the latest posts were from about 2006, and they also concerned Spring or other frameworks. I use none, and since 2006 something could have changed in url-patterns. By the way, finding the docs on web.xml is not that simple either.
Thank you in advance.

Set a DefaultServlet for your static contents. Here is an example:
http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html#where

Related

Alternate servlet definition to web.xml?

I am using google app engine, and I define all my servlet paths in web.xml. Is there a way to define these in code instead of xml? I ask because the web.xml file is very verbose and I have many servlets. If I could register the servlets in java code somewhere I can probably shorten things.
Thanks
You can define a "main" servlet whose job it is to dispatch to other servlets; in your web.xml, you would write something like:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>com.mydomain.myapp.Dispatcher</servlet-class>
</servlet>
</web-app>
This would map /* (your entire app) to your dispatcher class. You could then register your various servlets with the dispatcher under different paths (e.g. you could use the Spring DispatcherServlet or your own).

Mapping two servlets in web.xml where one URL pattern is substring of the other

I have a web application where I would like to tie a JSP to address http://host:port/status and a servlet to addresses like http://host:port/status/.... Is this possible? According to this article it should be possible ("container prefers an exact path match over a wildcard path match") at least for some containers and the Java Servlet Specification contains similar examples (albeit without wildcard, on p. 12-123 in April 2013 version), but if I try the following in web.xml it appears as if the JSP is never called and all requests (also to http://host:port/status) are routed to the servlet. My JSP and servlet are hosted on Google App Engine.
<servlet>
<servlet-name>Status</servlet-name>
<jsp-file>/Status.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>Status</servlet-name>
<url-pattern>/status</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>StatusUpload</servlet-name>
<servlet-class>com.example.StatusUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StatusUpload</servlet-name>
<url-pattern>/status/*</url-pattern>
</servlet-mapping>
Instead of mapping the same url to two different JSPs/Servlets in web.xml you can use a URL Rewriting filter like Tuckey UrlRewriteFilter which uses a configuration file urlrewrite.xml which would also be placed in WEB-INF. It uses regex in the rules.
These two rules should do what you want:
<rule>
<from>^/status$</from>
<to>/Status.jsp</to>
</rule>
<rule>
<from>^/status/(.*)$</from>
<to>/StatusUpload/?param=$1</to>
</rule>
Then in WEB-INF you would not map the JSP anymore but would map the Servlet to StatusUpload. When the user goes to /status/postfix the URL Rewriting filter will forward to the servlet (with the postfix part passed as a parameter) in the backend without the address the user sees in the address bar changing.

Jersey 2.3.1 on Google App Engine

I wonder if last version of Jersey does have support of Google App Engine.
I have found 'gae-integration' project (https://github.com/jersey/jersey/tree/master/incubator/gae-integration) with a link to Jersey 2.3.1. Actually all my attempts failed but maybe someone was luckier?
Thanks in advance!
I struggled to get Jersey 2 to work with GAE but figured it out now.
Tested OK with GAE SDK 1.9.10 and Jersey 2.12, including multipart/form-data. See for instance this blog article.
In Jersey 2 you have to enable features in your web.xml which is automatically enabled in Jersey 1. For example the snippet below enables JSP page support and the multipart/form-data MIME type features. (I don't think the GaeFeature is required, but haven't tested without it).
<servlet>
<servlet-name>com.namibiaonthenet.www</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.namibiaonthenet.www</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
org.glassfish.jersey.server.gae.GaeFeature;
org.glassfish.jersey.media.multipart.MultiPartFeature;
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
To enable the multipart/form-data feature an additional short config. file is required in your project - for details see my and #yves' answers here.
If you still struggle, let me know in a comment to this answer.

Servlet works on debug mode but fails to work when deployed to AppEngine (Appspot)

I have a gwt application that have some servlet on it. During development mode, the servlet can be accessed properly but when deployed I can't access it like:
Debug mode:
http://127.0.0.1:8888/mygwtapp/greet (Works)
Deployed in Appspot.com
http://mygwtapp123456.appspot.com/mygwtapp/greet (Does not work)
What could be the problem?
Here's the mapping on the web.xml
<servlet>
<servlet-name>greet</servlet-name>
<servlet-class>com.mygwtapp.server.GreetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>greet</servlet-name>
<url-pattern>/mygwtapp/greet</url-pattern>
</servlet-mapping>
Why aren't you using http://mygwtapp123456.appspot.com/mygwtapp/greet when deployed, when this is what you mapped in your web.xml?
Are you sure you've updated your default version in appengine control panel? If you updated version id in appengine-web.xml, but haven't set that as default, your appspot could be serving a web.xml that doesn't have your updated servlet.
If this is not the issue, please respond with any additional logging / error messages you encounter.

How to make GAE auto process an action

I use GAE and struts. When I input htpp://localhost:8888, it will autoload index.jsp.
What I need is for it to process an action immediately.
For example, when I input htpp://localhost:8888, it will autoload action "index.a" instead of autoloading the index.jsp page.
I tried using struts without GAE and it worked correctly. But after I added GAE to my project it would't work. What is the problem and how do I correct it?
Try adding <welcome-file-list> to your web.xml file:
<welcome-file-list>
<welcome-file>index.a</welcome-file>
</welcome-file-list>

Resources