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

Access Static Resources through Servlet

Nikhil Vakil Aug 14, 2012

Java servlet servlet-filters Technology

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,

&lt;servlet&gt;
	&lt;servlet-name&gt;WebServicesServlet&lt;/servlet-name&gt;
	&lt;servlet-class&gt;FullyQualifiedClassName&lt;/servlet-class&gt;
&lt;/servlet&gt;

&lt;servlet-mapping&gt;
	&lt;servlet-name&gt;Servlet Name&lt;/servlet-name&gt;
	&lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
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)

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

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.