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:
- Package bookstore. servlet;
- ...
- Import java. io .*;
- Import java. util .*;
- Import java. text. SimpleDateFormat;
- Public class SrvStartListener
- Extends HttpServlet implements ServletContextListener
- {
- Public void contextInitialized (ServletContextEvent sce)
- {
- GregorianCalendarCal=NewGregorianCalendar ();
- IntYear=Cal. Get (Calendar. YEAR); // get the current YEAR
- IntMonth=Cal. Get (Calendar. MONTH) + 1; // get the current MONTH
- FileFile=NewFile ("D :\\ serverLog \ log _" + year + "_" + month + ". xls ");
- FileWriterFw=Null;
- Try
- {
- // If the file exists, the log is appended to the end of the file; otherwise, a new file is created.
- Fw=NewFileWriter (file, true );
- SimpleDateFormatSdf=NewSimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
- StringStarttime=Sdf. Format (new Date ());
- Fw. write ("at" + starttime + "Start Web Container \ n ");
- } Catch (IOException ex)
- {
- Ex. printStackTrace ();
- } Finally
- {
- Try
- {
- If (fw! = Null)
- {
- Fw. close ();
- }
- } Catch (IOException ex1)
- {
- Ex1.printStackTrace ();
- }
- }
- }
- ...
- }
- Servlet 2.5 termination response
- Wizard for creating Servlet Filters
- User cache Servlet
- Extract Servlet and JSP page content using the filter
- Servlet Context and Config