Creating a JSP Page to Handle Login Action

In the following steps, you create the login_action.jsp page, which is a nonviewable page that processes the login operation.

  1. Create a JSP page and call it login_action.jsp. Accept all default settings for the JSP page.
  2. With login_action.jsp open in the Visual Editor, click and select the Page Directive on the top left corner of the page. The Property Inspector now shows the properties of the Page Directive.
  3. Click the down arrow next to the Import field. The Edit Property: Import dialog box is displayed. Select the Hierarchy tab and then select ResultSet after extending Java and SQL folders respectively. Click OK.
  4. Drag a jsp:usebean tag onto the page. Enter empsbean as the ID and browse to select hr.DataHandler as the Class. Set the Scope to session, and click OK.
  5. Position the cursor after the useBean tag and add a Scriptlet to the page. Enter the following code into the Insert Scriptlet dialog box and click OK.
    boolean userIsValid = false;
    String host = request.getParameter("host");
    String userid = request.getParameter("userid");
    String password = request.getParameter("password");
    String jdbcUrl = "jdbc:oracle:thin:@" + host + ":1521:ORACLE";
    userIsValid = empsbean.authenticateUser(jdbcUrl, userid, password, session);
    
  6. Add another Scriptlet, and add the following code to it:
    if (userIsValid){
    
  7. In the JSP page of the Component Palette, find Forward and drag it onto the page to add a jsp:forward tag onto the page. In the Insert Forward dialog box, enter employees.jsp as the value of the Page* field.
  8. Add another scriptlet, and enter the following code:
    } else {
    
  9. Add another jsp:forward tag, and this time move forward to login.jsp.
  10. Add a final Scriptlet, and enter a closing brace (}).
  11. Save your work.

To see the code that has been added to login_action.jsp, select the Source tab. The code displayed is similar to the following:

<body>
<%@ page import="java.sql.ResultSet"%><jsp:useBean id="empsbean"
                                                   class="hr.DataHandler"
                                                   scope="session"/>
<%boolean userIsValid = false;
String host = request.getParameter("host");
String userid = request.getParameter("userid");
String password = request.getParameter("password");
String jdbcUrl = "jdbc:oracle:thin:@" + host + ":1521:ORACLE";
userIsValid = empsbean.authenticateUser(jdbcUrl, userid, password, session);%> <%if(userIsValid){%><jsp:forward page="employees.jsp"/>
<%if (userIsValid){%><jsp:forward page="login.jsp"/><%}%>
</body>