Archive for the ‘JSP/Servlets’ Category

Free SCWCD Mock Exam for EL

24 August 2009

This practice exam focuses on EL. For those using Head First Servlets and JSP, 2nd Edition it provides a lot of practice for the last part of chapter 8.

Click here to read more…

Free SCWCD Mock Exam for JSPs

22 August 2009

This practice exam focuses on Java Server Pages.

Java Server Pages is the technology that changed the world of Web Applications. Developed on the robustness of Servlets, JSPs are the key component of the modern Web frameworks and the basis of promising new technologies like JSF.

This free practice material consists of 71 originally created questions. For those using Head First Servlets and JSP, 2nd Edition it’s a perfect companion for chapters 7-8.

Click here to read more…

Free SCWCD Mock Exam for Servlets

12 March 2009

This practice exam focuses on Servlets. For those using Head First Servlets and JSP, 2nd Edition it’s a perfect companion for chapters 4-6.

Click here to read more…

Custom Tags Tutorial

24 January 2009

This tutorial presents practical skills for Custom Tags; the most powerful feature of the JSP technology. Its purpose is to be a good review for those preparing for Sun Certified Web Component Developer certification and, at the same time, a good reference at work.

  1. Skills for Tag Files
  2. Skills for Simple Tag handlers
  3. Skills for Classic Tag handlers
  4. Body manipulation of Classic Tags

Thank you.

Custom Tags 4 – Body manipulation of Classic Tags

19 January 2009

This post explains how to manipulate the body of a classic tag.

Step 1. Write a BodyTagSupport and provide its doAfterBody() method.

package my;
public class Body extends BodyTagSupport {
  public int doAfterBody() {
    try {
      final String body = bodyContent.getString();
      final StringBuilder result = new StringBuilder();
      for (int index = 0; index < body.length(); index = index + 2) {
        result.append(body.charAt(index));
      }
      bodyContent.getEnclosingWriter().print(result.toString());
    } catch (Exception e) {
      e.printStackTrace();
    }
    return SKIP_BODY;
  }
}

Step 2. Declare it in a TLD.

<taglib ...>
  <uri>nikojava</uri>
  <tag>
    <name>body</name>
    <tag-class>my.Body</tag-class>
    <body-content>scriptless</body-content>
  </tag>
</taglib>

Step 3. Call it!

<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    (Before)
    <mari:body>ABABABABABABAB</mari:body>
    (After)
  </body>
</html>
Filtering the even characters of a classic tag's body

Filtering the even characters of a classic tag's body

As a result, only the characters at an even position (0, 2, 4, etc.) are printed!

Explanation

BodyTagSupport IS-A TagSupport. However it offers an extra weapon: EVAL_BODY_BUFFERED. If doStartTag() returns this value, then a series of methods is invoked: setBodyContent(BodyContent), then doInitBody(), then the body is evaluated and doAfterBody() is about to execute. At this point we do have access to the actual body content.

In the above example doStartTag() is not overriden, so by default EVAL_BODY_BUFFERED is returned. The actual content is taken as a string,

  final String body = bodyContent.getString();

and its even characters are selected:

  for (int index = 0; index < body.length(); index = index + 2) {
    result.append(body.charAt(index));
  }

More examples

Color alternate letters

public int doAfterBody() {
  try {
    final String body = bodyContent.getString();
    final StringBuilder result = new StringBuilder();
    for (int index = 0; index < body.length(); index++) {
      if (isOdd(index)) {
        result.append("<span style='color:red'>");
        result.append(body.charAt(index));
        result.append("</span>");
      } else {
        result.append(body.charAt(index));
      }
    }
    bodyContent.getEnclosingWriter().print(result.toString());
  } catch (Exception e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
}
private boolean isOdd(int number) {
  return number % 2 == 1;
}
<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    (Before)
    <mari:body>ABABABABABABAB</mari:body>
    (After)
  </body>
</html>
Coloring the even characters of a classic tag's body

Coloring the even characters of a classic tag's body

Remove capital letters

public int doAfterBody() {
  try {
    final String body = bodyContent.getString();
    final StringBuilder result = new StringBuilder();
    for (int index = 0; index < body.length(); index++) {
      final char current = body.charAt(index);
      if (!Character.isUpperCase(current)) {
        result.append(current);
      }
    }
    bodyContent.getEnclosingWriter().print(result.toString());
  } catch (Exception e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
}
<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    (Before)
    <mari:body>Google Sun JBoss</mari:body>
    (After)
  </body>
</html>
Disposing the capital letters of a custom tag's body

Disposing the capital letters of a custom tag's body

Have fun

public int doAfterBody() {
  try {
    final StringBuilder body = new StringBuilder(bodyContent.getString());
    final int initialSize = body.length();
    for (int index = 0; index < initialSize; index++) {
      bodyContent.getEnclosingWriter().append(body);
      bodyContent.getEnclosingWriter().append("<br />");
      body.deleteCharAt(body.length() - 1);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return SKIP_BODY;
}
<%@ taglib prefix="mari" uri="nikojava" %>
<html>
  <body>
    <mari:body>Microsystems</mari:body>
  </body>
</html>
Having fun with the body of a classic tag

Having fun with the body of a classic tag