Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide JSP extension from web pages

Im developing a web application(J2EE,Struts2,JSP,Tomcat) I want to hide the .jsp extension from webpages.

here is some piece of my web.xml:

     <filter>
         <filter-name>STSDispatcher</filter-name>
         <filter-class>
           org.apache.struts2.dispatcher.FilterDispatcher
         </filter-class>
     </filter>

     <filter-mapping>
         <filter-name>STSDispatcher</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>

     <welcome-file-list>
         <welcome-file>Login.jsp</welcome-file>
     </welcome-file-list>

I Googled around and found this solution:

    <servlet>  
         <servlet-name>myFoo</servlet-name>  
         <jsp-file>myJSPfile.jsp</jsp-file>  
    </servlet>  
    <servlet-mapping>  
         <servlet-name>myFoo</servlet-name>  
         <url-pattern>/main</url-pattern>  
    </servlet-mapping> 

I tried this like below, but didn't work:

for example I have "alert.jsp" and this is my web.xml:

<servlet>
    <servlet-name>alert</servlet-name>
    <jsp-file>/alert.jsp</jsp-file>
 </servlet> 

 <servlet-mapping>
     <servlet-name>alert</servlet-name>
     <url-pattern>/alert</url-pattern>
 </servlet-mapping>

but I got this error: "There is no Action mapped for namespace / and action name alert. - [unknown location]"

What should I do?

Thanks in advance.

like image 498
Amin Sh Avatar asked Jun 20 '11 06:06

Amin Sh


2 Answers

You can put the directory structure with the JSPs in WEB-INF, then use a servlet to forward to the JSPs according to the requested URL.

The servlet will be mapped to a wildcard pattern, like:

<servlet-mapping>
  <servlet-name>ForwardToJspServlet</servlet-name>
  <url-pattern>*.page</url-pattern>
</servlet-mapping>

or

<servlet-mapping>
  <servlet-name>ForwardToJspServlet</servlet-name>
  <url-pattern>/pages/*</url-pattern>
</servlet-mapping>

and the logic of the servlet will be converting the requested URL to the path of the JSP and then forwarding.

For example, the user requests the URL /yourApp/documents/documentAdd.page and you forward to the JSP under /WEB-INF/JSP/documents/documentAdd.jsp.

like image 102
Nicolae Albu Avatar answered Sep 22 '22 07:09

Nicolae Albu


I think you get the error because your struts2 filter intercept all the request coming to the server and tries to find a mapping in your actions but since /alert actually points to a servlet you see the error.
A solution would be to have the mapping in your struts config file like:

<action name="alert">
    <result>/alert.jsp</result>
</action>

This way when you request /alert you get the page with no extension (name of your action).

like image 22
doctrey Avatar answered Sep 20 '22 07:09

doctrey