Previous |
Next |
As something of an aside it's worthwhile being aware that the World Wide Web was invented by an Englishman called Tim Berners Lee in the early 1990's. Note he did not invent “the Internet”, which is the underlying technology on top of which the World Wide Web. He invented the idea of hyper-linked text based web pages that could be navigated via a browser. The key to his invention was its simplicity and the fact that it was made a free standard. I had personal experience of the genius of his invention because at the start of the Web Microsoft thought they had an invention that would rival and beat the standard web technologies. Their product was based on Rich Text Formatting, an option still available in MS Word for saving documents that can be opened with other tools.
I spent about a week trying to generate a single page using the Microsoft technology and make it available via the internet. After a week of frustration and no results I tried to create a web page. Within a few hours I had generated a reasonable looking page including photos and had uploaded it to a web site that was visible to anyone with a web browser. The magic was the simplicity yet flexibility of the technology. Of course things have moved on enormously since then but still at its heart is standard text that can be manipulated with any editor and uploaded to a server. The World Wide Web is the single biggest change in information availability since the invention of printing around 400 years ago.
It has made more information available to more people at a lower cost than ever before in human history. The inventor of the Web did not have a huge budget or vast team of researchers. It was the result of building on existing technologies and of understanding how to simplify technologies so they could be use by ordinary human beings. This chapter assumes you have a general working knowledge of HTML but if you need a refresher you might like to consider “Head First HTML” from O'Reilly, which in addition to being an excellent text book includes a charming photo of me (I kid you not).
In the beginning the world wide web was nothing but static web pages. This was because the main driving force behind Tim Berners Lee's invention was to make technical documents available to scientists. This might sound a little dull by contrast with the excitement that is available today but at the time it was a wonderful thing. Web site developers quickly realised they could make things more interesting by having interactive web pages where code ran on the server and the results were dynamically controlled by the user. One of the first widely used languages for interactive web sites was Perl. I had the dubious fortune to spend about six months of my life programming web sites in Perl and it left me with a feeling that there mus be a better way. Perl is a language designed for processing text and it has no in-built understanding of HTML or the basic HTTP protocol that underlines the web.
As a reaction to this various other languages were developed and modified to suit web programming. Some of these are Microsofts ASP, PHP and Servlets/JSP. JSP was designed after ASP and was heavily influenced by the Microsoft technology. Despite working with Servlets and JSP technology since the early 1990's I am not convinced it is the ultimate technology for programming web applications. It seems to introduce a considerable amount of new terminology for only a small practical benefit and it has been a moving target since its first release. Every few years it gets new features that attempt to improve but seem mainly to add to the learning burden.
Since it's orignal release JSP has had EL, JSTL, and JSF added to the original design, each of which attempt to allow the creation of cleaner pages that separate design and procedural coding. Although each new technology offers practical benefits they all seem move the programmer further away from the underlying web page.
The reason that Servlets and JSP go together is that a JSP (Java Server Pages) are actually converted into servlets “behind the scenes” and so they are very closely related. Servlets were invented before JSP and are a way of writing a Java program that understands the HTTP protocol. It may be better than writing in a language like Perl but it suffers from the problem that a servlet is a program and you only get to know what the output is like when it actually runs. The HTML takes the form of a sequence of statements that output the HTML tags.
JSP by contrast is more like an HTML page with special markers allowing Java statements to be inserted. As a result it is far easier to visualise what the output will look like than with Servlets. It is tempting to assume that JSP entirely replaces Servlets, but there are circumstances where Servlets are useful and it is very common for a large Java web application to consists of a large number of JSP's and just a few Servlets for the more “programmy” tasks. But enough of talking about these technologies now on to how you can actually create some code using them. This chapter is designed to be somewhat of a “bluffers guide” to Servlets and JSP in that it will give you a flavour of the most important aspects but to do “real world programming” you will need additional resources.
Understanding where Servlets/JSP fit into the world of Java requires understanding some slightly confusing and overlapping terminology. The Servlets/JSP technology are one of the layers of the J2EE software stack. J2EE stands for Java 2 Extended Edition. If you take a look at Suns page for this architecture you will see that it includes a whole raft of technologies.
http://java.sun.com/javaee/technologies/
Perhaps the three main foundations of J2EE are “plain old Java”, e.g. swing applications, Servlets and JSPs covered by this book and Enterprise Java Beans (EJB), which are typically used by major corporations where in the past they might have used mainframes.
To get some perspective of how these technologies are used “in the real world” it is worth understanding that the major Web application tool vendors such as IBM WebSphere and BEA WebLogic are all based on supporting the J2EE technologies and as such they are compliant JSP/Servlet containers. So you it should be possible to run all of the sample code in this section within WebSphere or WebLogic.
The traditional way of writing your first Servlet or JSP is to download the Tomcat Web container, configure it, compile your code and then copy the appropriate files to the correct directories and run Tomcat. This is neither convenient nor obvious and fortunately the free NetBeans system offers a far easier and satisfying way of getting started. The most minimal downloadable version of NetBeans comes with a pre-installed and configured version of Tomcat and Tomcat is the reference implementation of the servlets and JSP technologies. The idea of a “reference implementation” is that it is the standard that should work according to the specification document, thus every other implementation should work in the same way and if it doesn't then the fault is with the other implementation.
The NetBeans implementation means you can create, write, launch, debug and test Servlets and JSP without ever going out of the NetBeans IDE (Integrated Development Environment). It also offers all the features you would expect from any IDE such as color syntax highlighting, code reformatting and it is portable across operating systems because it is written entirely in Java itself. Another alternative is the Eclipse IDE, but I have always found NetBeans to be an easier “out of the box” experience requiring no additional downloads.
HTTP is the underlying protocol that is the basis for the World Wide Web. The word protocol comes from the world of diplomacy and means a set of rules that must be obeyed to aid communication. Thus years ago when ambassadors met from different cultures and speaking different languages, the diplomatic protocols would allow them to communicate accurately and without causing offence. If you have never noticed before try typing in a URL into a web browser and it will insert the characters http:// just before the domain name. Thus if I type in www.google.com into my FireFox browser it automatically modifies it to read http://www.google.com.
HTTP is a simple protocol based around the idea of a request, e.g. “please give me the home page for www.google.com” and a response which might be the equivalent of here is the home page for www.google.com“.The reason it is good to know about HTTP is that much of the task of servlets is to wrap up the details of requests and responses and make it easy to write programs that manipulate the details. Because Java Servlet Pages (JSP's) are converted to Servlets its good to understand why and what the limiting factors are.
Other sources
HTTP Made real easy by James Marshal
http://www.jmarshall.com/easy/http/
HTTP according to Wikipedia
http://en.wikipedia.org/wiki/Http
HttpServlet API docs from Sun
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServlet.html
Previous |
Next |