package com.cv.servlet.context.attribute;
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 org.apache.log4j.Logger;
/**
* Servlet implementation class ContextAttributeServlet
*
* @author Chandra Vardhan
*
*/
public class ContextAttributeServlet extends HttpServlet {
private static final Logger LOGGER = Logger
.getLogger(ContextAttributeServlet.class);
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
LOGGER.info("Entered into doGet(HttpServletRequest, HttpServletResponse) of ContextAttributeServlet class... ");
doPost(request, response);
}
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
LOGGER.info("Entered into doPost(HttpServletRequest, HttpServletResponse) of ContextAttributeServlet class... ");
PrintWriter pw = res.getWriter();
res.setContentType("text/html");
synchronized (getServletContext()) {
getServletContext().setAttribute("fooo", "chandra");
getServletContext().setAttribute("bar", "Budwiser");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LOGGER.info("If you see fooo : 'chandra' and bar : 'Budwiser' then it is correct...");
String foooVal = (String) getServletContext().getAttribute("fooo");
if (foooVal != null && foooVal.equalsIgnoreCase("chandra")) {
LOGGER.info("fooo : 'chandra' and bar : 'Budwiser' you set here and those you got as output...");
pw.print("<font color='GREEN'>fooo : 'chandra' and bar : 'Budwiser' you set here and those you got as output...</font><br/>");
} else {
LOGGER.info("fooo : 'Vardhan' and bar : 'Calsberg' you set in ContextAttributeServlet2 program and those you got as output...");
pw.print("<font color='RED'>fooo : 'Vardhan' and bar : 'Calsberg' you set in ContextAttributeServlet2 program and THIS IS WRONG output...</font><br/>");
}
pw.print("fooo attribute value is : " + foooVal + "<br/>");
pw.print("bar attribute value is : "
+ getServletContext().getAttribute("bar"));
}
pw.close();
}
} |
No comments:
Post a Comment