Package Servlets

Source Code of Servlets.Login

package Servlets;

import Control.UserAuthenticationException;
import Control.UserMessage;
import Control.WebEngine;
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;

/**
* Сервелт авторицазии
* @author roman
*/
public class Login extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
           RequestDispatcher dispatcherJsp = request.getRequestDispatcher("/login.jsp");
           UserMessage userMessage = null;
          
           if (request.getParameter("operationtype")!=null){
                if (request.getParameter("operationtype").equalsIgnoreCase("deavtorizare")){
                    WebEngine.Deavtorizare(response, request.getSession());
                }
           }
           if(request.getParameter("login")!=null && request.getParameter("password")!=null && request.getParameter("operationtype")!=null){
               //присутствуют и логин и пароль, значит была нажата кнопка ввойти/зарегистрироваться.
               try{
                   if(request.getParameter("operationtype").equalsIgnoreCase("login")){
                       //нажали ввойти
                       //проверяем, была ли нажата кнопка "сохранить меня"?
                       boolean isToSave=false;
                       if(request.getParameter("remember")!=null){
                           isToSave=true;
                       }
                       WebEngine.Authenticate(request.getParameter("login"), request.getParameter("password"), response, request.getSession(),isToSave);
                       response.sendRedirect("Context");
                       return;
                   }
                   if(request.getParameter("operationtype").equalsIgnoreCase("register")){
                       // нажали зарегистрироваться
                       WebEngine.Registration(request.getParameter("login"), request.getParameter("password"), response, request.getSession());
                       response.sendRedirect("Context");
                       return;
                   }
               }catch (UserAuthenticationException ex){
                   request.setAttribute("exception", ex);
               }
           }
          
          
           if(request.getAttribute("exception")!=null){
               userMessage = new UserMessage(((UserAuthenticationException)request.getAttribute("exception")).getMessage());
               request.setAttribute("userException", userMessage);
           }
          
           //переходим на страницу
            if (dispatcherJsp!=null){
                dispatcherJsp.forward(request, response);
            }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
TOP

Related Classes of Servlets.Login

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.