1

So, now that I have moved my JSP files to /WEB-INF/content from /content after coding my ProcessServlet to use forward() to get to them, how should I set up my web.xml URL pattern to get to the Servlet?

Note: My JSPs were under /content along with CSS, image and JS files. So /content/css, /content/image, /content/js are all still there.

I found that if I use the pattern "/content/*" in web.xml for my Servlet then requests for css, images and js all go through the Servlet as well. How should I avoid this?

Can someone suggest a better way to set up my URLs and directories?

Matthew
  • 8,183
  • 10
  • 37
  • 65

3 Answers3

2

2 options:

  1. Move /content/css, /content/image, /content/js to /resources/css, etc. To fix URLs in existing JSPs, just use find&replace the smart way. Should be a minute work.

  2. Change servlet's URL pattern /content/* to something else, e.g. /pages/*.

    • If you want to keep your existing URLs, add a filter on /content/* which does basically the following:

      String uri = ((HttpServletRequest) request).getRequestURI();
      if (uri.startsWith("/content/css/") || uri.startsWith("/content/image/") || uri.startsWith("/content/js/")) {
          chain.doFilter(request, response); // Goes to default servlet.
      } else {
          request.getRequestDispatcher("/pages" + uri).forward(request, response);
      }
      

    This is only a drastic change. You'd probably need to fix all links in JSPs, for sure if they are not designed the way that there's a master template wherein you've specified <base> in a single location. Also, you might want to add 301 redirects for old bookmarks and search indexes.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, that gives me a lot of options to work with. I'll keep complaining about this [link](http://stackoverflow.com/questions/8073945/processservlets-and-jsps-a-simple-request) when ever I can though. – Matthew Nov 11 '11 at 20:42
1

You don't have to use a servlet for such forwarding. If the number of jsp pages is not large, you can declare mapping for them directly in the web.xml, just as you'd do for servlets (See example here)

Yoni
  • 10,171
  • 9
  • 55
  • 72
  • 150 JSP files currently and this number is only going to grow. I like my Servlet since it is a central location for handling common tasks. – Matthew Nov 11 '11 at 19:55
1

This is how I setup mine:

+-WEB-INF/
|   +-jsp/*.jsp
+-styles/*.css
+-images/*.jpn,*.png,etc.

I use servlet mapping to map dynamic contents, e.g. *jsp, and leave the default servlet to deal with the static contents. Of course, this is not the only way to solve problem.

web.xml looks like this:

<servlet-mapping>
    <servlet-name>Your Servlet</servlet-name>
    <url-pattern>/content/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

The default servlet is provided by most servelt container and you do not need to write one. The keyword is "default" for tomcat and jetty.

gigadot
  • 8,879
  • 7
  • 35
  • 51
  • So, what is the entry for your URL pattern to get to your Servlet? Mine is: /content/* – Matthew Nov 11 '11 at 19:58
  • basically, there is no change to your pattern but you will need to change the urls of those to the static contents, e.g. /content/images/ to /images/ – gigadot Nov 11 '11 at 20:04
  • Also, why do you need a Servlet at all for CSS, images, js, etc? Isn't that just extra work that Java has to do? I eventually want to have regular Apache serve all those static resources (right now everything is going through Tomcat). – Matthew Nov 11 '11 at 20:06
  • Arg, I was hoping you wouldn't say that. I realized this as a possible outcome. This means I have to change all URL references to my static content such as stylesheets, images and js. I like my URL structure as it is, can I not keep it? – Matthew Nov 11 '11 at 20:09
  • 1
    In servlet container, everything must pass through a servlet regardless even the static content. if you define no mapping, the default servlet is automatically used to map everything. This is the same for apache but there is no such a thing as servlet. In apache httpd, it has its own thing to pipe the images (static contents) from hard disk into your the http connection. – gigadot Nov 11 '11 at 20:12
  • Just to be sure, Apache HTTPD _will_ serve these static resources faster, right? I plan on doing that later if it is worth it. – Matthew Nov 11 '11 at 20:15
  • i believe so too from my experience but you should rely on evidence this this case. However, if you split the static contents from your servlet container, it can become to difficult to port from one machine to another. – gigadot Nov 11 '11 at 20:19
  • All this trouble because Servlets don't have a passthrough mechanism as I found out in my other question here: [link](http://stackoverflow.com/questions/8073945/processservlets-and-jsps-a-simple-request) but apparently all these work arounds are the _simpler_ way to go. – Matthew Nov 11 '11 at 20:25