0

My JSP project is the back-end of a fairly simple site with the purpose to show many submissions which I want to present on the website. They are organized in categories, basically similar to a typical forum.

The content is loaded entirely from a database since making separate files for everything would be extremely redundant.

However, I want to give the users the possibility to navigate properly on my site and also give unique links to each submission.

So for example a link can be: site.com/category1/subcategory2/submission3.jsp

I know how to generate those links, but is there a way to automatically redirect all the theoretically possible links to the main site.com/index.jsp ? The Java code of the JSP needs access to the original link of course.

Hope someone has an idea.. Big thanks in advance! :)

DragonGamer
  • 834
  • 3
  • 9
  • 27
  • Have you considered using a Filter? – rickz Oct 07 '15 at 01:41
  • @rickz Thank you for the suggestion. Thought about that but hoped there would be an easier way. However, I've created a Servlet which handles every site with a `/*` pattern and that actually works ...almost! The code `request.getRequestDispatcher("/index.jsp").forward(request,response);` in the doGet() function of the server forwards every link correctly but it crashes the site because all the resources on my page are going through this filter, including external .js files and images. I've added a condition `if (!requestURL.contains("."))` but that seems to solve the issue only partially... – DragonGamer Oct 07 '15 at 03:42
  • Nevermind, solved it with help of the responses here: http://stackoverflow.com/questions/3655316/browser-cant-access-find-relative-resources-like-css-images-and-links-when-cal – DragonGamer Oct 07 '15 at 04:01

1 Answers1

0

Alright, in case someone stumbles across this one day...

The way I've been able to solve this was by using a Servlet. Eclipse allows their creation directly in the project and the wizard even allows you to set the url-mapping, for example /main/* so you don't have to mess with the web.xml yourself. The doGet function simply contains the redirection as follows:

request.getRequestDispatcher("/index.jsp").forward(request,response);

This kind of redirection unfortunately causes all relative links in the webpage to fail. This can be solved by hardlinking to the root directory for example though. See the neat responses here for alternatives: Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

Community
  • 1
  • 1
DragonGamer
  • 834
  • 3
  • 9
  • 27