This example show you how to read a text file in a servlet. Using the ServletContext.getResourceAsStream()
method will enable you to read a file whether the web application is deployed in an exploded format or in a war file archive.
The following servlet read the configuration.properties
file from the /WEB-INF
directory in our web application. The configuration.properties
file is just a regular text file with the following contents.
app.appname=Servlet Examples
app.version=1.0
app.copyright=2007
Here is our ReadTextFileServlet
servlet class.
package org.kodejava.example.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class ReadTextFileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
// We are going to read a file called configuration.properties. This
// file is placed under the WEB-INF directory.
String filename = "/WEB-INF/configuration.properties";
ServletContext context = getServletContext();
// First get the file InputStream using ServletContext.getResourceAsStream()
// method.
InputStream is = context.getResourceAsStream(filename);
if (is != null) {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
PrintWriter writer = response.getWriter();
String text;
// We read the file line by line and later will be displayed on the
// browser page.
while ((text = reader.readLine()) != null) {
writer.println(text + "</br>");
}
}
}
}
After creating the servlet class we need to register our servlet and create a servlet-mapping in the web.xml
file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Servlet Examples</display-name>
<servlet>
<servlet-name>ReadTextFileServlet</servlet-name>
<servlet-class>org.kodejava.example.servlet.ReadTextFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ReadTextFileServlet</servlet-name>
<url-pattern>/readTextFileServlet</url-pattern>
</servlet-mapping>
</web-app>
To access the servlet you can type http://localhost:8080/readTextFileServlet
in your browser URL address bar.
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020
- How do I get a list of all TimeZones Ids using Java 8? - April 25, 2020
Thank you very much, I am new to the concept of servlets so I wanted to ask if this code could be used in case I want to read a JSON file and not a text file. And what should be changed in the code in order to do that.
Thank you again
Hi Azum, of course you can read a JSON file in servlet. JSON file is actually just a text file. You just need a library such as Gson, Jackson, JSON-Java, etc to process the JSON file. For example to convert it into Java object.
I have copied the code and it doesn’t run, the servlet doesn’t work.
Error 404
Might be useful to drop the URL you input.
Beware, the servet is named Read… but the URL to input is read… (lower case R)