Java Servlets and Java Server Pages

Written by Larry Gray on . Posted in Servlets / JavaServerPages

Java Servlets and Java Server Pages

Well first maybe I should explain a bit about the first actual web applications called CGI's. CGI(Common Gateway Interface) is a protocol or language for formatting request and responses from an http server to and http client. On this web site the server is the Java web server Apache Tomcat 5.5. A client would be any web browser typically. Initially a web server would send a request in CGI format to standard output on the server, at the same time calling a program on the server to startup. This program can be written in any programming language which can get standard input and output to standard output (practically any language, and even shell scripts). So the server sends the request to standard output which is read as standard input by the program. It then processes the request and prepares a response which is again posted to standard output, the server then reads this back in as standard input. The server then sends this information back to the client as a response.

This was a pretty good thing in the dawn of the internet. However staring up a system process for each request used a lot of system overhead, ram, processor and such. This was especially true when the request begin to come in in large numbers at a time. A server could easily be bogged down with request it couldn't answer. It could even crash. However we have a savior, you might recall from the article on Desktop Applications and GUI 'multi threading'. Developers were already writing these servers on machines that could handle this multi threading. So they worked on a way to send this client request to a thread within the same process(the server itself) to be handled. In Java we call this thread a Servlet. This is far more efficient on system resources than starting up a whole system process (multitasking) for each client request. And threads may be pooled, meaning reused without shutting them down and restarting them each time saving even more precious ram and processor time. Since a servlet runs within the server process it is a mini-application. It is to the server what an "applet" is to the desktop application. The server is made in such a way that you merely drop your servlets into, this is even easier than plugging something in. I mean you simply build your Java sources for your Servlet classes, compile them and then locate them in a given folder structure. The Server automatically knows where to look for them based on good ole standards.

Also the Java Servlet API has some nice classes which encapsulate all that CGI messaging complexity. This api handles request data with one object and response data with another. It handles your http request types such as POST, GET, DELETE, etc. with a single method call. The API simplifies the whole mess so that you the programmer are free to work on your web apps logic.

As an example of running servlets look at the comment submission form on the arksoft home page. This is a servlet I wrote which displays the last 10 comments submitted. It also logs the comments in a text file on the server. And it sends me an email notifying me that a reader has submitted a comment, with the comment content and reply email address. A second servlet processes the captcha image spammer filter. Also the comment submission servlet rejects any comment with image links, or urls or html. I may eventually get this form at the bottom of each article but for now its just one form for the entire site.


Servlets are enough right? We can do anything we want with them now! Well yes but the ugliness of this was that source code becomes very ugly when its full of System.out.println(" ..........HTML.........HTML........"); We have mixed logic with content. It works. It works fairly well actually. Its just ugly. What is a JSP page? Its a text file named somename.jsp that contains (text only)(html)(text and or html and jsp tags)(jsp tags may contain Java source)

They came up with Java Server Pages to resolve this problem of ugliness and separate HTML from Java source. A jsp page is a text file that has a mix of HTML and JSP tags. This page is compiled by the server at runtime into you guested it, a Servlet and in this servlet the compiler builds all those System.out.println("...html..."); lines for you. When is it compiled? The first time a jsp page is accessed from a browser. This can take seconds to even a minute and may confound a visitor who thinks the page is not loading. Developers usually access the jsp pages themselves to compile them to save the visitor from having to wait. Speaking of the compiled servlet source, good news is that most of the time you never have to look at that ugliness. You can if you want too but there is usually no need unless a debugging problem arises. Java source code can also be included on the jsp page as well if needed though this is probably not best practice. Instead you try to focus on keeping your content in the jps pages and your application logic in supporting Servlets.

And can we somehow work JavaBeans into this scene? Yup, but I won't talk about it on this web site at this time except to say its called EJB Enterprise Java Beans and that requires an additional Server to run with Tomcat called JBoss. There are other 3rd party EJB containers as well. And in fact you could probably use the JavaBean design without an EJB container if you tried with some benefit.

That's not the end of the story with JSP, JSP has what is called Tag Libraries. To be honest I have yet to mess with them. JSP and Servlets and servers for that matter have one very important feature called 'Server Side Includes'. This means the output of one serlvet can be the input of another. It also means that multiple jsp pages and html pages can be combined into one html page for a single response. How is this helpful? Web page headers and footers are prime examples. Let say I have 100 web pages on a web site. Instead of editing 100 pages to update a header or footer I now only edit two files, one for the header and one for the footer. The index.jsp page for each one will look something like the following psuedo example.

JSP Include header.html tag Page content JSP Include footer.html tag

I use this technique on ever page on this web site for header, footer, bread crumb trail, horizontal menu, vertical menu, related links and recommended books. I sometimes use the same related links for sets of pages for example. Mastering the use of server side includes is a must for any web developer that will save countless hours of work or at least make work possible that otherwise would be put off or never done.