1.25k likes | 1.45k Views
Developing Web Applications. Using Ada, JGNAT, JavaServer Pages, and JDBC to Build Web Applications Terry Westley SIGAda 2001. What Will You Learn?. Overview of dynamic web technologies Create HTML pages that use JavaServer Pages (JSP) technology to access and display dynamic web content
E N D
Developing Web Applications Using Ada, JGNAT, JavaServer Pages,and JDBC to Build Web Applications Terry Westley SIGAda 2001
What Will You Learn? • Overview of dynamic web technologies • Create HTML pages that use JavaServer Pages (JSP) technology to access and display dynamic web content • Write Java Beans and Java servlets in Ada • Use JDBC in Ada to create, maintain, and query a relational database
Tutorial Prerequisites • We assume you are familiar with: • Creating static HTML pages • Programming in Ada • But, not: • Programming in Java • SQL • JavaServer Pages
References • DuBois, Paul, MySQL, New Riders, Indianapolis, IN, 2000. • Fields, Duane K., Kolb, Mark A., Web Development with JavaServer Pages, Manning Publications Co., Greenwich, CT, 2000. • Goodman, Danny, Dynamic HTML, The Definitive Reference, O'Reilly and Associates, Sebastopol, CA, 1998. • Horstmann, Cay S., Cornell, Gary, Core Java 1.1, Volume I -- Fundamentals, Prentice Hall, Upper Saddle River, NJ, 1997. • Lemay, Laura, Perkins, Charles L., Morrison, Michael, Teach Yourself Java in 21 Days, Professional Reference Edition, Sams Publishing, Indianapolis, IN, 1996.
Table of Contents • Dynamic Web Technologies • Required Components • Introduction to JSP • Introduction to Java Beans • Introduction to JDBC • Building a Web Application
What is a Dynamic Web? What do I mean by "dynamic?" • User interaction affects the HTML page • Data from a server affects the HTML page
Dynamic Web Technologies • Static HTML pages • Common Gateway Interface (CGI) • JavaScript • Java applets • Dynamic HTML (DHTML) • Java servlets • Template systems
Static HTML • Browser requests an HTML document • Server finds the file and sends it to browser via HTTP • Server sends image files separately • Static HTML example
Browser Server Static HTML Page Request Page Image
Common Gateway Interface (CGI) • Server-side processing • Usually used in conjunction with HTML forms • HTTP request is passed to a separate program which processes it and generates a response • David Wheeler's AdaCGI: • http://www.dwheeler.com/ • CGI example
Browser Server CGI Program Common Gateway Interface (CGI) Page Request Spawn HTTP Request Page Page
JavaScript • Client-side processing (in browser) • First introduced by Netscape • Object-based language with access to Document Object Model (DOM) • Small application whose source code is embedded in an HTML page and interpreted by the browser • JavaScript example
Browser Server JavaScript Page Request Page Image
Java Applets • Client-side processing • Small applications that server sends to browser as a separate file • Browser invokes the Java Virtual Machine to execute the applet • Applet may or may not have a user interface • Applet is discarded when user switches to another page • Java Applet example
Browser Server Java VirtualMachine Java Applets Page Request Page Applet Applet User Interface
Dynamic HTML (DHTML) • Client-side processing • DHTML is a combination of World Wide Web Consortium (W3C) standards for: • HTML 4.0 • JavaScript • Document Object Model (DOM) • Cascading Style Sheets (CSS)
Browser Server Dynamic HTML (DHTML) Page Request Page Image
Java Servlets • Server-side processing • Small Java applications that server executes • Similar to CGI except does not require spawning a new process • Servlet processes the HTTP request and generates the response • Java servlet example
Browser Server Java ServletContainer Java Servlets Page Request HTTP Request SpawnServletThread Page Page
Template Systems • Server-side processing • Embedded in HTML file: • Special tags request dynamic content • Scripts generate more HTML • Server processes the HTML file, filling in the dynamic content and executing the scripts, then passing the page to the browser
Browser Server TemplateProcessor Template Systems Page Request HTTP Request Page Page
Template Systems • ColdFusion • Microsoft Active Server Pages (ASP) • Server Side JavaScript • PHP • WebMacro • Apache Velocity • JavaServer Pages
Why JavaServer Pages? • Platform-independent technology • Java-based technology • Full object-oriented programming model • Access to Java platform, including all Java APIs • Improved performance over CGI and scripting languages
Why JavaServer Pages (2) • A programming model which provides for • Separation of presentation and implementation • Reusable component model: Java Beans • Tag extension mechanism • Last, but not least: • Use Ada to code Java Beans and Java servlets • Complete set of free tools
Table of Contents • Dynamic Web Technologies • Required Components • Introduction to JSP • Introduction to Java Beans • Introduction to JDBC • Building a Web Application
Required Components • Java Software Development Kit (SDK) • Ada to JVM compiler • JGNAT • Aonix ObjectAda • Relational database – MySQL • JDBC driver • JSP-enabled web server – Jakarta Tomcat
Java SDK • Get “Java 2 Standard Edition” • Windows version: j2sdk1_3_0-win.exe • http://java.sun.com • Install on • server • development computer
Test Java SDK Installation • Create file HelloJavaWorld.java: class HelloJavaWorld { public static void main (String args[]) { System.out.println ("Hello Java World!"); } } • Compile it: javac HelloJavaWorld • Execute it: java HelloJavaWorld • Should display: Hello Java World!
Ada to JVM Compiler • Ada compiler targeted to the Java Virtual Machine (JVM) • JGNAT by Ada Core Technologies • Aonix ObjectAda
JGNAT • Get JGNAT • Windows version: jgnat-1.1p-nt.exe • ftp://ftp.cs.nyu.edu/pub/gnat/jgnat/ • ftp://ftp.freesoftware.com/pub/ada/compiler/gnat/distrib/jgnat/ • Install on development computer only
JGNAT Environment Variables • Remove: • ADA_INCLUDE_PATH • ADA_OBJECTS_PATH • Add to PATH: • SET PATH=C:\JGNAT-1.1P\BIN;%PATH% • Add CLASSPATH: • SET CLASSPATH=.;C:\JGNAT-1.1P\LIB\JGNAT.JAR;%CLASSPATH% Dot is important; don’t leave it out!
Test JGNAT Installation • Create file hello.adb: with Ada.Text_IO; procedure HelloJGNAT is begin – HelloJGNAT Ada.Text_IO.Put_Line (“Hello, JGNAT World!"); end HelloJGNAT; • Compile it: jgnatmake hellojgnat • Execute it: java hellojgnat • Should display: Hello, JGNAT World!
Get MySQL • MySQL is an open-source (GPL) relational database (http://www.mysql.com) • Also available with non-GPL commercial license • Support available from TcX DataKonsult AB • See excellent user reference manual in Docs directory for installation, testing instructions and a tutorial • Install on server and development computers
MySQL Security • Remove anonymous user • mysql> delete from mysql.user where user=“”; • Give root a password • shell> mysqladmin –u root password <password> • Require passwords of all users and grant only needed privileges • Generally, only select, update, delete, and insert • mysql> grant select, update, delete, insert on <database> to <user> identified by “<password>”;
JDBC Driver • Java driver that supports Java API for using ODBC to connect to relational databases • Check http://www.mysql.com/ for available JDBC drivers for MySQL • For this tutorial, we will use Mark Matthews' driver found at http://www.worldserver.com/mm.mysql/ • Install on server and development computers
Get Jakarta Tomcat • An open-source (Apache Software Foundation) implementation of Sun's reference edition JSP-enabled web server • http://jakarta.apache.org/ • Can be run stand-alone or from within Apache • Install on server and development computers
Tomcat Environment Variables • Set environment variables • SET TOMCAT_HOME=C:\PROGRA~1\JAKARTA-TOMCAT • SET JAVA_HOME=C:\JDK1.3
Windows 95/98/ME • Use bin\startup.bat to start Tomcat • Use bin\shutdown.bat to stop Tomcat • May have to increase "Initial environment" memory size of startup.bat and shutdown.bat scripts to 2816
Windows 2000/XP • Can run Tomcat as a service • See instructions in Tomcat documentation for installation and configuration • wrapper.tomcat_home • wrapper.java_home • wrapper.class_path • Example: wrapper.properties
Test Tomcat • Start web server using startup.bat script from Tomcat bin directory • Start web browser • Go to http://localhost:8080/ to see local Tomcat home page • Try out some of the JSP and servlet examples • To shutdown web server, use shutdown.bat script from Tomcat bin directory
Table of Contents • Dynamic Web Technologies • Required Components • Introduction to JSP • Introduction to Java Beans • Introduction to JDBC • Building a Web Application
My First JSP <HTML><BODY>Hello, World!</BODY></HTML> • JSP example
Interactive JSP <HTML><BODY><% String visitor = request.getParameter("name"); if (visitor == null) visitor = "World";%>Hello, <%= visitor %></BODY></HTML> Script Tag See Interactive JSP example
Browser Server JSPContainer JSP Processing Page Request HTTP Request CompilePage SpawnServletThread Page Page
Factorial • Another JSP example • Compute and display factorials • All Java code in JSP document
The Bean Edition <html><body><jsp:useBean id="hello" class="hellobean$typ"/><jsp:setProperty name="hello" property="name" param="name"/>Hello,<jsp:getProperty name="hello" property="name"/>!</body></html> See Bean example
Browser Server JSPContainer JSP Bean Processing Page Request HTTP Request Compile Page Spawn Servlet Thread Create Bean setProperty getProperty Page Page
JSP Script Tags • Declarations <%! String visitor %> • Scriptlets <% visitor = request.getParameter("name"); if (visitor == null) visitor = "World"; %> • Expressions <%= visitor %>
JSP XML Tags • Declarations <jsp:declaration> String visitor </jsp:declaration> • Scriptlets <jsp:scriptlet> visitor = request.getParameter("name"); if (visitor == null) visitor = "World";</jsp:scriplet> • Expressions <jsp:expression> visitor </jsp:expression>
JSP Bean Tags • Create a bean <jsp:useBean id="hello" class="hellobean$typ"/> • Set bean property <jsp:setProperty name="hello" property="name" param="name"/> • Get bean property <jsp:getProperty name="hello" property="name"/>