Logging with Java Servlets

Logging and Filters

XML/HTML Parsing


  • Log interest enevents in an application
    • Configuration
    • Informational
    • Error
  • Java Logging Architecture
    • By default we just try using system out print messaging.
    • The problem is everything is always printed, so not really scalable.
      • It does however have value while piecing together your value
    • Loggers
      • Generate the log message
    • Handlers
      • Filter the log message based on any filters defined
    • Appenders
      • Write the log message
      • |Application -> | Loggers -> | Handlers -> | Appenders |
      • Loggers and Appenders could be treated the same
      • It provides a level filter that defines a relative order for log messages
        • Logging levels in Java’s default Logging package java.util.loggin
          • SEVERE
          • WARNING
          • INFO -> Log message takes priority over Fine
          • CONFIG
          • FINE
          • FINER
          • FINEST
          • From top to bottom severity
          • Levels are associated with
            • Messages
            • Loggers
            • Handlers
          • Logging Algorithm
            • A log message is sent to the Logger
              • if message’s level >= logger’s level then the logger passes the message to the handler
              • if message’s level >= handler’s level hen the handler sends the message to the appender
          • Where are the logs written?
            • Console
            • Flat files
            • Sockets
            • SMTP and SMS
            • Databases
            • Will not happen automatically, we actually need to write the logger.
          • Config.properties
            • We just need to import java.util.logging,
              • Logger infoLogger = Logger.getLogger(“cs.examples.TestLogging”);
              • infoLogger.severe(“Logging a severe ..”);…. infoLogger.finest();
              • You can define the default logging levels at the logging.properties file
              • /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib/logging.properties
              • By default they are set to info….
              • You can create your own logging file with
                • -Djava.util.logging.config.file=/usrers……/newlogging.properties
              • The logging messages are sent to all the handlers, even parent’s handlers…
                • So if there is a info handler it’ll print out info
                • A logger willl publish the log message to its parent’s handlers
              • Logger infoLogger = Logger.getLogger(“cs378.examples.TestLogging”);
                • infoLogger.setLevel(level.info);
              • Logger parentLogger = Logger.getLogger(“cs378.examples”);
                • parentLogger.setLevel(level.CONFIG);
              • Logger logger1 = Logger.getLogger(“cs378”);
                • logger1.setLevel(level.severe);
              • Name spacing ensures and manages the loggers according to their name notation
                • cs378, cs378.examples, cs378.examples.getgoing
            • Logging API versus Implementation
              • Log against an API, which allows you to swap out implementation without changing your code
              • Some popular APIs
              • Provide adapters to convert the logging API events to the logging framework
                • get from front end and log to backend
              • Log4j 2
                • Provides both aPI and implementation


  • A filter is any piece of code that intercepts requests to resources, responses from resources, or both, and act on those
  • Something that intercepts a request to a resource
  • Filters can inspect and modify requests and responses
  • Filters can reject, redirect, or forward requests
    • Logging every request and response
      * add tracking information

      • Authenticate filters
        • Centralize authentication and authorization checks
        • Error handling
          • Reporting back
        • Compression and encryption
      • Creaitng a filter
        • implementing an Filter interface
      • Servlet container calls the filter’s init method to initialize it…
      • Servlet container calls the filter’s destroy method to destroy it..
        • This happens when the web application is shutdown
      • doFilter is called during the filter’s lifetime…
        • void doFilter(Servlet REquest request, ServletResponse response, FitlerChain, chain)
          • throws java.io.IOException, ServletException
          • ServletRequest is HttpServletRequest
          • ServletResponse is HttpServletResponse
            • Called ServletResponse because it may be used on other protocols besides HTTP
          • |Servlet container -> | Filter 1 ->| Filter 2 —-> | Servlet Container —-Service method–> | Servlet |
          • |Servlet container |<– Filter 1 | <—- Filter 2 | <——Servlet Container | <—— Servlet |
          • service method which is part of the servlet interface, that is called by the servlet container..
        • Fitler Mappings
          • Filters can be mapped to URL Patterns
          • If the same URL pattern is mapped toboth, a Servlet and a Filter, then the Filter will be called first.
            • Three options
              • Deployment descriptor
                • filter-mapping
                  • filter-name My filter /flter-name
                  • url-pattern /foo /url-pattern
                  • dispatcher REQUEST dispatcher
                • /filter-mapping
                • Dispatcher tag means the filter will respond to normal requests, which is the default value
                • Ordered by the order specific
              • Annotation
            • @WebFilter(filterName=“myFilter”, urlPatterns={“/foo”}, dispatcherTypes={DispatcherType.REQUEST})
              * public class MyFilter implements Filter{}
              * Cannot be ordered

              • Configure using ServletContext
              • Must be done before ServerContext finishes
                • pgs 238–239
              • filter section
                • filter
                  • filter-name
                    • filter-class
                • /filter
              • @WebServlet()

XML/HTML Parsing

  • Given an HTML document, output specific nodes from that match a Query criteria…
    • Example

    • cs378
      • assignments
        • assignment Caching Proxy /assignment
        • assignment Servlets /assignment
      • /assignments
    • /cs378

    • Query critera: ‘assignment’ node
      • assignment Caching Proxy /assignment
      • assignment Servlets /assignment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s