Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mapping *.jsp to *.html

 
Arron Ferguson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not entirely sure how to make all requests to JSPs map directly to the HTML extension. For example, invoking index.jsp by only allowing the user to type index.html. If the user types "index.jsp" giving a 404 error instead.

Any help most appreciated.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a servlet entry with mapping for each jsp page in your web.xml.




To keep them from typing '*.jsp' in directly, you could put your actual JSP pages under WEB-INF or use a filter that looks for '.jsp' at the end of the URI. You could also put them in a directory and create a mapping for that directory that sends them to your 404 page.
 
Arron Ferguson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Souther,

Thanks for your reply. I guess I'm trying to find a "silver bullet" since my web app creates JSP files dynamically. I suppose what I can do is get one of the servlets to add any new JSPs that have been created to the web.xml file (along with its url-mapping).

Thanks again.

 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also create a Servlet or JSP onto which EVERYTHING maps. You would then inside this Servlet / JSP forward the request onto whichever page they specified in the URL!
 
Arron Ferguson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Baker wrote:
You could also create a Servlet or JSP onto which EVERYTHING maps. You would then inside this Servlet / JSP forward the request onto whichever page they specified in the URL!


Heheh, you know, right after reading this, I started looking at examples and the light went on after reading:

http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-filters-p2.html

And I came to the same conclusion.



Great minds think alike?

 
Arron Ferguson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on Tim Baker's good idea, I came up with a Servlet that asks a Java class for a list (Vector) of names of HTML documents that are considered 'valid' (i.e. map 1:1 to JSP files). It then uses those names to map to the JSP (without the user knowing) and redirects to that JSP. If a user types in the name of an html document that does not exist (i.e. 404), I redirect to the 404 servlet. Since people here shared their time in answering, it's only fair I share the code (not that it's spectacular or anything):



The following entry must also be in the Web app's web.xml:



Enjoy.



[ February 04, 2005: Message edited by: Arron Ferguson ]
[ February 04, 2005: Message edited by: Arron Ferguson ]
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool
 
Arron Ferguson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually,

the following line:



Should be replaced with:



Otherwise the user can type:

adsflasdfasdfindex.html

instead of index.html and it would still work.

I always seem to find these *after* I've submitted. :roll:

But it works now - I tried it.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I always seem to find these *after* I've submitted.


You can edit your posts after you submit them.
Just click on the notepad/pencil icon.
[ February 04, 2005: Message edited by: Ben Souther ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic