jump to navigation

Accessing JSPs kept outside the servlet context May 23, 2009

Posted by Anoop Somasundaran in JSP, Tomcat.
Tags: ,
8 comments

In one of my projects we had a requirement to access the JSPs/HTMLs which were kept outside the servlet context. The following is the trick that worked for me.

My application was located at /opt/TOMCAT_HOME/webapps/app1 and I had to access (include) the HTML files which were located at a different folder (/folder/files/).

Configuring tomcat to access the Files:

Add the following tag under the <Host name="localhost".. tag in server.xml

<Context path="/app1/files" docBase="/folder/files" debug="0" reloadable="true" crossContext="true"/>

The files for the request http://localhost:8080/app1/files/file.jsp will be fetched from the folder /folder/files.

This will work just fine if you call the JSPs directly. How ever if you want to include the files in a different context in your JSP neither action nor directive will work and will throw error saying that page cannot found.

In my project I had to include the files (HTMLs) in my JSP and the above tags didn’t work.

In this case the JSTL tag <c:import> will work perfectly. Remember that you will have to provide the complete url (http://localhost:8080/app1/files) for the parameter ‘url’ and relative path /app1/files will not work.

<c:import url="http://localhost:8080/app1/files" />

This will help to access the JSPs which are placed in a different context as well. However make sure that you are using crossContext=”true” in the context tag.