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.
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.
Websites can present webpages to users. There are two uses for Java in these websites:
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.
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:
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 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:
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!