package org.egjug.hashim.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.egjug.hashim.CaptchaServiceSingleton; import com.octo.captcha.service.CaptchaServiceException; /**** * @author Ahmed Hashim (eng.hashim@gmail.com) * @since June 2006 * */ public class Login extends HttpServlet { /** * The doGet method of the servlet.
* * 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 { } /** * The doPost method of the servlet.
* * 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 { Boolean isResponseCorrect = Boolean.FALSE; //remenber that we need an id to validate! String captchaId = request.getSession().getId(); //retrieve the response String turing = request.getParameter("turing"); // Call the Service method try { isResponseCorrect = CaptchaServiceSingleton.getInstance() .validateResponseForID(captchaId, turing); } catch (CaptchaServiceException e) { //should not happen, may be thrown if the id is not valid e.printStackTrace(); } if (isResponseCorrect == true) { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.write("Congratulation... Login success"); out.flush(); out.close(); } else { RequestDispatcher dis = request.getRequestDispatcher("/jsp/login.jsp"); dis.forward(request,response); } } }