Log in ServletContext : Log « Servlets « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Servlets » LogScreenshots 
Log in ServletContext
 
// web.xml
/*
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <servlet>
    <servlet-name>LoggingServlet</servlet-name>
    <servlet-class>LoggingServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoggingServlet</servlet-name>
    <url-pattern>/loggingServlet</url-pattern>
  </servlet-mapping>
</web-app>
*/

import java.io.IOException;

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 LoggingServlet extends HttpServlet {
  private ServletContext context;

  public void init(ServletConfig configthrows ServletException {
    super.init(config);
    context = getServletContext();
    context.log("Init has been invoked");
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws IOException {
    ServletOutputStream out = res.getOutputStream();
    context.log("doGet has now been invoked");
    res.setContentType("text/html");
    out.println("<html><head><title>Logging Servlet</title></head>");
    out.println("<body>Visit the <tomcat-home>\\logs and open the xx file to see the log entries");
    out.println("</body></html>");
  }
}
           
         
  
Related examples in the same category
1.Servlets Logging Filter Demo
2.Logging Filter
3.Context log
4.Logger without configure file
5.Logger Servlet
6.Another logger servlet
7.Logger new config
8.Servlet: Root logger
9.Session logger
10.LoggerSkel for Servlet
11.Context logger
12.Log Filter
13.Servlet Logging
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.