package com.cv.servlet.filter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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 javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
/**
* Servlet implementation class LoginServlet
*
* @author Chandra Vardhan
*
*/
public class LoginServlet extends HttpServlet {
private final String userID = "chandra";
private final String password = "kodam";
private static final Logger LOGGER = Logger.getLogger(LoginServlet.class);
/**
* Constructor of the object.
*/
public LoginServlet() {
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
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
LOGGER.info("Entered into doGet(HttpServletRequest ,HttpServletResponse ) of LoginServlet class... ");
String user = request.getParameter("userName");
String pwd = request.getParameter("password");
if (userID.equals(user) && password.equals(pwd)) {
ServletContext context = getServletContext();
context.setAttribute("user2", "chandra");
HttpSession session = request.getSession();
session.setAttribute("user1", "chandra");
session.setMaxInactiveInterval(30 * 60);
Cookie userName = new Cookie("user", user);
userName.setMaxAge(30 * 60);
response.addCookie(userName);
response.sendRedirect("success.jsp");
} else {
RequestDispatcher rd = getServletContext().getRequestDispatcher(
"/login.html");
PrintWriter out = response.getWriter();
out.println("<font color=red>Either user name or password is wrong. Please look at logs... </font>");
LOGGER.info("Enter username = 'chandra' and password = 'kodam' ... ");
rd.include(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 LoginServlet class... ");
doGet(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