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 property.java . I have saved this file inside WEB-INF/classes folder. My application name is ReadProperty
import java.util.Properties;
import java.io.*;
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
@WebServlet("/readprop")
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/server.properties");
out.println("<title>Read Property File</title>");
// load a properties file
prop.load(input);
// get the property value and print it out
out.println(prop.getProperty("APPNAME"));
out.println("<p>");
out.println(prop.getProperty("DATA"));
} catch (IOException ex) {
//ex.printStackTrace();
out.println(ex);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
The property file we are using is server.properties and I am calling it using
input = getServletContext().getResourceAsStream("/WEB-INF/properties/server.properties");
As you can see I have a properties folder inside my WEB-INF
Content of the server.properties file is
APPNAME=Test Application
DATA=Database
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><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
<html>
<head>
<title>Create table in mysql database using jsp</title>
</head>
<body>
<TABLE style="background-color: #ffffcc;">
<TR>
<TD align="center"><h2>Reading Property File Values</h2></font>
</TR>
<TR>
<TD align="center"><h3><jsp:include page="/readprop"></jsp:include></h3></TD>
</TR>
</TABLE>
</body>
</html>
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
