Last updated on May 6th, 2022 at 12:22 pm

In this tutorial we are going to create a simple Java servlet with the option to read a property file. Once that is done our next step is to create a JSP and use jsp:include directive to display the servlet within JSP.

Let us start by creating a Java file with some name, here I am using . I have saved this file inside WEB-INF/classes folder. My application name is ReadProperty

import java.util.Properties;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.annotation.*;  // Tomcat 10

public class property extends HttpServlet {
public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        Properties prop = new Properties();
        InputStream input = null;
        try {
                input = getServletContext().getResourceAsStream("/WEB-INF/properties/");
                out.println("<title>Read Property File</title>");
                // load a properties file
                // get the property value and print it out
        } catch (IOException ex) {
        } finally {
                if (input != null) {
                        try {
                        } catch (IOException e) {

The property file we are using is and I am calling it using

input = getServletContext().getResourceAsStream("/WEB-INF/properties/");

As you can see I have a properties folder inside my WEB-INF

Content of the file is

APPNAME=Test Application

Compile the Java code and make sure there is no error.

Using Annotation WebServlet

As you might have noticed in the above code we are calling HttpRequest to using URL “/readprop” via annotation @WebServlet("/readprop"), which is applicable to Tomcat 7 onwards. In other words, the full URL shall be http://ip_addr:port/<your_app>/readprop to trigger this HttpRequest

Using web.xml

Now add the below xml code to your existing web.xml file

    </servlet><servlet -name="">property</servlet>
    <servlet -class="">property</servlet>

<servlet -mapping="">
    </servlet><servlet -name="">property</servlet>
    <url -pattern="">/property</url>

Create JSP file to load servlet

On your application root directory create a file name load_props.jsp

Add the below code to it

        <title>Create table in mysql database using jsp</title>
        <TABLE style="background-color: #ffffcc;">
                <TD align="center"><h2>Reading Property File Values</h2></font>
                <TD align="center"><h3><jsp:include page="/readprop"></jsp:include></h3></TD>

In the above code I am including /readprop which is the webservlet annotation. If you don’t want to use that make sure to update the include page directive to /property or any class name you have given in the web.xml file.

Load your web application http://app:8080/ReadProperty/load_props.jsp

The result will be similar to this

Leave a Reply

Your email address will not be published. Required fields are marked *