package com.cv.servlet.session.listener;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
/**
* Servlet implementation class HttpSessionListenerServlet
*
* @author Chandra Vardhan
*
*/
public class HttpSessionListenerServlet extends HttpServlet {
private static final Logger LOGGER = Logger.getLogger(HttpSessionListenerServlet.class);
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HttpSessionListenerServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
LOGGER.info("Entered into doGet(--) of HttpSessionListenerServlet class... ");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// setting “dog” attribute in request object
Dog dog = new Dog( getServletContext().getInitParameter("contextParam"));
out.print("My Dog's first name is : "+ dog.getName());
HttpSession session = request.getSession();
session.setAttribute("dog", dog);
out.print("<br/>");
out.print("<br/>");
Dog dogAttribute = (Dog)session.getAttribute("dog");
out.print("My Dog's second name is : "+ dogAttribute.getName());
out.print("<br/>");
out.print("<br/>");
dog.setName("Bubby is third pet name...");
// replacing “dog” attribute in request object
session.setAttribute("dog", dog);
// remove “dog” attribute from request object.
Dog dogAttribute2 = (Dog)session.getAttribute("dog");
out.print("My Dog's third name is : "+ dogAttribute2.getName());
session.removeAttribute("dog");
session.invalidate();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
LOGGER.info("Entered into doPost(--) of HttpSessionListenerServlet class... ");
doGet(request, response);
}
} |
No comments:
Post a Comment