Servlet listener Interface

Source: Internet
Author: User

The Web Application Server generates various events, such as Web Container startup events, session expiration events, and request arrival events. We have already elaborated on the interfaces of these events in JBuilder 2005 actual JSP development. The event processing method greatly improves the control capability of the Program on the Web application server.

In this section, we listen to the Web Container startup and shutdown events, and record the Web application startup and shutdown system logs when the events occur, this log allows the system administrator to view the startup and shutdown status of Web applications. Logs are saved in the Directory D:/serverLog as Excel files. After successful logon, you can download the log as an attachment. The function of downloading the Excel log file is implemented in the last section of this topic. Next we will create the Servlet listener that records system logs.

1. Start the wizard for creating the Servlet listener

Use File-> New...-> Web-> double-click the Listener Servlet icon to start the wizard for creating the Servlet Listener.

This step is similar to creating a standard Servlet. we specify the Class Name of the Listener: SrvStart Listener, the package name is bookstore. servlet, and press Next to Next.

2. Specify the event interface implemented by the Servlet listener

Because the starting and closing events of Web containers are defined by the ServletContextListener interface, we select this interface and press Finish to create this listener.

Open the SrvStar tListener. java created in the wizard and enter the following bold code:

 
 
  1. Package bookstore. servlet;
  2. ...
  3. Import java. io .*;
  4. Import java. util .*;
  5. Import java. text. SimpleDateFormat;
  6. Public class SrvStartListener
  7. Extends HttpServlet implements ServletContextListener
  8. {
  9. Public void contextInitialized (ServletContextEvent sce)
  10. {
  11. GregorianCalendarCal=NewGregorianCalendar ();
  12. IntYear=Cal. Get (Calendar. YEAR); // get the current YEAR
  13. IntMonth=Cal. Get (Calendar. MONTH) + 1; // get the current MONTH
  14. FileFile=NewFile ("D :\\ serverLog \ log _" + year + "_" + month + ". xls ");
  15. FileWriterFw=Null;
  16. Try
  17. {
  18. // If the file exists, the log is appended to the end of the file; otherwise, a new file is created.
  19. Fw=NewFileWriter (file, true );
  20. SimpleDateFormatSdf=NewSimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
  21. StringStarttime=Sdf. Format (new Date ());
  22. Fw. write ("at" + starttime + "Start Web Container \ n ");
  23. } Catch (IOException ex)
  24. {
  25. Ex. printStackTrace ();
  26. } Finally
  27. {
  28. Try
  29. {
  30. If (fw! = Null)
  31. {
  32. Fw. close ();
  33. }
  34. } Catch (IOException ex1)
  35. {
  36. Ex1.printStackTrace ();
  37. }
  38. }
  39. }
  40. ...
  41. }
  1. Servlet 2.5 termination response
  2. Wizard for creating Servlet Filters
  3. User cache Servlet
  4. Extract Servlet and JSP page content using the filter
  5. Servlet Context and Config

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.