Article

Complete the MVC Puzzle with Struts

Page: 1 2 3 4 5 6 7 Next

The View

For the view of this application we will use 2 simple JSPs. These are shown below:

<html>  
 
   <head>  
       <title>Please Login</title>  
   </head>  
 
   <body>  
       <h1>Please enter your login information</h1>  
       <br/>  
 
       <form action="controller?action=login" method="post">  
           <table border="0">  
               <tr>  
                   <td>Username</td>  
                   <td><input type="text" name="username"/></td>  
               </tr>  
               <tr>  
                   <td>Password</td>  
                   <td><input type="password" name="password"/></td>  
               </tr>  
               <tr>  
                   <td>  
                       <input type="submit"/>  
                   </td>  
                   <td>  
                       &nbsp;  
                   </td>  
               </tr>  
           </table>  
       </form>  
   </body>  
 
</html>

The login.jsp page

This login page simply gives the user the opportunity to enter their username and password. This submits its input to the controller Servlet (shown below), telling it to invoke the login action (this is the action parameter sent by the form):

<jsp:useBean id="user" class="com.samjdalton.mvc.model.UserBean"  scope="session" />  
<html>  
 
   <head>  
       <title>Welcome</title>  
   </head>  
 
   <body>  
       <h1>Welcome <jsp:getProperty name="user" property="name"/></h1>  
 
   </body>  
</html>

The welcome.jsp page

The welcome.jsp page displays a welcome message to the user using the username they provided on the previous page. As you can see, this simply uses a JavaBean that is in the session (as shown by the useBean tag.) This bean is placed in the session by the Controller, as we’ll see next.

The Controller

A Servlet, the code for which is shown below, implements the Controller in our application:

package com.samjdalton.mvc.controller;  
 
import com.samjdalton.mvc.controller.actions.LoginAction;  
 
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.ServletException;  
import javax.servlet.RequestDispatcher;  
import java.io.IOException;  
 
 
public class Controller extends HttpServlet {  
   protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {  
       doPost(httpServletRequest, httpServletResponse);  
   }  
 
   protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {  
 
       // get the action property from the request  
       String theAction = httpServletRequest.getParameter("action");  
 
       Action action=null;  
       if (theAction.equalsIgnoreCase("login")) {  
            action = new LoginAction(httpServletRequest,httpServletResponse);  
       }  
 
       // execute the action, which returns the page to go to next  
       String page = action.execute(httpServletRequest,httpServletResponse);  
 
       RequestDispatcher rd = httpServletRequest.getRequestDispatcher(page);  
 
       // forward to the next page  
       rd.forward(httpServletRequest,httpServletResponse);  
   }  
}

Controller.class

This is a very simple controller that merely uses a request parameter (action) to decide which action to invoke. In this case, the page sends the login action as the parameter, and the LoginAction is invoked. This action implements a standard interface (Action) with one method, execute, that takes the Request and Response objects as its parameters. The action class returns the path of the next page (view) to invoke, and the user is redirected to this page.

The LoginAction in our example is shown below:

package com.samjdalton.mvc.controller.actions;  
 
import com.samjdalton.mvc.controller.Action;  
import com.samjdalton.mvc.model.UserBean;  
 
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
 
 
public class LoginAction implements Action {  
   public LoginAction(HttpServletRequest request, HttpServletResponse response) {  
   }  
 
   public String execute(HttpServletRequest req, HttpServletResponse res) {  
       // get the user name  
       String username = req.getParameter("username");  
 
       // create a UserBean  
       UserBean user = new UserBean(username);  
 
       // put it in the session  
       req.getSession().setAttribute("user", user);  
 
       // redirect to the welcome.jsp page  
       return "/welcome.jsp";  
   }  
}

LoginAction class

This action obtains the username parameter from the request, creates a new model object (UserBean), places this into the Session, and indicates that the next page in the flow is welcome.jsp.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article