Skip to main content.

JSP Controls Tag Library

Navigation: JSP Controls | About

Overview

This page contains full source code of Login Component that you can try out on a Quick Start page.

Composite Page, index.jsp

First goes the composite page, index.jsp, that aggregates the Login Component:

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

<html>
  <head>
    <link rel="stylesheet" href="../css/simpleform.css">
    <script src="../js/behaviour.js" language="javascript"></script>
    <script src="../js/jspcontrols.js" language="javascript"></script>
  </head>

  <body>
    <p>This paragraph is defined directly in the parent page
    and should <strong>precede</strong> the content of login control.</p>

      <div id="LoginComponent">
        <jsp:include page="loginComponent.jsp"/>
      </div>

    <p>This paragraph is defined directly in the parent page
    and should <strong>follow</strong> the content of login control.</p>
  </body>
</html>

The component is inserted into a composite page dynamically using <jsp:include> action. This means, that servlet/JSP container inserts the actual HTML response generated by the component, not JSP code. Two plain text paragraphs are defined in the page to make sure that content from the main page, surrounding the component, is not lost after <jsp:include> action returns.

Login Component, loginComponent.jsp

The Login Component is defined in loginComponent.jsp file:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.ArrayList,
                 net.jspcontrols.util.Constants"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="jc" uri="http://jspcontrols.net/tags/core"%>

<jc:component id="LoginComponent">

<%-- **********************************************************************
     The stub for authentication service; allows only "user"/"pass" account
     ********************************************************************** --%>

<%!
    public boolean authenticate(String username, String password){
        return "user".equals(username) && "pass".equals(password);
    }
%>

<%-- **********************************************************************
     Input phase: a user submitted a login/logout form or clicked a logout link
     ********************************************************************** --%>

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

      // Read username and password submitted from dialog 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, so it is not stored.
      session.setAttribute("username", username);

      // Log the user in
      if (authenticate(username, password)) {
          session.removeAttribute("username");
          session.setAttribute("USER", username);

      // User accout is not found, generate error messages
      } else {
          ArrayList messages = new ArrayList();
          String message = "Account not found.";
          messages.add(message);
          session.setAttribute("MESSAGES", messages);
      }
  %>
</jc:handler>

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


<%-- **********************************************************************
     Finishing with input phase: reload component if it is the input phase
     ********************************************************************** --%>

<jc:reload/>

<%-- **********************************************************************
     Preender Phase: do URL magic. You can stick your own code in tag's body
     ********************************************************************** --%>

<jc:prerender/>

<%-- **********************************************************************
     Render Phase: login subview
     ********************************************************************** --%>

<c:if test='true'>
  <jsp:include page="loginComponent-viewLogin.jsp" />
</c:if>

<%-- **********************************************************************
     Render Phase, logout subview
     ********************************************************************** --%>

<c:if test='false'>
  <jsp:include page="loginComponent-viewLogout.jsp" />
</c:if>

<% session.removeAttribute("MESSAGES"); %>

</jc:component>

This does not differ much from the sample code used in the "Component lifecycle" section. The major change is that the views are factored out into separate JSP files. The same can be done for any component. Including one file from another is OK, but do not try to forward from included file, this will not work in most servlet containers (forwarding from included JSP fragment works on Resin though).

Login Panel, loginComponent-viewLogin.jsp

The login panel is defined in loginComponent-viewLogin.jsp file:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="jc" uri="http://jspcontrols.net/tags/core" %>

<h3>Login Component: "Log In" subview</h3>

<c:if test="${not empty sessionScope.MESSAGES}">
  <c:forEach var='message' items='${sessionScope.MESSAGES}'>
    <ul>
      <li><b>Error: ${message}</b></li>
    </ul>
  </c:forEach>
</c:if>

<form class="jspcCommand" method="get" action="${jspcComponentAddress}">
  <label for="username">Username:</label>
  <input type="text" name="username" value="${username}" maxlength="20" size="20"/><br/>

  <label for="password">Password:</label>
  <input type="text" name="password" value="" maxlength="20" size="20" /><br/>

  <span class="jspcCommand"><input type="submit" name="loginEvent" value="Log In"/></span><br/>
</form>

Logout Panel, loginComponent-viewLogout.jsp

The logout panel is defined in loginComponent-viewLogout.jsp file and looks similar:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="jc" uri="http://jspcontrols.net/tags/core" %>

  <h3>Login Component: "Log Out" subview</h3>

  <form class="jspcCommand" method="get" action="${jspcComponentAddress}">
    <label>Current user:</label>
    <div class="datavalue">${USER}</div><br/>

    <label></label>
    <span class="jspcCommand"><input type="submit" name="logoutEvent" value="Log Out"/></span><br/>

    <label></label>
    <span class="jspcCommand"><a href="${jspcComponentAddress}?logoutEvent">Log out via link</a></span><br/>
  </form>

See detailed explanation of how this code works in the "Component lifecycle" section.