Java EE Tutorials - Java Server Pages

We spent quite a few posts discussing Java Servlet technology, but now it is time to move onto another part of the Java EE stack - Java Server Pages. At this point there are number of paths we can start down, but since JSP’s both build upon and work closely with Servlets, it seems like a logical next step.

The Basics

  • First released in 1999, making the technology almost as old as Servlets.

JSP allows you to write HTML pages which contain embedded Java code. When you request the page, the server will run the embedded code, which usually renders more markup and other data. The final result is (or should be) a valid HTML page, which your web browser will then render.

Essentially, JSP pages are the Java equivalent to PHP, or the ERB templates that are used in Ruby on Rails.

Relationship to Servlets

So how do JSP pages relate to Servlets? Since the pages contain Java code, they actually have to be compiled by the web server. But here’s the catch - when we write a JSP page, we don’t have to define a full fledged Java class. We can simply write arbitrary lines of Java. So how exactly does this code get compiled? How could it possibly be valid?

The secret is that each JSP page is actually compiled into a Servlet class which is able to render the final content.

JSP and MVC

JSP pages can be used all on their own, but doing so isn’t that useful. All that embedded code won’t do a lot of good without any data to work against. We can solve this problem by using our pages as the View component in a Model-View-Controller framework. There are quite a few MVC solutions for Java, such as Apache Struts and Spring MVC, but you can also make a poor man’s MVC setup with nothing but Servlets and JSP. We’ll take a deeper look at this in another post.

General Observations

JSP and Servlets have a lot in common (aside from the fact that one is compiled into the other). They’re both fairly old, and they’re both used as the backbone for more advanced frameworks and features.

One thing they don’t have in common is reputation. There have been minor quibbles over the years regarding Servlets, but they’re still considered a useful and acceptable technology.

Java Server Pages, on the other hand, have fallen out of favor over the years. There a number of reasons for this, some better than others:

  • Many developers dislike JSP because it can lead to bad practices. If you ever run into an old, JSP-heavy Enterprise web application, there’s a good chance you’ll come across at least one page containing several large classes worth of Java code. This practice isn’t encouraged, but it is allowable, which means that someone, somewhere is going to choose to behave badly. The answer, at least among some developers, is to simply prevent it from happening by avoiding JSP entirely.
  • It turns out that JSP is not the only way to render dynamic web pages in a Java EE web application. There’s a newer alternative, named Java Server Faces, which is also part of the Java EE stack. It works very differently than JSP, and I’m not going to be shy in declaring that I don’t like it at all. Oracle, however, loves the hell out of it, and for years they have been pushing its adoption, while discouraging the use of JSP. We’re not yet at the point in which JSP has been deprecated, but at this rate it wouldn’t surprise me to see such a move in the coming years.

Now for my rebuttals:

  • Yes, JSP can be used to create evil, nasty, messy code. But so can ERB, and I’ve never heard similar complaints from Rubyists. That tells me that the problem is not with the technologies, but the developers using them. Java is a hugely popular language which attracts developers of many skill levels, some of which are bound to be undisciplined and willing to write crappy JSP pages. Banning it from a project doesn’t solve anything, and when used carefully, JSP can be quite useful.

  • Maybe it is because I hate it with the intensity of a thousand burning suns, but I can’t for the life of me figure out Oracle’s obsession with Java Server Faces. This isn’t simply a case where Java developers recommend or highly suggest its use. Rather, I’ve seen Oracle’s Java EE evangelists write blog posts and comments in which they are shocked (shocked!) to hear that anyone would want to use anything but JSF. For example - the upcoming Java EE 8 is going to have a new, modern MVC feature as an alternative to JSF (and classic JSP). And here are two blog posts from Oracle employees in which they discuss this new feature as if it is diseased. They can barely hide their disgust (I’ve seen far, far more extreme reactions, but unfortunately I can’t remember where they were).

The bottom line is that JSP is not evil. And if you know what you’re doing, it can be a hell of a lot simpler and easier to use than anything that’s come around since. Like with anything, it all depends on the situation and the use case.

Next Steps

In our next post, we will take a look at how to create and serve up basic JSP pages.