Flylib.com

Books Software

 
 
 

The Next 17 Chapters

I l @ ve RuBoard

The Next 17 Chapters

Okay, enough philosophy and political science. If you feel like you've been climbing up the roller coaster and are getting impatient for that first plunge, don't worry because the ground's about to fall out from under you.

In the next three chapters, we're going to provide a review of JSP and JDBC for those of you who have seen it, and a basic introduction for those who haven't. Then we're going to dive right in to our sample application and begin to apply the tools to a real-life problem.

First you will get your platform in place. Then you'll run a few tests to make sure that it works, and you'll get familiar with the building blocks that you'll be using. The building blocks are JSP, JDBC, and MySQL.

Next, you will go over the sample application using object-oriented design methodologies, including use cases and process flow diagrams. This ensures that by the time you actually get to the coding, you should already understand in your head what the site is supposed to look like.

Then you'll be ready to start coding. This book tends to lean toward a step-wise approach, getting one subsystem working and tested before moving forward to the next. This has the advantage of isolating problems to the most recently added code, and it also builds confidence by letting you see pieces of the site at work before the entire site is done.

You'll move through the site, starting with user login and registration and then moving to product display, purchase, shopping cart display, shipping and payment information capture, and finally checkout, fulfillment, and order history. Each section will be used to highlight different techniques and problem areas to watch out for. Most of these are culled from actual problems encountered during the development of real sites.

Even well-designed projects have a degree of trial and error, backtracking, and rethinking. This book attempts to capture as closely as possible the actual development process that you will go through while hammering out the sample application. When you've walked through the entire process, you should be well prepared to tackle your own JSP application, even if it's not a shopping-cart site.

I l @ ve RuBoard
I l @ ve RuBoard

Part I: JSP and JDBC

IN THIS PART
 

1 A JSP/JDBC Review

 

2 Java Beans and JSP

 

3 Using Servlet Functionality with JSP

I l @ ve RuBoard
I l @ ve RuBoard

Chapter 1. A JSP/JDBC Review

IN THIS CHAPTER

  • How Does JSP work

  • Running Tomcat

  • Finding the Java Sources

  • A Quick Look at SFBC and My SQL

  • Working with ResultSets

  • Using PreparedStatement

  • Inserting, Deleting, and Updating

  • Using Cursors

If you already are a JSP guru, consider this chapter a quick refresher. If you are new to JSP, this should provide enough of a grounding to get you through the remainder of the book.

I l @ ve RuBoard
I l @ ve RuBoard

How Does JSP Work

To begin, let's look at the world's simplest JSP page, our version of the infamous "Hello World" program (see Listing 1.1).

Listing 1.1 HelloWorld.jsp
<HTML>
 <HEAD><TITLE>Hello World!</TITLE></HEAD>
 <BODY>Hello World!</BODY>
</HTML>

Okay, I can hear the screams of protest already. "That isn't JSP ”that's plain old HTML. What are you trying to pull? I want my money back!"

It might look like innocent HTML, and if you put it into a .html file on a Web server, it would be delivered as straight HTML. But look what happens to this innocent little snippet in Listing 1.2 if you put it into a .jsp file instead.

Listing 1.2 HelloWorld as Java
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import org.apache.jasper.runtime.*;
public class _0002fjsp_0002fhello_0005fworld_jsp extends HttpJspBase {


    static {
    }
    public _0002fjsp_0002fhello_0005fworld_jsp( ) {
    }

    private static boolean _jspx_inited = false;

    public final void _jspx_init() throws org.apache.jasper.runtime.JspException {
    }

    public void _jspService(HttpServletRequest request, HttpServletResponse  response)
        throws java.io.IOException, ServletException {

        JspFactory _jspxFactory = null;
        PageContext pageContext = null;
        HttpSession session = null;
        ServletContext application = null;
        ServletConfig config = null;
        JspWriter out = null;
        Object page = this;
        String  _value = null;
        try {

            if (_jspx_inited == false) {
                synchronized (this) {
                    if (_jspx_inited == false) {
                        _jspx_init();
                        _jspx_inited = true;
                    }
                }
            }
            _jspxFactory = JspFactory.getDefaultFactory();
            response.setContentType("text/html;charset=ISO-8859-1");
            pageContext = _jspxFactory.getPageContext(this, request, response,
                      "", true, 8192, true);

            application = pageContext.getServletContext();
            config = pageContext.getServletConfig();
            session = pageContext.getSession();
            out = pageContext.getOut();

            // HTML // begin [file="/jsp/hello_world.jsp";from=(0,0);to=(4,0)]
                out.write("<HTML>\r\n <HEAD><TITLE>Hello World!</TITLE></HEAD>\r\n
graphics/ccc.gif
<BODY>Hello World!</BODY>\r\n</HTML>\r\n");

            // end

        }  catch (Throwable t) {
            if (out != null && out.getBufferSize() != 0)
                out.clearBuffer();
            if (pageContext != null) pageContext.handlePageException(t);
        }  finally {
            if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
        }
    }
}

Our simple HTML has been turned into Java code! However, if you look carefully , you'll see this line:

out.write("<HTML>\r\n <HEAD><TITLE>Hello World!</TITLE></HEAD>\r\n <BODY>Hello World!</
graphics/ccc.gif
BODY>\r\n</HTML>\r\n");

The HTML is still living happily, embedded as a write statement inside the Java.

This example is important because it demonstrates the most important thing to remember about JSP. Even though you might think at the time that you're writing HTML with Java code embedded, you're really always writing Java code with HTML embedded.

Of course, if you used JSP only to output static HTML content, it would be a huge waste of resources and performance. JSP is great because programmatic functionality can be embedded into HTML. For example, let's look at a very simple JSP page in Listing 1.3.

Listing 1.3 5loop.jsp
<HTML>
 <HEAD><TITLE>Hello World!</TITLE></HEAD>
 <BODY>
 <%
   int i = 0;
   while (i++ < 5) {
 %>
  Hello World! Loop #
  <% out.print(i); %>
  <br>
 <% }  %>

</BODY>
</HTML>

We've suddenly corrupted the pure HTML file by placing Java code in the middle. The Java is delimited by the <% and %> characters . Now turn your thinking around 180 °. We don't have an HTML file with a little Java embedded in it; we have a Java source with some HTML in it. Remember that all HTML in the source is going to turn into out.print statements in the intermediate .java file. So, when Java is placed in a JSP file, it is really escaping into the "real" world and out of the artificial HTML world. Just this once, we're going to look at a portion of the .java file in Listing 1.4 (leaving out most of the boilerplate from the "Hello World" example).

Listing 1.4 A Java Fragment of JSP Code
// HTML // begin [file="/jsp/5loop.jsp";from=(0,0);to=(3,1)]
                out.write("<HTML>\r\n <HEAD><TITLE>Hello World!</TITLE></HEAD>\r\n
<BODY>\r\n ");
            // end
            // begin [file="/jsp/5loop.jsp";from=(3,3);to=(6,1)]

                   int i = 0;
                   while (i++ < 5) {

            // end
            // HTML // begin [file="/jsp/5loop.jsp";from=(6,3);to=(8,2)]
                out.write(" \r\n  Hello World! Loop #\r\n  ");

            // end
            // begin [file="/jsp/5loop.jsp";from=(8,4);to=(8,19)]
                 out.print(i);
            // end
            // HTML // begin [file="/jsp/5loop.jsp";from=(8,21);to=(10,1)]
                out.write("\r\n  <br>\r\n ");

            // end
            // begin [file="/jsp/5loop.jsp";from=(10,3);to=(10,6)]
                 }
            // end
            // HTML // begin [file="/jsp/5loop.jsp";from=(10,8);to=(14,0)]
                out.write("\r\n\r\n</BODY>\r\n</HTML>\r\n");

As you can see, the Java code from the JSP file is included verbatim, while the HTML is put inside print statements. Although it might look strange to see HTML code wrapped inside a Java while loop in the .jsp file, it makes perfect sense when you see the resulting output file.

I l @ ve RuBoard