[ACCEPTED]-How to hide the .jsp extension in my web server urls-jsp

Accepted answer
Score: 11

You can create a servlet mapping like this:

<servlet-mapping>
   <servlet-name>MappingServlet</servlet-name>
   <url-pattern>path/*</url-pattern>
 </servlet-mapping>

The 5 url-pattern must be edited to suit your 4 needs. You need of course to create the 3 servlet in order to map the url to the actual 2 jsp. This technique is used by most of the 1 MVC frameworks.

Score: 6

Use this servlet mapping in your web.xml file.

<servlet>   
  <servlet-name>search</servlet-name>
  <jsp-file>/search.jsp</jsp-file>
</servlet>
<servlet-mapping>
  <servlet-name>search</servlet-name>
  <url-pattern>/search</url-pattern>
</servlet-mapping>

0

Score: 5

UrlRewrite is a good flexible Java-based framework-independent 6 solution.

This is better than a Servlet mapping 5 in web.xml, because that is too limited in what 4 you can do, and better than an Apache based 3 solution because it is part of your web 2 application so you do not need to put Apache 1 in front of your application server.

Score: 2

Map the .jsp as a servlet, but use a <jsp-file> tag 1 instead of a <url-mapping> tag.

<servlet>
  <servlet-name>myjsp</servlet-name>
  <jsp-file>/myjsp.jsp</jsp-file>
</servlet>
Score: 1

If you opt for the Apache rewrite rule, rather 14 than the application server mapping/filter 13 (as I did) you might also want to do more 12 than just look for "^([0-9a-zA-Z]+)$"

You 11 may want to confirm the url is not a directory 10 or a file that does exist if apache is fronting 9 and serving the non-jsp resources. And 8 confirm that the JSP exists, and do a pass 7 thru rather than redirect, and append any 6 possible query string.

RewriteCond %{REQUEST_URI} !^/.*\.(jsp)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.jsp -f
RewriteRule ^(.+)$ /$1.jsp [PT,QSA,L]

And to make sure that 5 users only see this via /search, not /search.jsp 4 then you want to rewrite the reverse as 3 well

RewriteRule ^(.+)\.jsp$ $1 [R=301,QSA,L]
RewriteRule ^(.+)index$ $1 [R=301,QSA,L]

This is a good idea for SEO purposes 2 so that search engines dont ding you for 1 duplicating content at multiple urls.

Score: 1

I have a jsp located at data/feed.jsp Adding 2 this to my web.xml meant I could access 1 it at data/feed ...

<servlet>   
  <servlet-name>feed</servlet-name>
  <jsp-file>/data/feed.jsp</jsp-file>
</servlet>
<servlet-mapping>
  <servlet-name>feed</servlet-name>
  <url-pattern>/data/feed</url-pattern>
</servlet-mapping>
Score: 0
RewriteEngine On

RewriteRule ^([0-9a-zA-Z]+)$  $1.jsp

In your .htaccess should rewrite all URLs 2 to .jsp. So search.jsp will just be search, as 1 you described.

Score: 0

I am not a JSP expert but I think you can 2 define URL-mappings in web.xml to provide 1 aliases for your servlets and jsps.

More Related questions