How do I log from a servlet using ServletContext.log()?

In Java servlets, you can use the ServletContext.log() method to log messages, exceptions, or context-specific information. The ServletContext object is available in your servlet and allows you to log messages to the server’s log file.

Here are the common ways you can log using ServletContext.log():

1. Logging a simple message:

You can log plain text messages with ServletContext.log(String message).

package org.kodejava.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.ServletContext;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/logMessage")
public class MyLogServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServletContext context = getServletContext();
        context.log("This is a simple log message.");
        response.getWriter().println("Message logged.");
    }
}

2. Logging a message with an exception:

If you want to log an exception with additional context, you can use ServletContext.log(String message, Throwable throwable).

package org.kodejava.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.ServletContext;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/logException")
public class MyLogServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServletContext context = getServletContext();
        try {
            // Simulate an exception
            int result = 10 / 0;
        } catch (Exception e) {
            context.log("An exception occurred: ", e);
        }
        response.getWriter().println("Exception logged.");
    }
}

3. Including dynamic information:

You can include dynamic content in the log messages to make your logs more informative.

package org.kodejava.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.ServletContext;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/logAccess")
public class MyLogServlet3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServletContext context = getServletContext();
        String userIP = request.getRemoteAddr();
        context.log("Access from IP: " + userIP);
        response.getWriter().println("Access logged.");
    }
}

Points to Remember

  1. Where the logs are written: The ServletContext.log() messages are typically written to the application server’s log file (e.g., catalina.out for Tomcat). This location depends on the server configuration.
  2. Severity levels: The ServletContext.log() does not natively support different log levels like INFO, WARN, or ERROR. If you need more advanced logging capabilities, consider using a logging framework such as SLF4J, Log4j, or java.util.logging.
  3. Thread Safety: The logging methods of ServletContext are thread-safe.

This is how you can log using ServletContext.log() in your servlet-based application.

Maven dependencies

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>6.1.0</version>
    <scope>provided</scope>
</dependency>

Maven Central

How do I share data between servlets using ServletContext?

In Java Servlets, you can share data between servlets using the ServletContext object. The ServletContext is an application-wide object that all servlets in a web application can access. It allows servlets to share information. Here’s how you can use it:


Steps to Share Data Using ServletContext:

  1. Set Attribute in ServletContext:
    • A servlet can store an object in the ServletContext as an attribute using the setAttribute method.
    ServletContext context = getServletContext();
    context.setAttribute("sharedData", "This is shared data");
    
  2. Retrieve the Attribute in Another Servlet:
    • Another servlet can retrieve the shared data using the getAttribute method.
    ServletContext context = getServletContext();
    String sharedData = (String) context.getAttribute("sharedData");
    
  3. (Optional) Remove the Attribute:
    • If needed, you can remove the attribute using the removeAttribute method.
    context.removeAttribute("sharedData");
    

Example Use Case:

Create two servlets: one for setting the data and one for retrieving it.

SetDataServlet.java

package org.kodejava.servlet;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/setData")
public class SetDataServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Set shared data in ServletContext
        ServletContext context = getServletContext();
        context.setAttribute("sharedData", "Hello from SetDataServlet!");

        response.getWriter().println("Data set successfully.");
    }
}

GetDataServlet.java

package org.kodejava.servlet;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/getData")
public class GetDataServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Retrieve shared data from ServletContext
        ServletContext context = getServletContext();
        String sharedData = (String) context.getAttribute("sharedData");

        response.getWriter().println("Shared Data: " + sharedData);
    }
}

Key Points:

  1. Application Scope:
    • Attributes in the ServletContext are available globally across the web application. They can be accessed by all servlets and JSPs.
  2. Thread-Safety:
    • Be cautious about thread safety because servlets handle multiple requests concurrently. If multiple threads modify the shared data simultaneously, data consistency issues may occur.
    • You may need to synchronize access to the shared object.
  3. Lifecycle:
    • Attributes in the ServletContext remain in memory until they are explicitly removed using removeAttribute, or the application is redeployed/stopped.

Advanced Sharing via ServletContextListener:

If you need to initialize or clean up shared data when the application starts or stops, you can use a ServletContextListener.

package org.kodejava.servlet;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;

@WebListener
public class AppContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        context.setAttribute("sharedData", "Initial shared data");
        System.out.println("Application started. Shared data set.");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Application stopped. Cleaning up...");
    }
}

This ensures shared data is set and removed in a centralized manner.


This approach to sharing data is straightforward and works seamlessly for many use cases in a web application.

Maven dependencies

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>6.1.0</version>
    <scope>provided</scope>
</dependency>

Maven Central

How do I obtain ServletContext of another application?

The ServletContext.getContext(String uripath) enable us to access servlet context of another web application deployed on the same application server. A configuration need to be added to enable this feature.

In the example below we will forward the request from the current application to the /otherapp/hello.jsp page. We place a string in the request object attribute of the current application and going to show it in the hello.jsp page.

package org.kodejava.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = {"/context"})
public class GetAnotherContextServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Get ServletContext of another application on the same Servlet
        // container. This allows us to forward request to another application
        // on the same application server.
        ServletContext ctx = request.getServletContext().getContext("/otherapp");

        // Set a request attribute and forward to hello.jsp page on another 
        // context.
        request.setAttribute("MESSAGE", "Hello There!");
        RequestDispatcher dispatcher = ctx.getRequestDispatcher("/hello.jsp");
        dispatcher.forward(request, response);
    }
}

To enable this feature in Tomcat we need to enable the crossContext attribute by setting the value to true, the default value is false. Update the server.xml file to add the following configuration inside the <Host> node.

<Context path="/webapp" debug="0" reloadable="true" crossContext="true"/>

Maven dependencies

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

Maven Central

How do I read text file in Servlet?

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=2021

Here is our ReadTextFileServlet servlet class.

package org.kodejava.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@WebServlet(name = "ReadTextFileServlet", urlPatterns = "/read-text-file")
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>");
            }
        }
    }
}

To access the servlet you can type http://localhost:8080/read-text-file in your browser URL address bar.

Maven dependencies

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

Maven Central

How do I share object or data between users in web application?

In a web application there are different type of scope where we can store object or data. There are a page, request, session and application scope.

To share data between users of the web application we can put a shared object in application scope which can be done by calling setAttribute() method of the ServletContext. By this way data can then be accessing by other users by calling the getAttribute() method of the ServletContext.

Let’s see the example code in a simple servlet.

package org.kodejava.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "SharedObjectServlet", urlPatterns = "/shared-object")
public class ApplicationContextScopeAttribute extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ServletContext context = this.getServletContext();
        context.setAttribute("HELLO.WORLD", "Hello World 123");
    }
}

And here is what we code in the JSP page to access it.

<%= request.getServletContext().getAttribute("HELLO.WORLD") %>

Maven dependencies

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

Maven Central