Share on Google+Share on Google+

Use Log in Servlet Context

This section illustrates you how to use log in Servlet Context.

Use Log in Servlet Context


This section illustrates you how to use log in Servlet Context.

We are providing you an example. In the given example, an object of Servlet Context is defined. The method context.log() writes the specified message to a servlet log file. The name and type of the servlet log file is specific to the servlet container.




 Here is the code of

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LogInServlet extends HttpServlet {
private ServletContext context;

public void init(ServletConfig config) throws ServletException {
  context = getServletContext();
public void doGet(HttpServletRequest req, 
  HttpServletResponse res)
throws IOException {
  ServletOutputStream out = res.getOutputStream();
  context.log("Your message has been written. ");
  out.println("<html><head><title>Log in ServletContext</title>
  out.println("<body>Go to the tomcat-home\\logs and open 
  the xx file to see your log entries"

In web.xml, do the following servlet-mapping



After deploying the tomcat, Following output will be displayed on the browser

The message provided through the servlet has been written in the log file

Download Source Code


Posted on: August 20, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Use Log in Servlet Context  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image