April 8, 2010

The evolution of Servlets and JSP

This article is about clarifying how to use Servlets and JSP pages. When starting out to learn Java Web Application development you will undoubtedly have many questions about how all the terms and terminology interacts with each-other, or what the benefit is. Eg. Scriptlets, Servlets, Tags, Expression Language, Standard Actions, Custom Tags, Directives, etc

If you didn't have the good fortune to stumble upon a good book to give you a clear and solid foundation you will often be confused about when to use what etc.

So here is a simple introduction to why the basic Java Web Application Development technologies were built.

In the beginning

In the beginning there was darkness (and simplicity). The only things that existed were static html pages. They would have html and text, and that's it.

Soon after came dynamic pages, which could show things like the current date or timestamp. The server would generate the html code, and everyone was happy (see Perl and CGI scripts)

Servlets

After Java emerged, Servlets were created to allow dynamic pages to be written using the Java language.


public class NiceServlet extends HttpServlet
{ 
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
   PrintWriter out = response.getWriter(); 
   out.println("<html><head><title>A Nice Servlet</title></head><body>"); 
   out.println("The nice body of this servlet displays the date for you: "+(new java.util.Date())); 
   out.println("</body></html>"); 
   out.close(); 
 } 
}

]]>

For a time things were good, as Servlets could leverage the power of Java to create just about any kind of html content.

  • Advantages:Now you can generate dynamic html with Java.
  • Disadvantages: Very hard to maintain and cumbersome to write and update once the dynamic page starts to have a lot of stuff.

But things became complicated, because creating large dynamic html page content with lots of data required many lines of out.println() statements. So something else came along...

Scriptlets and Expressions


<% scriptlets %>

<%= expressions %>

Scriptlets and Expressions were created to simplify the generation of code from a page. With the creation of JSP pages, you could now write a scriptlet or expression and the Servlet Container (eg. Tomcat) would dynamically generate all the servlet code for you automatically (eg. all the out.println() statements).

There were other things as well that simplified things. Directives, declarations and of course server side comments.


<%@ directives %> //include other files or import java packages

<%! declarations %> //declare variables or methods outside of the service() method

<%-- comments --%> 

  • Advantages:You can do anything with Scriptlets and Expressions that you can write with Java code.
  • Disadvantages: A lot of custom code which makes the JSP page hard to maintain and read.

Standard Actions

To simplify things even more, now you didn't even have to use Scriptlets or Expressions, you could use Standard Actions

Why, because JSP pages started to get large and bogged down with Scriptlets, Expressions all over the place. It was hard to maintain (less by a Java developer) but mostly by web designers. Or at least that was the drive to simplify things (I still think today if you're developing Java web applications you need to be familiar with both HTML and Java equally);

The getProperty Standard Action in Action. The following code will display the name property of the bean called 'user' (if it's defined);
<jsp:getProperty name="user" property="name" />
The above standard action will allow the Servlet Container to convert the code to the following (which you otherwise would have had to write)
User user = (User)request.getAttribute("user");
out.println(user.getName());

But Standard Actions had a shortcoming. They weren't compatible with indexed properties. So having a Garden object which had a Tree object which had a getName() property would be a problem as you couldn't do this:

<jsp:getProperty name="garden" property="tree.name" />

Standard Actions would also do silly things like throw a Null pointer exception if the property didn't exist or was 'null'

  • Advantages: Much more cleaner and quicker to use than scriptlets and expressions
  • Disadvantages: Don't work with anything except String or primitive bean properties

EL (JSP Expression Language)

So came EL (JSP Expression Language) which allows you to do all of the above (introduced with JSP 2.0 Specification)

I'm not going to go into detail here about how the EL is used and what it does, but rest assured it's cool, once you digg it that is. I'll leave it up to others to get into it, but here's a useful link if you don't know anything about EL A JSTL Primer Part 1 - The Expression Language.

  • Advantages: Much easier to use. Empty or null values don't throw errors.
  • Disadvantages: No easy way to loop through collections or arrays.

Unfortunately though even EL has its shortcoming. It can't for one thing loop through collections or arrays.

JSTL (Java Standard Tag Library)

JSTL are custom tags which let you do things like formatting data or iterating through collections and displaying each element in a table row for example.

JSTL Tags can be used as easily as JSP standard actions. That's one advantage.

The main advantage however is that JSTL combined with EL will cover 99% of the things you will need in a JSP file.

Custom Tag Libraries

Finally, for those .1% of cases where you need some complex behavior specific to your application, you can also create your own custom tags. But that is quite an advanced topic, if any of the previous things I mentioned in this post aren't too familiar to you

If you are looking just for some custom tags, here is a good website with various libraries. JSP Tag Libraries.

So that's a quick run through the basic evolution of Servlets and JSPs. I hope you found this article useful. Please let me know if you did.