0 Replies Latest reply: Feb 6, 2013 12:43 PM by 985905 RSS

    Model view controller issue

    985905
      Hi folks

      I'm new to java and j2ee world so bear with , I'm implementing the Service to worker pattern, what I need is to perform a user login task

      FrontController(which is a servlet)-- sends login info to -->ViewHelper(which is a custom tag)-- that creates custom html according to whether the user is logged or not and populate the-->View(which is a jsp page)

      1. The user sends the user name and the password via get/post method
      2. The front controller will send those parameters to the view helper
      3. The view helper access the model and verify whether this user exists or not
      4. The view helper populates the view according to the login state

      And here is the implementation

      Front Controller part :
      package presentation;
      import java.io.IOException;
      import javax.servlet.*;
      import javax.servlet.annotation.*;
      import javax.servlet.http.*;

      @WebServlet("/FrontController")
      public class FrontController extends HttpServlet {
           private static final long serialVersionUID = 1L;
      public FrontController() {
      super();
      // TODO Auto-generated constructor stub
      }
           protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                process(request,response);
           }
           protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                process(request, response);
           }
           protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                What should I put here to send the parameters  to the login view helper

           }//end of process
      }//end of front controller


      ViewHelper LoginHelper part:

      package helpers;

      import java.io.IOException;
      import javax.servlet.jsp.JspWriter;
      import javax.servlet.jsp.JspException;
      import javax.servlet.jsp.tagext.SimpleTagSupport;
      import model.*;

      public class LoginHelper extends SimpleTagSupport {
           
           UserBean ub;
           @Override
           public void doTag() throws JspException, IOException {     
                JspWriter writer = getJspContext().getOut();
                ub = new UserBean();
                if(ub==null)
                {
                     writer.println("<span id='loginInfo'>Your not logged in yet!</span>");
      writer.println("<span id='loginInfo'>Wrong user name or password!</span>");
                }
                else if(ub!=null)
                {
                     writer.println("<span id='loginInfo'>Welcome " + ub.getUsername() + "</span>");
                }
           }
           
           
           protected UserBean getUser(String username,String password)
           {
                //Get data from the business model layer and compare them with the sent paramters from the
      //Front controller if the user exists

      how to get the user sent parameters from the controller to make authentication validation

                UserBean user = new UserBean();
                //Set the user parameters if there is not user set the user to null
                return user;
           }//end get user
           

      }//end Login helper class

      The view part :
      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%>
      <%@ taglib uri="/WEB-INF/helloTagLib.tld" prefix="helloTagLib" %>
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Insert title here</title>
      </head>
      <body>
      <helloTagLib:LoginHelper/>
      </body>
      </html>

      By the way I tested the view helper as a custom tag and it works fine, the only issue is how to pass the parameters to it from the front controller?

      Edited by: 982902 on 6 févr. 2013 10:43