The T-Files


Wed, 19 Nov 2003

ASP - make the pain go away, please

My struggles with ASP (and JSP) have just hit a new low with a page essentially looking like this:

<%
out = ""
out = out + "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=0>" + vbNewLine

.. many more lines like this

out = out + "</TABLE>" + vbNewLine
response.write out
%>
<%
response.write "</BODY></HTML>" + vbNewLine
%>

The one sorry reason for the existence of these technologies is that you can easily output large quantities of static text:

java.io.PrintWriter pw = response.getWriter();
pw.println("my pretty html "+myCoolVariable);
pw.println("<p>more of my pretty html");

becomes a more friendly

my pretty html <%= myCoolVariable %>
<p>more of my pretty html

Here is a step-by-step tutorial that introduces harmful and ugly measures you can take to end up with code like above:

  1. You have heard server pages are bad because they embed a lot of programming logic in HTML, thus mixing presentation and logic, which should be separated. You amend this by reverting to old school, which has the reverse situation and put the HTML back into the programming language. Since you are still making a server page, you have to slap tags around everything to turn it into a scriptlet.
    <%
    java.io.PrintWriter pw = response.getWriter();
    pw.println("my pretty html "+myCoolVariable);
    pw.println("<p>more of my pretty html");
    %>
    
  2. You have heard that you should buffer output before sending it over the network. You have not heard that your application server is managing buffers for you. So you decide to collect all the output before printing it. You do not see why you should use those extravagant StringBuffer things when it is much easier to just concatenate strings repeatedly.
    <%
    String out = "";
    out = out + "my pretty html "+myCoolVariable;
    out = out + "<p>more of my pretty html";
    response.getWriter().println(out);
    %>
    
  3. You think your scriplet is too long. Also, you find yourself using the last line quite often in other pages and want to factor it out into something you can more easy copy and paste.
    <%
    String out = "";
    out = out + "my pretty html "+myCoolVariable;
    out = out + "<p>more of my pretty html";
    %>
    
    <%
    response.getWriter().println(out);
    %>
    
  4. Change from Java/JSP to Visual Basic/ASP.