Chapter 26. Servlets and JSP

  • Overview of Servlets and JSP

  • Why Use Servlets?

  • Releases and Versions

  • Installing the Tomcat Software

  • Running the Example Servlets

  • Ports and Protocols

  • The HTML to Invoke a Servlet

  • A Servlet and Its Request/Response

  • Servlet Request

  • Response to a Servlet Request

  • Writing Your Own Servlet

  • Servlet Operating Cycle and Threading

  • Java Server Pages

  • Java Beans in Servlets and JSP

  • Last Words on JSP, Beans, and Tag Libraries

  • Further Reading

  • Exercises

  • Some Light Relief—Using Java to Stuff an Online Poll

This chapter describes two key Java technologies that are used in server-side Java: servlets and JSP.

Overview of Servlets and JSP

The first of these technologies, servlets, provides a way for a browser to cause a program to run on the server. The server calculates something, possibly accesses a database, and sends HTML output back to the browser on the client. In a nutshell, that is the architecture of all web-based B2C (business-to-consumer) e-commerce systems, from Amazon.com to the Zdnet.com online store. These are applications where the end-user interface is provided by a web browser, and the back-end logic runs on a server.

Servlets for creating web pages with the latest data

An ordinary page of HTML is static. Each time the server sends it out, it sends the exact same bytes. The only way it changes is if someone updates the HTML file with an editor. But many kinds of information change dynamically: stock prices, the weather, seats available on a flight, amount of inventory on hand, account balance, contents of an online shopping cart, and so on. Servlets and JSP are a great way to get this dynamic information into a web page. The pages that the user sees are calculated by general-purpose programs that can reference and update databases as part of serving the request.

Servlets are the most popular way for a browser to get a dynamic web page from a program on the server. The old way used an interface called CGI, and your scripts would be written in Perl or Visual Basic or some other language. With servlets, your code is written in Java. There is a size/complexity tradeoff here: small scripts (less than a couple of pages) can be written at the drop of a hat and are well suited to Perl or PHP.

The larger and more complicated your code, the more you will benefit from using a Java servlet. If you need to access a database, you can use Java's JDBC library. If you need threading or network libraries, Java has them. If your code needs to run with permissions-based access control, Java supports this. Instead of a mixture of different tools and libraries, you can write the whole thing in one consistent language. Perhaps the biggest advantage is that servlets free you from being tied to one kind of hardware or one software vendor. Because of Java's portability, you can easily move to more capable hardware as your processing needs increase. Your servlet code can be run by any servlet server. It's easy to add new capacity and leave the software unchanged.

JavaServer Pages for embedding dynamic web page content

The second technology we will visit in this chapter is called “ava Server Pages,” modelled after the ColdFusion technology invented by the Allaire company of Boston, Massachusetts. Allaire now makes a Java servlet and JSP execution environment known as JRun that can be plugged into any web server.

Allaire's ColdFusion product was copied by Microsoft and given the name ASP—“Active Server Pages.” ColdFusion, ASP, and JSP are all ways of mixing (“embedding”) programming statements, scripts, and components in web pages on the server side. When the server gets a request for the page, it executes the embedded programming statements, and sends to the browser the ordinary HTML plus the HTML dynamically generated by executing the embedded code. If you are familiar with ASP, you can pick up JSP in a few minutes. To put it another way, JSP looks a lot like fragments of Java code, but embedded in an HTML page.

For small programs, JSP is simpler than a servlet. Servlets require the programmer to write Java code to output all the HTML (the fixed part and the dynamically varying part) along with all the headers that a web server adds automatically. JSP allows a programmer to write down the fixed HTML content. Then you write Java code fragments in the same web page. When a browser browses the page, the server executes the Java fragments to create the dynamically varying HTML. Then the server sends all the HTML over to the browser. The whole page looks like static content to the browser. This approach allows the server to send the full HTTP headers, including one that tells the browser the page length.

JSP is (in theory) a good way to separate the dynamic and static parts of your web page. In practice, it takes considerable discipline to prevent your JSP programs from becoming hard to maintain. So people have invented additional frameworks to help with this; the most popular is Struts from the Apache group at http://jakarta.apache.org/struts. It's an open source framework with a free download.

JSP doesn't have any extra capabilities compared with servlets, and indeed JSP is usually implemented in terms of servlets. Both servlets and JSP are big topics, covered at book length elsewhere. In this chapter we map out the territory and show working examples of each. The goals are to show you how to write basic servlets, and to cover the key points of server-side Java. This chapter can be worked through in an afternoon, and gives you enough information to know what you're talking about on an e-commerce project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.18.141