Saturday 6 September 2014

Add ServletRequestListener using WebListener

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class RequestListener implements ServletRequestListener {

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("Request Destroyed");
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
       System.out.println("Request initialized");
       
    }
}

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.*;

@WebServlet(urlPatterns = {"/SampleApp"})
public class SampleApp extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException{
        try(PrintWriter out = res.getWriter()){
            ServletContext context = req.getServletContext();
            System.out.println("In doGet method");
        }
    }
}

Run ' SampleApp', server console has messages like below.

Info:   Request initialized
Info:   In doGet method
Info:   Request Destroyed



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment