Websites and Java (2000)

Java is very often associated with the world wide web and websites. This article puts Java in perspective and gives an overview of the possibilities of Java on the web.

Introduction

The internet is a world-wide network of computers and devices. This world-wide network allows many protocols over it, one of them being http. Http is the protocol that is used by web browsers to retrieve content from a web server. This is what we mostly refer to as the world wide web.

Java & the web

Websites can present webpages to users. There are two uses for Java in these websites:

  1. Applets - small applications that are dynamically downloaded by the webbrowser when the webpage refers to an applet
  2. Dynamically generated webpages - Java is invoked by the webserver to generate webpage content, for example by accessing a database

There are also alternatives for applets in the market, that store the downloaded Java application permanently on the hard-drive. This makes them download-once instead of cache-only.

The rest of this article will discuss the dynamically generated webpages.

Servlets

To make dynamically generated HTML pages possible, the servlet APIs were introduced in Java. It is a standardized API with ways to develop Java programs that plug into the webserver and can reveive requests and send responses. Vendors can then implement the servlet API and offer a run-time environment to the servlet developers.

When looking at the way this would work from end-to-end, this is how it works:

  • you enter a URL in the browser
  • an HTTP request arrives at the web server
  • web server recognizes in the URL that request is for a servlet, and forwards the request to the servlet engine
  • the servlet engine recognizes in the URL what servlet needs to be invoked, and invokes that servlet
  • the servlet receives a request and response object that it can use to (1) read cookies and URL parameters and (2) return content to the browser.

As you can see it is a long path that a request travels. This is why it is important that the webserver and servlet engine are optimized for performance.

Compared to other technologies, servlets are optimized in a simple but effective way. The essence of it is, that a servlet instance is kept alive by the servlet engine, and is managed in a pool of servlet instances. This can for example mean that a "ShoppingCartServlet" would during run-time result in 10 instances of ShoppingCartServlet in the servlet engine, which can handle requests concurrently.
Competing technologies often re-instantiate processes for each request, and do not do any process pooling, thus creating a lot of CPU and memory overhead.

The disadvantage of servlets is that the developer needs to embed HTML code in the Java code when using it without any additions. Solution to this is to extract the HTML from the Java code and put it in separate files, but that automatically leads us to the following solution...

JSP - Java Server Pages

JSP is a Java standard backed by all major software vendors (except Microsoft), and is part of the Java Enterprise Edition.

JSPs are HTML templates that can be treated as if it was the HTML page itself. However it can handle more tags, that are processed on the webserver-side and never make it to the browser. There are two kinds of server-side tags:

  • Java-code tags - denoted by "<%" and "%>", everything between those tags is compiled and executed as Java code, which allows for Java logic embedded in the HTML pages. This is especially useful for controlling the flow in an application.
  • tag-libary tags - denoted by "<libname:tagname", it allows the JSP pages to invoke some java logic that is defined in a tag library elsewhere. The advantage of this is re-use of the same logic across JSP pages. Of course, the same result can be achieved by invoking the same Java classes using plain Java-code tags.

The trick of JSP pages is, that the entire JSP page is taken by the JSP engine, converted into one servlet with all HTML in the Java code again, and then compiled once. So every JSP page is essentially a servlet!

To allow for run-time changes, the JSP standard enforces that a JSP page must be re-compiled when it changes. This has the effect that all JSP engines now automatically re-compile JSP pages if needed, and contain complex functionality for reloading the new JSP page servlet!

The downside of JSP pages is that it invites people to embed a lot of Java code inside the HTML page. It is a much better approach to separate the presentation (HTML) from the business logic (Java code).

But when used wisely, JSP is a flexible way to quickly develop HTML applications using Java!

Conclusion

Servlets allow you to develop server-side Java processes that can handle http requests and return dynamically generated webpage content.

JSP pages are useful for separating the presentation from the Java code, but have to be used wisely to reach that goal.