package com.cv.servlet.http;
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 ServletSessionAttributeServlet
*
* @author Chandra Vardhan
*
*/
public class HttpSessionServlet extends HttpServlet {
private static final Logger LOGGER = Logger.getLogger(HttpSessionServlet.class);
/**
* Constructor of the object.
*/
public HttpSessionServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
LOGGER.info("Entered into doGet(HttpServletRequest ,HttpServletResponse ) of HttpSessionServlet class... ");
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
LOGGER.info(
"Entered into doPost(HttpServletRequest ,HttpServletResponse ) of HttpSessionServlet class... ");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
HttpSession httpSession = request.getSession();
if(n != null ) {
httpSession.setAttribute("name", n);
LOGGER.info("httpSession.setAttribute('name', ... ");
// Object user = httpSession.getAttribute("name");
// out.print("<font size=5> If you get the content in 'GREEN' color then
// that is correct... If you get 'RED' then that is
// WRONG...</font><br/>");
if ("Chandra".equalsIgnoreCase(n.toString())) {
// out.print("<font color='GREEN'>The value of 'bar' attribute is :
// "+barAttribute+"</font><br/>");
LOGGER.info("httpSession.getAttribute('name')..." + n);
} else {
// out.print("<font color='RED'>The value of 'bar' attribute is :
// "+barAttribute+"</font><br/>");
LOGGER.info("httpSession.getAttribute('name')..." + n);
}
out.println("Welcome : " + n);
out.print("<br/><a href='HttpSessionServlet2'>visit next page</a>");
}
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
} |
great
ReplyDelete