Tuesday 18 July 2017

Load a resource from WEB-INF directory

Typical web application structure looks like below.

What is WEB-INF directory?
We can place the resources like jsp file, property files, images etc., in WEB-INF directory. The resources placed in WEB-INF directory are not public, they can be accessed via servlets.

How can I access the contents of WEB-INF directory?
By using getResource and getResourceAsStream method calls on the ServletContext, you can access the contents of WEB-INF directory.

Ex:
                 String filePath = "/WEB-INF/app.properties";

                 String appName, version, author;

                 try (InputStream is = request.getServletContext().getResourceAsStream(filePath)) {
                          Properties properties = new Properties();
                          properties.load(is);

                          appName = properties.getProperty("appName");
                          version = properties.getProperty("version");
                          author = properties.getProperty("author");
                 }

Above statements load the property file located in WEB-INF directory, and read the contents.

Let’s see how to load a property file from WEB-INF directory.

Step 1: Create new dynamic web project 'WebAppDemo' in Eclipse. You can refer below link to create dynamic web project.


Project structure looks like below.

Step 2: Create a file ‘app.properties’ under ‘WEB-INF’ directory.
app.properties
appName=Demo Application to read resources in 'WEB-INF' directory
version=1.0
author=Krishna

Step 3: Define a servlet ‘HelloWorld’ like below.

HelloWorld.java

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
 private static final long serialVersionUID = 1L;

 public HelloWorld() {
 }

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String filePath = "/WEB-INF/app.properties";

  String appName, version, author;

  try (InputStream is = request.getServletContext().getResourceAsStream(filePath)) {
   Properties properties = new Properties();
   properties.load(is);

   appName = properties.getProperty("appName");
   version = properties.getProperty("version");
   author = properties.getProperty("author");
  }

  response.getWriter().append("Application name : ").append(appName).append("<br />version : ").append(version)
    .append("<br />author : ").append(author);
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }

}

Run the application on server and hit below url.



You can able to see below output.

My final project structure looks like below.




Previous                                                 Next                                                 Home

No comments:

Post a Comment