package com.example.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.example.service.MainUI;


/**
 * Servlet implementation class FrontController
 */
@WebServlet(urlPatterns={"/FrontController"})
public class FrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MainUI mainUi;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FrontController() {
    	this.mainUi = new MainUI();
        
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest (request, response);
	}

	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}	
	
	
	/**
	 * Common method to process all client requests (GET and POST)
	 */
	private void processRequest(HttpServletRequest request, HttpServletResponse response) {

		String forwardToJsp = null;						
		String action;
		if((action = request.getParameter("action")) == null) {
			action = "";
		}
		
		switch (action) {
		case "login":
			System.out.println("working front");
			forwardToJsp = mainUi.login(request, response);
			break;
			
		case "logout":
			HttpSession session = request.getSession();
			session.setAttribute("username" username);
			forwardToJsp = mainUi.logout(request, response);
			break;
			
		case "register":
			forwardToJsp = mainUi.register(request, response);
			break;
		
		default: 
			forwardToJsp = "/errorPage.jsp";
			break;
		}
		
		forwardToPage(request, response, forwardToJsp);
	}
	
	/**
	 * Forward to server to the supplied page
	 */
	private void forwardToPage(HttpServletRequest request, HttpServletResponse response, String page){
		
		//Get the request dispatcher object and forward the request to the appropriate JSP page...
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(page);
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

@@ -1661,219 +1661,181 @@
 %0A%09%09%09
-System.out.println(%22working front%22);%0A%09%09%09forwardToJsp = mainUi.login(request, response);%0A%09%09%09break;%0A%09%09%09%0A%09%09case %22logout%22:%0A%09%09%09HttpSession session = request.getSession();%0A%09%09%09session.setAttribute(%22username%22 username);
+HttpSession session = request.getSession();%0A%09%09%09//session.setAttribute(%22username%22 username);%0A%09%09%09forwardToJsp = mainUi.login(request, response);%0A%09%09%09break;%0A%09%09%09%0A%09%09case %22logout%22:
 %0A%09%09%09

move session to login case