Java Server Pages and JDBC Tutorial
There are many options available when implementing object-oriented designs. We have a range of programming languages we can use (e.g. Java, Visual Basic, Delphi, C++, C#, Cobol, …); a range of data management strategies (relational database (RDBMS), object-oriented database (OODBMS), files (text or binary), XML database, persistent objects); various “middleware” inter-connectivity (sockets, RMI, CORBA, DCOM, XML, HTTP, WAP, SOAP); and various kinds of user interface approaches (HTML-based, command-line, desktop GUI, virtual environment, mobile device).
In this tutorial we’ll learn how to built thin-client applications (HTML for the most part, though can be used for e.g. WML too) using Java Server Pages (JSPs). JSPs provide a server-side “scripting” language which contains (mostly static) presentation mark-up, with embedded dynamic processing logic expressed in the Java programming language. We’ll see how JSPs provide HTML-based user interfaces that are supplied to a browser running on a user’s machine via the HTTP protocol. We will use the Java DataBase Connectivity (JDBC) API to obtain and update data stored in a relational database (I’ll use MS Access in this tutorial, but its easy to use SQL Server, Oracle, Interbase etc too).
I’ve chosen JSPs as our implementation platform for several reasons:
• They provide a “nice” distributed object-based platform form learning basic DIS implementation strategies
• Many “nasty” DIS issues are handled for you (threading, complexities of synchronisation, distributed object creation etc)
• You can produce some nifty, platform-independent, web-based user interfaces quickly and (relatively) easily
• JSPs are straightforward to use, even with limited Java programming background. We’ll use basic constructs and not get into any particularly complex stuff.
I use JBuilder4 in this tutorial to implement the JSP examples. I also briefly explain how to run them using JDK1.3 and the Tomcat JSP server. You could use JBuilder3 to compile the JavaBeans code in the examples below, but will need to use Tomcat to run the JSPs. You can find another basic tutorial, including information on downloading JDK1.3 and Tomcat at:
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/
The example code used in this tutorial is available off the CS392 class share:
http://kula.usp.ac.fj/class-shares/CS392
Download the video.ZIP file, and unzip it to a directory. If using Jbuilder 4 or 5, you can browse the directory structure and open projects. If using JDK1.3/Tomcat, you can use the DOS or LINUX command line to browse the directories, compile programs and deploy JSPs and class files (see the .bat files). To run these, you need to manually start Tomcat and an HTTP browser (e.g. IE5 or Netscape), then go to the URL http://localhost:8080/examples/jsp/video2/ to run them (see examples below).
How JSPs Work
JSPs are server-side files that contain a combination of (usually) HTML mark-ups plus embedded Java code. The mark-ups are rendered in the browser to provide the usual HTML-based user interfaces, while the Java code provides dynamic data look-up and update, as well as data processing logic. Often, Java objects called “JavaBeans” are used with JSPs to encapsulate data processing and user interface behaviour (more on these later…)
When a browser requests a JSP URL, the HTTP server the URL is requested from performs the following actions:
1. a HTTP GET or POST command is sent from the browser to the HTTP server hosting the JSP
2. the HTTP server determines a JSP has been requested and, if it’s the first request or the JSP source has been changed, compiles the JSP to a Servlet (another Java server-side technology, which is basically a HTTP server plug-in, just like a Java Applet is a browser (client) plug-in)
3. Once compiled, the HTTP server loads and runs the JSP’s servlet (a Java object)
4. The Servlet accesses databases/remote objects, does data formatting for output etc etc
5. HTML is returned to the requesting browser, where it is formated
JSP Basics
The following is a cut-down version of the customer_maintenance.jsp file. You can find this in video/customer maintenance JSP/src/video2 (appologies for the weird path – a Jbuilder4 peculiarity…). Open the project video/customer maintenance JSP/CustomerMaintenanceJSP.jpr in Jbuilder4, or use your favourite editor for JDK1.3. I have just included some basic JSP elements in this version here:
Some of the key parts of a JSP include:
• HTML content. This is “static” i.e. doesn’t change. The concept of a JSP is that it contains presentation mark-up (typically HTTP) and in this is embedded dynamic Java code
• Page directives. These tell the JSP server to do certain things with/for your JSPs. Common ones include:
o Importing Java APIs, JavaBean definitions e.g. <%@ page import=”java.util.*” %> is just like putting import java.util.*; at the top of a .java file.
o
• JavaBean identification. JavaBeans are very simple software components – Java objects – often used by JSPs to store data, encapsulate form processing and encapsulate database accesses.
• Bean property access/update e.g.
• Java variables e.g. <%= BeanName.getPropertyName() %> gets the value of the PropertName property from a JavaBean object called BeanName.
• “Scriptlets”, basically arbitary Java code in the JSP. This is enclosed in <% and %> tags.
Running a JSP
The example programs use an MS Access database called “video.mdb” (in the Video.ZIP file). To run them you first need to set up an ODBC DSN name called “video” to point to this. To do this, go:
• Select Start||Settings|Control Panel on the Windows desktop
• Double-click ODBC Data Sources
• Click on Add
• Select MS Acess Drive, then Finish
• Set Data Source Name to video
• Click Select and choose the video.mdb file (you may have to wander around directories to find it with the Windows 3.1 file browser it has!)
Jbuilder4
Open the CustomerMaintenanceJSP.jpr project file (under video\customer maintenance JSP\). Your Jbuilder screen should look something like the following:
Select Project|Rebuild Project menu item. If you get errors, right-click on CustomerMaintenanceJSP.jpr and select Properties. Check the four properties (for Ouput, Backup, Working and Source) make sense for where you have unzipped Video.ZIP to. Change the start of them if necessary (mine are installed under C:\My Documents\java\video – this is unlikely to be the same place as you use!).
Run the program by selecting Run|Run Project. If you get an error, or nothing happens, right-click the CUstomerMaintenanceJSP.jpr project icon and check the Properties|Run tab. Check under the JSP/Servlet tab the file customer_maintenance.jsp appears (click the “…” button to set/reset it…)
All being well, your screen should now look like:
You can enter e.g. 1 for ID, click Find and, all being well, you should get some stuff displayed!
If you change a JavaBean’s source (.java) file, you need to stop the HTTP server and re-run it (click on the red box at the bottom left of the screen to stop it, then select Run|Run Project again).
JDK1.3/Tomcat
Download the tomcat installation ZIP file and unzip this to C:\tomcat. Follow the instructions with it to start it up:
1. Start a DOS command window
2. CD to C:\Tomcat
3. Set JAVA_HOME to where you have your JDK1.3 installation
4. Type bin\startup
5. A new DOS window should open with Tomcat http server messages displayed e.g.:
To check its running, open IE5 or Netscape and go to http://localhost:8080/. A pretty picture and text should appear. Note that Jbuilder4 starts a Tomcat session up itself (you’ll see the start-up messages for this appear whenever you run a JSP in Jbuilder4)…
You need to put files in two places for Tomcat to use them:
C:\Tomcat\webapps\jsp\examples\jsp\<
C:\Tomcat\webapps\jsp\examples\Web-inf\classes\<
Change directory to video/customer maintenance JSP/. Type “compile” to have the required JavaBean files compiled for you (these live in src/video2). Type “run” to have files copied to the above directories.
To run the JSP, point your browser to: http://localhost:8080/examples/jsp/video2/
Note that Tomcat will automatically re-compile a changed JSP file. However, if you change and re-compile a JavaBean source (.java) file, you need to stop Tomcat (select its DOS window and go CTRL-C) and re-start it, as it can’t dynamically reload the changed Bean code.
What happens when you run a JSP? The following things occur:
1. Point browser to URL e.g. http://localhost:8080/examples/jsp/video2/customer_maintenance.jsp
2. HTTP GET request is sent by browser to the http server (localhost:8080 means the Tomcat server running on your machine)
3. The HTTP server asks its JSP engine to compile the requested JSP to a Java servlet (only if the JSP source has changed since last accessed by the HTTP server, or it’s the first time the JSP was requested)
4. Once compiled, the servlet is loaded by http server’s servlet engine and run
5. The servlet instantiates any JavaBeans it requires (i.e. Java objects)
6. The servlet returns HTML data to the requesting browser via http server
Its important to remember that JSPs are compiled on-the-fly to servlets. This can explain some of the weird error messages you get when starting a JSP (in Jbuilder4) or going to a JSP page in IE5/Netscape. For example, if I introduce a syntax error into the scriptlet code of a JSP e.g. add:
<% this is bad Java code; %>
I get the following result in Jbuilder4 (and same result in IE5/Netscape window if going to this JSP):
This can make debugging JSPs a little tricky. That’s why later on we’ll see that putting most of our Java code into JavaBeans associated with the JSP is good – its both easier to manage the code, reuse it and to debug it!
JavaBeans and JSPs
Things called “JavaBeans” are used to hold data in JSPs, particular when processing information entered by the user on a form. They are also often used to provide processing logic for the JSP. Basically a JavaBean is a very simple “component” object – one that has getXXX() and setXXX() methods for every “property” of the component. A Java system can find out, dynamically at run-time, all the properties of a JavaBean and get/set their values. So, for example, CustomerData is a JavaBean I have developed which holds information about a customer. It has getID()/setID(), getName()/setName() etc functions to allow the JSP to read/write these property values. Here is the OOD diagram for this JavaBean. Note it inherits common properties/functions from a PersonData class (so we can define e.g. Staff, Manager etc JavaBeans that are also Person specialisations):
Some of the Java code implementing this Bean (in video/Shared/video2/CustomerData.java) is shown on the right. JavaBean objects can also have any other Java function – we’ll see this used later for form processing (i.e. a processRequest() function).
Form Processing with JSPs
Data display (output) with JSPs is easy – we have HTML markups+Java code. How do we get input from the user???
With HTML-based user interfaces, the normal way is to use HTML forms. These are enclosed in tags, and typically include the usual HTML tags (,
,
,
,
- ,
, etc) plus marked-up text, along with tags for data entry (text fields, radio and check-box groups, list boxes, etc) and actions (buttons). When we click a button in an HTML form, the contents of the form are sent to an HTTP server using an HTTP POST request. The JSP can read the entered data which is stored by the HTTP server as named POST command parameters, using either the Request data structure provided by the HTTP server, or more commonly from one or more JavaBean objects the POST data is stored into.
A (cut-down) version of the customer maintenance form is shown below:
Key elements in a JSP form include:
• HTML
Tidak ada komentar:
Posting Komentar