url-pattern /* serves jsp files and static assets as seperate requests - java

I have a simple servlet which renders different content based on different geo locations
for example:
example.com/us
example.com/de
example.com/uk
example.com/..
so actually the servlet should supports all geo locations as url-pattern. So thats why I defined the rule below in my web.xml file:
<servlet-mapping>
servlet-name>MyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
this is how the servlet look like:
public class MyServlet extends HttpServlet{
String showPage = "/pages/show.jsp";
public void doGet(HttpServletRequest request, HttpServletResponse response){
System.out.println("servlet initiated... ~> "+request.getRequestURI());
...
request.getRequestDispatcher(showPage).forward(request, response);
}
}
As soon as I try to forward the page, the servlet starts again and again; it tries to also handle the request /page/show.jsp.
So I get such an ouput when I try to access example.com/us:
servlet initiated... ~> /us
servlet initiated... ~> /page/show.jsp
Exception...
servlet initiated... ~> /page/show.jsp
Exception...
servlet initiated... ~> /page/show.jsp
...
It throws an exception because my servlet is not expecting such a url-pattern /page/show.jsp but it triggers because I have define /* url-pattern in my web.xml file.
Any idea? how can I skip or exclude the unwanted requests like the one above? Thanks.

I could not find a specific reference but I believe that "/*" root url pattern conflicts with the default mapping in the the global web.xml which could be contributing to your problem.
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- The mapping for the default servlet -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
try adding explicit jsp servlet tag and servlet mapping to avoid processing the jsp through the default mapping which is created with the "/" root url pattern. I'm not sure that this will solve your problem.
<servlet>
<servlet-name>showpage</servlet-name>
<jsp-file>/pages/show.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>showpage</servlet-name>
<url-pattern>/show</url-pattern>
</servlet-mapping>
Another more explicit solution would be to create a url pattern that differentiates all of the country geo requests.
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/geo/*</url-pattern>
</servlet-mapping>
So example.com/geo/us etc will map explicitly
I attempted the same global setting and had a similar problem where it recursively attempted the request and threw an exception over and over until I stopped the Tomcat server. The problem was resolved when used more specific url pattern.

Bart answer gave me the clue. Actually there are many ways to handle this, one is to use Filters and the other one is to define all the static routes in web.xml and instead of the /* I have used /.
So to resolve the issue I change the url-pattern as below:
<!-- static assets -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<!-- my servlet -->
<servlet-mapping>
servlet-name>MyServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
You also don't need to define the pattern for org.apache.catalina.servlets.DefaultServlet its already there by default.
So After above changes everything works fine.

When resolving pages/show.jsp the container will match on the longest path first then look at the file extension (.jsp) afterwards. So pages/show.jsp matches /* and therefore the container loads the Servlet again and doesn't initiate the JSP engine servlet.
Are all your jsps in a pages directory? Maybe you can add that mapping to your web.xml
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>/pages/*</url-pattern>
</servlet-mapping>

Per the servlet specification (2.5 here, but it should be the smae for newer version) :
The container will try to find an exact match of the path of the request to the
path of the servlet. A successful match selects the servlet.
The container will recursively try to match the longest path-prefix. This is done
by stepping down the path tree a directory at a time, using the ’/’ character as
a path separator. The longest match determines the servlet selected.
If the last segment in the URL path contains an extension (e.g. .jsp), the servlet
container will try to match a servlet that handles requests for the extension.
An extension is defined as the part of the last segment after the last ’.’ character.
If neither of the previous three rules result in a servlet match, the container will
attempt to serve content appropriate for the resource requested. If a "default"
servlet is defined for the application, it will be used.
(source : servlet-spec-2.5)
So for your case, it should be sufficient to add a more precise mapping (as said by Si Kelly) :
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>/pages/show.jsp</url-pattern>
</servlet-mapping>

Related

How to avoid matching paths that don't have a slash with servlet-mappings?

I'm trying to set up a servlet so that any requests for /foo/* will go to my Foo servlet, except for requests in the form of /foo/bar/*, which go to the Bar one. However, I want /foo/bar to go to the Foo servlet, not the Bar one. Is there a way to do this with just url-patterns in web.xml?
My mappings:
<servlet-mapping>
<servlet-name>Bar</servlet-name>
<url-pattern>/foo/bar/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Foo</servlet-name>
<url-pattern>/foo/*</url-pattern>
</servlet-mapping>
I've tried removing the asterisks and trying a few other patterns, but the only way I can see to do this is to have a specific mapping for /foo/bar, though it seems like there should be a better way.
If you are using servlet specification v2.5 or above then you could provide multiple url-pattern elements for the same servlet-mapping like:
<servlet-mapping>
<servlet-name>Foo</servlet-name>
<url-pattern>/foo/*</url-pattern>
<url-pattern>/foo/bar</url-pattern>
</servlet-mapping>

Why do we need a servlet name?

I have been readin Head First JSP and Servlet, I see that the web.xml has
<!-- To name the servlet -->
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>packy.FirstServlet</servlet-class>
</servlet>
<!-- For URL's to map to the correct servlet -->
<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/ServletURL</url-pattern>
</servlet-mapping>
Why hide the original servlet's location ? I can simply see that it is for security reason and some more such kinda advantages, but why have a name for each servlet ? Why can't the web.xml be simple like
<servlet>
<url-pattern>ServletURL</url-pattern>
<servlet-class>packy.FirstServlet</servlet-class>
</servlet>
It allows you to have multiple servlet mappings on a single servlet instance (even spread over multiple web.xml/web-fragment.xml files) without the unnecessary need to create a separate instance per mapping:
<servlet>
<servlet-name>someServlet</servlet-name>
<servlet-class>com.example.SomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>someServlet</servlet-name>
<url-pattern>/enroll</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>someServlet</servlet-name>
<url-pattern>/pay</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>someServlet</servlet-name>
<url-pattern>/bill</url-pattern>
</servlet-mapping>
(note: yes, you can have multiple URL patterns per mapping, but that wouldn't cover them being split over multiple web.xml/web-fragment.xml files)
It allows you to map filters on the particular servlet without worrying about what URL patterns the servlet is/would be using:
<filter-mapping>
<filter-name>someFilter</filter-name>
<servlet-name>someServlet</servlet-name>
</filter-mapping>
Your proposal would support neither of them.
Note that since Servlet 3.0, which is out for almost 4 years already (December 2009; please make sure that you learn the matters by up to date resources ... anything older than 1~3 years should be carefully reviewed), you can easily use the #WebServlet annotation to minimze web.xml boilerplate:
#WebServlet("/servletURL")
public class SomeServlet extends HttpServlet {}
Just solely this annotation already maps it on an URL pattern of /servletURL without any web.xml entry.
We don't really need a servlet name. It's just that this is how the Java EE designers chose to declare and map servlets in XML.
Nowadays, you can declare and map a servlet using the #WebServlet annotation, and the name attribute of this annotation is optional.

Servlet mapping direct JSP files rather than using a Servlet to manage the URL requests

I am redoing the URL mapping structure within my Java web application. I am trying to find the most efficient and proper way to map the servelets and resources to their proper URLs.
There are two strategies that I have been able to create, but I am not sure which is more efficient.
Mapping All urls to one Servlet which Handles the requests
In this case I have a Servlet named "URL", with the following servlet mapping:
<servlet-mapping>
<servlet-name>url</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The url Servlet is set up like such as an example and works fine:
String task = request.getRequestURI().substring(request.getContextPath().length());
if ("/home".equals(task)){
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/home.jsp");
dispatcher.forward(request, response);
}
The problem I initially had with this that all the static resources such as JS, Images, etc... weren't served. I had the option to create separate directories for the static content as a solution, but off the top of my head I switched to mapping it all directly in the web.xml.
Mapping it all directly in the web.xml.
In this case the url patterns are directly mapped to the JSPs and Servlets like so:
<servlet>
<servlet-name>home</servlet-name>
<jsp-file>/jsp/Home.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
Example:
localhost:8080/home <- home.jsp
localhost:8080/about <- about.jsp
localhost:8080/login <- doLogin servlet
Are these bad? Which would be a more efficient and proper way to map the urls to their intended JSP files and Servlets?
I use a combination of both, I define all the static pages in web.xml and right at the end of the web.xml, I create a catch-all that will handle dynamic pages.
So home, about, login, etc are all static pages, define them in web.xml
Something like account/abc and blog/some-random-article is handled dynamically.
<servlet>
<servlet-name>NotFound</servlet-name>
<servlet-class>com.site.PageNotFoundServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NotFound</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
If in your servlet code, if you don't know how to handle the url, in other words the url is something like /asdfadfasdf which you don't handle, throw a 404 back, if the url starts with /blog (from /blog/random-article), go to the blog page with the random-article as the content.
The case of "home.jsp" is not same here
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/home.jsp")
and
<servlet>
<servlet-name>home</servlet-name>
<jsp-file>/jsp/**Home.jsp**</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>

Servlet mapping: url-pattern for URLs with trailing slash

I have a problem related to the servlet mapping. I have the following in web.xml:
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>test.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
If I access to http://localhost:<port>/MyApp/HelloWorld the servlet HelloWorldServlet is called.
I also want my servelet to respond to http://localhost:<port>/MyApp/HelloWorld/. How can I achieve this effect? I'm developing with NetBeans but it does not allow me to put a pattern ended with /.
After you've added your wildcard on your <url-pattern>
<url-pattern>/HelloWorld/*</url-pattern>
You can get the extra path associated with the URL by using HttpServletRequest.getPathInfo().
E.g.
http://localhost:<port>/MyApp/HelloWorld/one/
The result will be
/one/
From the JavaDoc:
Returns any extra path information
associated with the URL the client
sent when it made this request. The
extra path information follows the
servlet path but precedes the query
string and will start with a "/"
character.
Use a wildcard. You can redirect all the traffic going to a specific URL to the same servlet. For example, you can add the following:
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorld/*</url-pattern>
</servlet-mapping>
This will redirect the URL with a slash to your original servlet.
One thought - this would redirect anything to this URL pattern to the servlet. If you want to have other URL's past this URL, you should create a servlet that will redirect to the correct URL (by looking at the URL specified). Alternatively, you could use a framework that provides mapping for you.

How can I find out a servlet's URL?

Let's say I have this in my web.xml:
<servlet>
<description></description>
<display-name>MainServ</display-name>
<servlet-name>MainServ</servlet-name>
<servlet-class>MainServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainServ</servlet-name>
<url-pattern>/MainServ</url-pattern>
</servlet-mapping>
Imagine I'm in that servlet's doGet method. Is there anyway of getting at the /MainServ value?
Via HttpServletRequest.getServletPath; from the Javadoc:
This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string.
E.g.:
String path = req.getServletPath();
...if you've called the first argument to doGet req.

Categories