package com.cv.servlet.cookie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class MyCookie2 extends HttpServlet {
private static final Logger LOGGER = Logger.getLogger(MyCookie2.class);
/**
* Constructor of the object.
*/
public MyCookie2() {
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 MyCookie2 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 MyCookie2 class... ");
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String string1 = getServletContext().getInitParameter("first_name");
String string2 = getServletContext().getInitParameter("last_name");
System.out.println(string1);
System.out.println(string2);
// Create cookies for first and last names.
Cookie firstName = new Cookie("first_name", string1);
Cookie lastName = new Cookie("last_name", string2);
// Set expiry date after 24 Hrs for both the cookies.
// firstName.setMaxAge(60*1);
// lastName.setMaxAge(60*6);
// Add both the cookies in the response header.
response.addCookie(firstName);
response.addCookie(lastName);
String title = "Setting Cookies Example";
String docType = "<!doctype html public \"-//w3c//dtd html 4.0 "
+ "transitional//en\">\n";
out.println(docType + "<html>\n" + "<head><title>" + title
+ "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n"
+ "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n"
+ " <li><b>First Name</b>: " + string1 + "\n"
+ " <li><b>Last Name</b>: " + string2 + "\n" + "</ul>\n"
+ "</body></html>");
// request.setAttribute("mycoo",firstName );
RequestDispatcher dispatcher = request
.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
/*
* RequestDispatcher dispatcher = request
* .getRequestDispatcher("/ReadCookie"); dispatcher.forward(request,
* response);
*/
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
} |
No comments:
Post a Comment