Skip to main content.

JSP Controls Tag Library

Navigation: JSP Controls | About

Activating web component

Component is activated by sending a request that contains an event parameter. An event parameter can be sent either via Submit tag when HTML form is submitted, or via Link tag that generates an HTML <A> link.

Accept phase

For an HTML form, the submit element must contain event name in its event attritubute. Recall the view fragment rendered on initial page load step:

<jc:render view="notloggedin">
  <h3>Log In</h3>
    <jc:form>
      Name: <input type="text" name="username" value="<c:out value=""/>"/><br/>
      Password: <input type="password" name="password" value="" />
      <jc:submit event="login" value="Log In"/>
    </jc:form>
</jc:render>

The <jc:form> tag generates a proper HTML FORM tag, setting the name of component JSP file in its action attribute. Therefore, when the form is submitted, the request containing username, password and login event parameter will be sent directly to the component.

Accepting Input And Handling An Event

Because the Handler tags are placed in component's JSP file first, they get a chance to process the request first. The body of a Handler tag is evaluated if value of request contains parameter with the same name as content of tag's event attribute:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="jc" uri="http://jspcontrols.net/tags/core" %>
<jc:component id="LoginComponent">

  <%-- Accept phase --%>

  <jc:handler event="login">
    <%
      // Log out current user first
      session.removeAttribute("USER");

      // Read username and password submitted from login form
      String username = request.getParameter("username");
      String password = request.getParameter("password");

      // Store user name in session if needed to redisplay.
      // No need to redisplay password.
      session.setAttribute("username", username);

      // Account found
      if (authenticate(username, password)) {
          // Log in user
          session.setAttribute("USER", username);
          // No need to cache username after login
          session.removeAttribute("username");

      // User accout not found, generate error message
      } else {
          // Generate error message
          ArrayList messages = new ArrayList();
          String message = msgs.getMessage("login.accountnotfound");
          messages.add(message);

          // Store errors in the session for use at render phase
          session.setAttribute("MESSAGES", messages);
      }
    %>
  </jc:handler>
  <jc:handler name="logout"> ... </jc:handler>
  <jc:reload/>

  <%-- Render phase --%>

  <jc:prerender> ... </jc:prerender>
  <jc:render view="notloggedin"> ... </jc:render>

  <jc:render view="loggedin"> ... </jc:render>
</jc:component>

If the user provided proper credentials, the user name is saved in session-scoped variable USER, otherwise the USER variable is removed from the session. This fact is used in the body of Prerender tag to select a view to display.

After the user information has been processed, the component must proceed to render phase and redisplay itself. If incorrect credentials were submitted, the Login Component should generate an error message and redisplay the login form. If credentials were correct and account has been found, the Login Component should display user information and logout button.

There ar three ways to proceed to render phase from accept phase: