So What Is JSP All About?

I l @ ve RuBoard

If you meet the requirements mentioned, you should already have a pretty good idea what the answer to this question is. JSP is all about doing highly object-oriented Web sites that can leverage all the best practices of modern software engineering. These practices include things such as SQL databases and UML-based design.

This isn't to say that JSP is a cure-all and that using it will automatically make your Web site a paragon of engineering art. It's just as possible to design bad Web sites in JSP as with any other technology. That's why, as you go through the text, you will see how to incorporate the best practices and how to avoid the pitfalls of convenience when projects get stressful.

JSP itself is an evolutionary step along the path that started with the first static Web servers, moved through CGI-enabled servers, and finally the first generation of script-enabled servers. JSP is less a Web server with a Java component than it is a Java engine that understands the Web.

JSP grew out of Java servlets. Servlets allow the developer to handle the incoming Web requests using a Java program that has access to all the normal information that a Common Gateway Interface (CGI) program would. In addition, the servlet has access to session-persistent objects. These are Java objects that are associated with a specific user session and can be used to store state between requests .

Servlet programming was a major step forward in allowing developers to write well-structured modular Web applications using an object-oriented language. It also solved the problem of state persistence, allowing more information to reside on the server during a transaction and less to have to pass back and forth between the user and the server.

Servlets still suffered from one major problem. Because they eventually need to spit out HTML, the HTML coding had to be embedded in the servlet code. This led to code fragments like the one shown here:

 // Output the HTML Header Out.println("<HTML>\n<HEAD>\n<TITLE>Thank you for Registering</TITLE></HEAD>\n"); Out.println("<IMG SRC=\"thanks.jpg\" WIDTH=200 HEIGHT=100 ALIGN=\"LEFT\">"); 

This kind of embedding gets very old very fast when you have to code a lot of pages. In addition, having to escape all of the quotation marks can lead to a lot of confusing and hard-to-find errors if you leave out a backslash.

Eventually, a still-better idea emerged. Suppose that you could combine the best of static HTML pages and with the interactive capabilities of servlets. The result was JavaServer Pages (on the Microsoft side, the result was Active Server Pages). As Figure I.1 shows, JSP is a complicated beast . In the next chapter, you'll walk through this flow in detail, but for the moment, here are the major steps:

  1. A request comes in from a browser using the normal HTTP request format.

  2. The Web server hands off the request to JSP. JSP looks at the filename and finds the appropriate JSP file.

  3. The .jsp file is converted into a .java file, containing Java code that will create a class whose name is derived from the .jsp filename.

  4. JSP then compiles the .java file using javac to produce a .class file. Note that the two previous steps are skipped if a .class file already exists and is newer than the .jsp file.

  5. An instance of the newly created class is instantiated and sent the _jspService message.

  6. The new instance looks to see if there is already an instance of the stuff.User object called user existing in the session object space for the currently connected user. If not, one is instantiated.

  7. As part of servicing stuff.jsp, the user instance is called with the getUserName() method.

  8. If the JSP processing requires access to information in a database, it uses JDBC to make the connection and handle the SQL requests.

Figure I.1. Looking at the JSP processing flow.

graphics/infig01.gif

That is the way that JSP handles requests.

As you can see, a tremendous amount of power is available in the JSP world. Developers are free to write Web pages that look mostly like HTML, except where callouts to Java are required. But, at the same time, they are free to develop fully fleshed-out object-oriented applications using all the features that Java can bring to bear. They also get all the benefits of servlets, including session persistence.

HOW IS JSP DIFFERENT FROM CGI?

Several differences exist, some superficial and some more important, between how CGI and JSP look at the world.

CGI is a transient beast. Every transaction with the user is a new day, started fresh with a blank slate. If you want to carry state forward from one click to the next, it needs to be carried along with the page, either as embedded hidden form tags, as some kind of a cookie placed in the query portion of the URL (leading to great URLs such as http://www.mysite.com/view.cgi?uid=234235435not a real Web site), or as a session cookie delivered by the browser. And, any state that you do maintain resides externally, in either files or database tables, and must be reloaded on each request.

CGI also makes you turn the page inside out, putting your HTML inside print statements in whatever language you choose to write.

By comparison, JSP automatically carries along whatever state you want to preserve from request to request without any effort on your part. It lets you, as the developer, concentrate on business logic rather than figuring out the context of a request. It also lets you persist your state in object-oriented form, and it even lets two user sessions share objects between them.

The JSP page looks like HTML with some additional Java stuck in where programmatic output is needed, and it can be maintained by HTML designers, provided that they exercise some basic caution. It also allows the business logic to be separated from the presentation logic, which allows you to place the business rules in a well-defined set of classes away from the Web pages themselves .

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net