JSP, J2EE, and the Role of the Servlet
THIS FIRST CHAPTER WILL INTRODUCE YOU to the big picture surrounding JSP technology. JSP technology is part of a bigger technology often referred to as J2EE, and in this chapter, you will see where JSP fits into the panorama that is J2EE. After you have seen where JSP fits into the J2EE puzzle, you will be introduced to the core technologies that underline JSP, namely servlets.You will see what servlets are, and why your understanding of them is critical if you are to become a proficient JSP developer.
The chapter will then finish with an introduction to the basic syntax of JSPs themselves. This chapter is foundational to the rest of the book. The Progression of Java and Web Applications
Java first made its mark on the IT world in the mid-1990s about a year after the Internet started making an impression. At the time, I was working as a Web designer for a software agency and started learning Java so I could write Java applets to improve the pages I was creating.
Java applets are Java programs that run in Web pages using a Java virtual machine (JVM) within the browser.
Java’s primary use back then was to make Web pages more interesting with the use of Java applets.Typical uses included image manipulation applets, scrolling text, and (for those of us who love tennis) the applet that gives us the Wimbledon scores. Applets ran on the client machine and had no reliance on the server. If you wanted server-side processing, you used Common Gateway Interface (CGI) programs, which were often written in the Perl programming language.You could write CGI programs in Java, but, frankly, it wasn’t worth the trouble because a wrapper also had to be written to invoke the JVM that would then invoke your CGI program.
CGI had performance issues associated with it, however. Every request that came in to the server invoked the CGI application as a separate process in its own address space; this process serviced the request and then shut down. Large numbers of users resulted in many instances of CGI programs running concurrently, which can adversely affect the performance of your CGI program.This is illustrated in the left part of Figure.There was then a development by a company called Open Market, which enabled each CGI program to service multiple clients. It does still require a separate address space for each CGI program however.They still could not all operate within one running application.
Netscape, Apache, and Microsoft all published APIs that enable you to write serverside Web applications. For Microsoft and Netscape, these APIs are ISAPI and NSAPI, respectively.The Apache API is referred to as the Apache Web Server API.These APIs enable the developer to write much more efficient code.These server-side applications are multithreaded—meaning that the application could be permanently running, servicing multiple requests without needing multiple instances of the application running. Figure contrasts CGI with the NSAPI/ISAPI model. However, the main drawback of these APIs is that they are tied into one specific server product.
ISAPI/NSAPI. Java programmers did however want to harness the platform independence of the Java programming language on the server, just as they had on the client with Java applets.
Many platforms are used to host Web sites.To see the spread of different Web servers currently in use visit
The idea that you would be able to write server-side applications that could run on any server is very appealing.
Eventually, a mechanism was developed whereby Java could be used to write serverside programs without using CGI.The servlet was born!
The Java servlet is a server-side Java program that can be used to process a request and send a response.The Java servlet is multithreaded, and many servlets can be running within one JVM, processing many requests at any one time.The top half of Figure demonstrates the basic servlet model.
Although the top half of Figure demonstrates how multiple servlets can run within a JVM, and then how they can each service multiple clients, it is somewhat simplistic. Do the servlets run within the Web server, or is the Web server separate? Most Web server software cannot run servlets without an additional plug-in of some kind. As a result, various vendors came up with servlet containers that could be plugged into your existing Web server. For example, the Jakarta project came up with Tomcat for the Apache Web server, whereas Allaire came up with JRun for IIS.
Things have progressed a great deal since servlets were first developed.There were a number of significant initiatives going on at Sun, however, other than the servlet API.A new Java platform was emerging.When Java 2 was launched, it came out in three flavors:
the micro edition for embedded devices, the standard edition for regular desktop type applications, and the enterprise edition for enterprise applications.The servlet API is in fact a part of the enterprise edition of Java 2 (or the J2EE as it is sometimes referred to).