---
title: Access Static Resources through Servlet
description: Access Static Resources through Servlet
image: https://blog.e-zest.com/hs-fs/file-3395199039-png/blog-files/web-application-structure.png
---

[![e-Zest logo\_An accion lab company\_Colour](https://blog.e-zest.com/hubfs/images/Logos%20with%20Tagline/Corporate/SVG/e-Zest%20logo_An%20accion%20lab%20company_Colour.svg "e-Zest logo_An accion lab company_Colour")](https://www.e-zest.com/)

- [WORK](https://www.e-zest.com/our-work)
- [SERVICES](https://www.e-zest.com/our-services) 
    - [Product Engineering](https://www.e-zest.com/digital-product-engineering-services)
    - [Enterprise Data Services](https://www.e-zest.com/enterprise-data-services)
    - [Digital Commerce](https://www.e-zest.com/digital-commerce-services)
    - [Digital Operations](https://www.e-zest.com/digital-operations)
    - [Digital Consulting](https://www.e-zest.com/digital-transformation-consulting-services)
    - [Digital Experience Design](https://www.e-zest.com/digital-experience-design)
- [INSIGHTS](https://blog.e-zest.com/)
- [CAREERS](https://www.e-zest.com/careers)
- [ENTERPRISE DATA SERVICES](https://www.e-zest.com/enterprise-data-services)
- [GENERATIVE AI](https://www.e-zest.com/generative-ai)
- [Healthcare](https://healthcare.e-zest.com)
- [Contact Us](https://www.e-zest.com/contact-us)

- ![Elevating Product Engineering with AI and ML](https://blog.e-zest.com/hubfs/Elevating%20Product%20Engineering%20with%20AI%20and%20ML.jpg)

  [BLOGS](https://blog.e-zest.com)
  
  #### Elevating Product Engineering with AI and ML

  As technology evolves and customer choices broaden, there is an increasing ...

  [Read more](https://blog.e-zest.com/elevating-product-engineering-with-ai-and-ml)
- ![e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024](https://blog.e-zest.com/hubfs/e-Zest%20%E2%80%93%20An%20Accion%20Labs%20Company%20-%20Gets%20Recognition%20as%20a%20Contender%20in%20the%20ISG%20Provider%20LensTM%20Study%20on%20Digital%20Engineering%20Services%202024.jpg)

  [NEWS](https://blog.e-zest.com/news)
  
  #### e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024

  We are delighted to announce that e-Zest – An Accion Labs Company has been ...

  [Read more](https://blog.e-zest.com/news/e-zest-an-accion-labs-company-gets-recognition-as-a-contender-in-the-isg-provider-lens-study-on-digital-engineering-services-2024)
- ![e-Zest Solutions supports Digital Pune Mission](https://blog.e-zest.com/hubfs/blog-files/Digital%20Literacy%20Bus%20e-Zest.png)

  [CSR](https://blog.e-zest.com/csr)
  
  #### e-Zest Solutions supports Digital Pune Mission

  **Pune, India:** e-Zest Solutions, a leading digital technology innovation ...

  [Read more](https://blog.e-zest.com/csr/e-zest-solutions-supports-digital-pune-mission)

- [Work](https://www.e-zest.com/our-work)
- [Services](https://www.e-zest.com/our-services)
- [Insights](https://blog.e-zest.com)
- [Careers](https://www.e-zest.com/careers)
- [Enterprise Data](https://www.e-zest.com/enterprise-data-services)
- [Generative AI](https://www.e-zest.com/generative-ai)
- [Innovations](https://www.e-zest.com/innovations)
- [Healthcare](https://healthcare.e-zest.com)
- [About](https://www.e-zest.com/about-us)
- [Contact](https://www.e-zest.com/contact-us)

- [Blog](https://blog.e-zest.com/)
- [News](https://blog.e-zest.com/news)
- [CSR](https://blog.e-zest.com/csr)

- [![Linkedin Icon](https://blog.e-zest.com/hubfs/icons/linkedin_icon_white.svg)](https://www.linkedin.com/company/e-zest-solutions/)
- [![Twitter Icon](https://blog.e-zest.com/hubfs/twitter-logo-silhouette%20(5).svg)](https://twitter.com/ezest)
- [![Facebook Icon](https://blog.e-zest.com/hubfs/facebook-app-logo%20(4).svg)](https://www.facebook.com/61259256638)
- [![Instagram Icon](https://blog.e-zest.com/hubfs/instagram-logo%20(4).svg)](https://www.instagram.com/ezestsolutions/)
- [![Youtube Icon](https://blog.e-zest.com/hubfs/icons/youtube_icon_white.svg)](https://www.youtube.com/user/ezestcorporate)

[![Go to top icon](https://blog.e-zest.com/hubfs/AA%20Assets/top%20button.svg)](https://blog.e-zest.com/access-static-resources-through-servlet/#top)

Hit enter to search or ESC to close

Insights

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

- [Blog](https://blog.e-zest.com)
- [News](https://blog.e-zest.com/news)
- [Podcasts](https://www.e-zest.com/podcasts)
- [Healthcare](https://blog.e-zest.com/healthcare)
- [CSR](https://blog.e-zest.com/csr)
- [Whitepaper](https://blog.e-zest.com/whitepaper)
- [Playbook](https://blog.e-zest.com/playbook)
- [Media Kit](https://www.e-zest.com/media-kit)

# Access Static Resources through Servlet

[Nikhil Vakil](https://blog.e-zest.com/author/nikhil-vakil)  Aug 14, 2012

[Java](https://blog.e-zest.com/topic/java) [servlet](https://blog.e-zest.com/topic/servlet) [servlet-filters](https://blog.e-zest.com/topic/servlet-filters) [Technology](https://blog.e-zest.com/topic/technology)

- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/access-static-resources-through-servlet/&title=&summary=&source=>
- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/access-static-resources-through-servlet/&text=Access%20Static%20Resources%20through%20Servlet>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/access-static-resources-through-servlet/>
- <https://www.instagram.com/ezestsolutions/?u=https://blog.e-zest.com/access-static-resources-through-servlet/>

##### **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](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)](https://blog.e-zest.com/hs-fs/file-3395199039-png/blog-files/web-application-structure.png?width=158&height=229&name=web-application-structure.png)

**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

 servletConfig.getServletContext.getRealPath(“.”)

 C:..Tomcatwebappssample.

**doGet(HttpServletRequest, HttpServletResponse)**

1. Extract the path of JS under sample directory

 /resources/js/sample.js

2. Concat path from #1 and #2. This is actual path of JS file

 C:..Tomcatwebappssample./resources/js/sample.js

3. Read the above file with FileInputStream
4. 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](http://stackoverflow.com/questions/2725102/how-to-use-a-filter-in-java-to-change-an-incoming-servlet-request-url)

- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/access-static-resources-through-servlet/&text=Access%20Static%20Resources%20through%20Servlet>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/access-static-resources-through-servlet/>
- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/access-static-resources-through-servlet/&title=&summary=&source=>

### Similar Blog

[![How to Create Own Custom JMeter Template?](https://blog.e-zest.com/hubfs/How%20to%20Create%20Own%20Custom%20JMeter%20Template%201280x667%202560x1334.jpg) How to Create Own Custom JMeter Template? We frequently need to construct Test Plans with a similar format when working on web application performance testing using Apache ... By Kishore Bhamare Feb 28, 2023](https://blog.e-zest.com/how-to-create-own-custom-jmeter-template)

[![Hoisting in JavaScript](https://blog.e-zest.com/hubfs/Blog_Files/Hoisting%20in%20JavaScript-190225.png) Hoisting in JavaScript Generally, we all have heard of the term ‘hoisting’ with reference to flag hoisting. By Ajit Sakhare Feb 26, 2019](https://blog.e-zest.com/hoisting-in-javascript)

[![OWASP ZAP Modes](https://blog.e-zest.com/hubfs/OWASP%20ZAP%202.5.0.png) OWASP ZAP Modes OWASP ZAP is an open-source web security testing tool, used for detecting vulnerabilities in web applications.ZAP provides you ... By Kishore Bhamare Dec 22, 2016](https://blog.e-zest.com/owasp-zap-modes)

[![Automation using Selenium WebDriver Frameworks](https://blog.e-zest.com/hubfs/Selenium_blog_image.png) Automation using Selenium WebDriver Frameworks Frameworks help to structure our code and make the maintenance process easy. Without frameworks we will place all our code and ... By Uddhav Naik Sep 26, 2016](https://blog.e-zest.com/automation-using-selenium-webdriver-frameworks)

[Tutorial: Elasticsearch Integration in a Java Application The whole world relies on search to get access to relevant information and data. Handling this large amount of data presents a ... By Jyoti Agrawal Jun 11, 2015](https://blog.e-zest.com/tutorial-elasticsearch-integration-in-a-java-application/)

[![Downloading Large Files through Web Service](https://blog.e-zest.com/hs-fs/file-3395199459-png/blog-files/downloading-large-files-through-web-service-1.png) Downloading Large Files through Web Service **This solution is developed in Java. The solution can be implemented where any File Download tool is not to be used.** By Nikhil Vakil Sep 06, 2012](https://blog.e-zest.com/downloading-large-files-through-web-service/)

## Helping Organizations Thrive in the Digital Age

[Contact Us](https://www.e-zest.com/contact-us)

- [About](https://www.e-zest.com/about-us)
- [Services](https://www.e-zest.com/our-services)
- [Work](https://www.e-zest.com/our-work)
- [Insights](https://blog.e-zest.com/)
- [Healthcare](https://healthcare.e-zest.com/)
- [Accion Labs](https://www.accionlabs.com/)

- <https://www.linkedin.com/company/e-zest-solutions/>
- <https://twitter.com/ezest>
- <https://www.facebook.com/eZest/>
- <https://www.instagram.com/ezestsolutions/>
- <https://www.youtube.com/user/ezestcorporate>

- Dallas
- Detroit
- Hannover
- Vienna
- London
- Pune

 © 2025 e-Zest Solutions

- [Privacy Policy](https://www.e-zest.com/privacypolicy)
- [Cookie Policy](https://www.e-zest.com/cookie-policy)

 Contact us

 Contact us

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.