<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">

Access Static Resources through Servlet

Nikhil Vakil Aug 14, 2012

Java servlet servlet-filters Technology


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,


	&lt;servlet-name&gt;Servlet Name&lt;/servlet-name&gt;

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.


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.


The above scenario has the following solutions.

Solution 1 - Application with Spring

Please go through the link below


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)

Web Application Structure (Tomcat Server)

Servlet Class: com.sample.servlet.TryServlet

Web.xml: Map this servlet

	&lt;servlet-name&gt;Static Resource Servlet&lt;/servlet-name&gt;

	&lt;servlet-name&gt;Static Resource Servlet&lt;/servlet-name&gt;

URL of JavaScript File.


Servlet Code


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


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.


e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.