e-Zest members share technology ideas to foster digital transformation.

Access Static Resources through Servlet

Written by Nikhil Vakil | Aug 14, 2012 3:58:13 PM
Preface

Many times we face problems while accessing static resources through Servlet. Following blog post will explain one possible solution for this problem. It is applied in the web application that is only exposing Web Services to outside world.

Assume that the Web Application is already developed. However, currently the application does not have any static resources.

In the web.xml the Servlet serving Web Services is mapped with /* URL pattern.

For example,

<servlet>
	<servlet-name>WebServicesServlet</servlet-name>
	<servlet-class>FullyQualifiedClassName</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>Servlet Name</servlet-name>
	<url-pattern>/*</url-pattern>
</servlet-mapping>
Requirement

This Web Application needs to be enhanced to

  1. Provide some functionality
  2. Develop a Web Interface for the above mentioned functionality

The Web Application contains static contents (e.g. JavaScript) files.

Problem

In Web Application, the JavaScript files are invoked through <script/> tags. These tags contain src attribute that has URL of JavaScript file.

As the Web Service servlet (WebServicesServlet) is mapped to /*, all the JavaScript URL will be answered by this servlet. The same servlet won’t be able to find JavaScript.

This will result in an error.

Solution

The above scenario has the following solutions.

Solution 1 - Application with Spring

Please go through the link below

http://static.springsource.org/spring-webflow/docs/2.3.x/reference/html/ch12s02.html

Solution 2 - Application with/ without Spring

Write a Servlet extending HttpServlet that will

  1. Find the path of Static Resource
  2. Read the Static Resource with FileInputStream
  3. Write the data to Response OutputStream
Implementation Steps

Web Application Structure (Tomcat Server)

Servlet Class: com.sample.servlet.TryServlet

Web.xml: Map this servlet

&lt;servlet&gt;
	&lt;servlet-name&gt;Static Resource Servlet&lt;/servlet-name&gt;
	&lt;servlet-class&gt;com.sample.servlet.TryServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;

&lt;servlet-mapping&gt;
	&lt;servlet-name&gt;Static Resource Servlet&lt;/servlet-name&gt;
	&lt;url-pattern&gt;/staticResources/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

URL of JavaScript File.

http://localhost:8080/.../staticResources/resources/js/sample.js

Servlet Code

init(ServletConfig)

  1. Get the Real path of Application on Server
  2. servletConfig.getServletContext.getRealPath(“.”)

    C:..Tomcatwebappssample.

doGet(HttpServletRequest, HttpServletResponse)

  1. Extract the path of JS under sample directory
  2. /resources/js/sample.js

  3. Concat path from #1 and #2. This is actual path of JS file
  4. C:..Tomcatwebappssample./resources/js/sample.js

  5. Read the above file with FileInputStream
  6. Write the bytes from FileInputStream to response OutputStream
Solution 3 - Filters

I found this solution on the below URL. However, I haven’t tried this solution.

http://stackoverflow.com/questions/2725102/how-to-use-a-filter-in-java-to-change-an-incoming-servlet-request-url