JATO Tutorial

Chapter 1, Task 2

Task 2—Create the Login page

Let's create the first page of our application.

Step 1: Create the Login page JSP

  1. Copy the template file __page__.jsp from the /templates/1_2/ModuleTemplates directory to your /jatotutorial/module1 directory. Please note that we are now working with the documents directory, not the Java packages directory in the /WEB-INF/classes directory

    /JatoTutorial/jatotutorial/module1/__page__.jsp

  2. Rename the file Login.jsp
  3. Edit the file and replace the following tokens as indicated:

    Token Replacement Text
    __appPackage__ jatotutorial
    __modulePackage__ module1
    __page__ Login

  4. Add the JATO JSP tags for the message field, the name entry field, and the submit button as indicated below:

<%@page info="Login" language="java"%>
<%@taglib uri="/WEB-INF/jato.tld" prefix="jato"%>
<jato:useViewBean className="jatotutorial.module1.LoginViewBean">
...
<html>
<head>
<title>Login</title>
</head>
<body>
<jato:form name="Login">
    <h1><jato:text name="statusMessage" escape="true"/></h1>
    <p>
    Login:<jato:textField name="login" size="20" maxLength="20"/>
    <p>
    Password:<jato:password name="password" size="20" maxLength="20"/>
    <p>
    <jato:button name="submit"/>
</jato:form>
</body>
</html>
</jato:useViewBean>

Step 2: Create the Login ViewBean

  1. Copy the template file __page__ViewBean.java from the /templates/1_2/ModuleTemplates directory to your /WEB-INF/classes/jatotutorial/module1 directory.
  2. /JatoTutorial/WEB-INF/classes/jatotutorial/module1/__page__ViewBean.java
  3. Rename the file LoginViewBean.java
  4. Edit the file and replace the following tokens as indicated:

    Token Replacement Text
    __appPackage__ jatotutorial
    __modulePackage__ module1
    __page__ Login

  5. As indicated below, create the child name constants CHILD_UID, CHILD_PWD, CHILD_SUBMIT, and CHILD_MESSAGE. (Note that the values for these name constants must correspond exactly with the field names in the JSP we just created.) Also add the code for the createChild(), registerChildren(), and handleSubmitRequest() methods. Note, some parts of the template code have been omitted here. Any omitted sections are not pertinent to this example.
package jatotutorial.module1;

import java.io.*;
import java.lang.reflect.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import jatotutorial.*;
import com.iplanet.jato.*;
import com.iplanet.jato.model.*;
import com.iplanet.jato.model.sql.*;
import com.iplanet.jato.util.*;
import com.iplanet.jato.view.*;
import com.iplanet.jato.view.event.*;
import com.iplanet.jato.view.html.*;

public class LoginViewBean extends ViewBeanBase
{

    public LoginViewBean()
    {
        super(PAGE_NAME);
        setDefaultDisplayURL(DEFAULT_DISPLAY_URL);
        registerChildren();
    }

    protected void registerChildren()
    {
        registerChild(CHILD_MESSAGE,StaticTextField.class);
        registerChild(CHILD_LOGIN,TextField.class);
        registerChild(CHILD_PASSWORD,TextField.class);              
        registerChild(CHILD_SUBMIT,Button.class);
    }
	
    protected View createChild(String name)
    {
        if (name.equals(CHILD_MESSAGE))
        {
            StaticTextField child = 
                new StaticTextField(this, CHILD_MESSAGE, "");
            return child;
        }
        else
        if (name.equals(CHILD_LOGIN))
        {
            TextField child = 
                new TextField(this, CHILD_LOGIN, "");
            return child;
        }
        else
        if (name.equals(CHILD_PASSWORD))
        {
            TextField child = 
                new TextField(this, CHILD_PASSWORD, "");
            return child;
        }
        else                  
        if (name.equals(CHILD_SUBMIT))
        {
            Button child = 
                new Button(this, CHILD_SUBMIT, "Submit");
            return child;
        }
        else
            throw new IllegalArgumentException(
                "Invalid child name \""+name+"\"");
    }

    ...

    public void handleSubmitRequest(RequestInvocationEvent event)
        throws ServletException, IOException 
    {	
        // Retrieve the login name & password
        String loginName = getDisplayFieldStringValue(CHILD_LOGIN);
        String password = getDisplayFieldStringValue(CHILD_PASSWORD);
              
        String theMessage = "";
              
        // Check the login name
        // Note, we don't check the password in this example
        if (loginName.equalsIgnoreCase("anton") || 
            loginName.equalsIgnoreCase("alfki") ||
            loginName.equalsIgnoreCase("bonap"))
        {
            theMessage = "Congratulations, " + loginName + 
                ", you are now logged in!"; 
        }    
        else   
        {         
            theMessage = "Sorry, " + loginName +  
                ", your login or passsword was incorrect!";
        }

        // Set the ouput status message
        getDisplayField(CHILD_MESSAGE).setValue(theMessage);     

        // Redisplay the current page   
        forwardTo();
    }

    ... 

    // The "logical" name for this page
    public static final String PAGE_NAME="Login";

    // The URL that points to the JSP which uses this ViewBean 
    public static final String DEFAULT_DISPLAY_URL=
        "/jatotutorial/module1/Login.jsp";
	
    public static final String CHILD_MESSAGE  = "statusMessage";
    public static final String CHILD_LOGIN    = "login";
    public static final String CHILD_PASSWORD = "password";      
    public static final String CHILD_SUBMIT   = "submit";   
}