The previous sections showed how you can write ATG servlet beans that generate JSP code from Java code, while still being able to display parameters defined in a JSP and setting parameters for those displayed parameters.

These functions give you the ability to write applications that completely separate JSP formatting from Java functionality, which is one of the main goals of tag libraries. This separation is essential in large applications because it allows JSP designers and Java coders to work together and maintain autonomy.

As an example, consider the following ATG servlet bean that displays a list of numbers. Name this servlet bean Counter.java and create a class and component for it as described in Simple ATG Servlet Bean Example:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import atg.servlet.*;

public class Counter extends DynamoServlet {
public Counter () {}
public void service (DynamoHttpServletRequest request,
                     DynamoHttpServletResponse response)
     throws ServletException, IOException
{
  ServletOutputStream out = response.getOutputStream ();
  out.println ("<ul>");
  for (int i = 0; i < 10; i++) {
    out.println ("<li>This is number " + i);
  }
  out.println ("</ul>");
}
}

This ATG servlet bean might be invoked from a JSP like this:

<%@ taglib uri="/dspTaglib" prefix="dsp" %>
<dsp:page>

<html>
<head><title>Counter</title></head>
<body><h1>Counter</h1>

<dsp:droplet name="/test/Counter">
</dsp:droplet>

</body></html>

</dsp:page>

At first, this looks like a simple and workable solution. The problem is that the ATG servlet bean now contains formatting information. This formatting information is usually subject to change many times during the course of development. If every change requires a designer to ask a Java developer to change and recompile a Java class, the simple solution becomes an obstacle.

When you use ATG servlet beans, you can rewrite the previous example so that all of the JSP is removed from the Java object, while the functionality is retained by the Java object:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import atg.servlet.*;

public class Counter2 extends DynamoServlet {
public Counter () {}
public void service (DynamoHttpServletRequest request,
                     DynamoHttpServletResponse response)
     throws ServletException, IOException
{
  ServletOutputStream out = response.getOutputStream ();
  for (int i = 0; i < 10; i++) {
    request.setParameter ("number", new Integer (i));
    request.serviceParameter ("lineformat", request, response);
  }
}
}

This new ATG servlet bean has no formatting left in it at all. Instead, the ATG servlet bean relies on the formatting to be passed as a parameter called lineformat. It then uses the number parameter to set the value for each line. The ATG servlet bean is then invoked from a JSP like this:

<%@ taglib uri="/dspTaglib" prefix="dsp" %>
<dsp:page>

<html>
<head><title>Counter</title></head>
<body><h1>Counter</h1>

<ul>
<dsp:droplet name="/test/Counter2">
  <dsp:oparam name="lineformat">
    <li>This is number <dsp:valueof param="number"/>
  </dsp:oparam>
</dsp:droplet>
</ul>

</body></html>

</dsp:page>

Now all formatting information is concentrated in JSP files, making it much easier for a JSP developer to get at it.


Copyright © 1997, 2015 Oracle and/or its affiliates. All rights reserved. Legal Notices