Front Controller

Script 1.0 FrontController.jsp

/*
//
//$Models.iNetModels.Com-Id:Administrator 12:00 PM 07/20/2006 $
// Copyright (C) 2006 The iNetModel Development Team
// FrontController Servlet Models.iNetModels.Com
//
// Script 1.0 FrontController.jsp
//
*/

Import javax.servlet.*;
Import javax.servlet.http.*;

public class FrontController extends HttpServlet {
public static final String USERNAME_PARAM = "username";
public static final String PASSWORD_PARAM = "password";
public static final String USERBEAN_ATTR= "userbean";

public static final String CONTROLLER_PARAM = "controller";

public void init(ServletConfig config)
throws ServletException {
super.init(config);
}

public void destroy() {
}

// handle get requests
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {

//read the parameters from the request for the default next page

String nextPage = request.getRequestURL();

//strip off the prefix
nextPage =
   Nextpage.substring(CONTROLLER_PREFIX.LENGHT());

//find the userbean from session
HttpSession session = request.getSession(true);
UserBean userBean =
(UserBean)session.getAttribute(USERBEAN_ATTR);

if(userBean == null || !userBean.IsLoggedIn()) {
//read requst parameters
   String username =
     request.getParameter(USERNAME_PARAM);
   String password =
     request.getParamete(PASSWORD_PARAM);

//IF IT DOESNT EXIST CREATE ONE
if(userBean == null) {
userBean = UserBeanFactory.newInstance();
session.setAttribute(USERBEAN_ATTR,userBean);
}

//record username and password
userBean.setUsername(username); userBean.setPassword(password);

//attempt to login
boolean result = userBean.doLogin();

if (!result)
nextPage = "login.jsp";
}
//transfer control to the selected page controller "View"
    RequestDispatcher dispatcher =
    getServletContext().getRequestDispatcher(nextPage);
    dispatcher.forward(request, response);
   }
  }

//if the user is logged in the request is forwarded immediately to
//the requested URL--but with the prefix stripped off.