OOSSE Week 7 Java Server Pages Format of lecture: Introduction What are Java Server Pages? (JSPs) What do you need to run JSPs? Demo of an example of a JSP in action Version 1.2 Nov 2009 Slide 1
OOSSE Week 7 Web-Enabled Applications Objectives of this session:- Overview/History. List the advantages of web-enabled applications. Understand how object oriented technology can help with this type of application. List technologies that make web-enabled applications possible. List the capabilities of tools in developing this type of application. Version 1.1 Nov 2008 Slide 2
OOSSE Week 7 Overview Early web users were limited to static HTML. Useful for presenting company information often in a hierarchical manner. Little or no interaction with the user. Java applets addressed this problem enabling additional code to be downloaded but still a problem of building customised information. Dynamically generated HTML using the Common Gateway Interface (CGI) was also a proposed solution. Version 1.1 Nov 2008 Slide 3
OOSSE Week 7 Overview CGI-created HTML was the forerunner for server-side Java based techniques. Java Servlets. JavaServer Pages(JSP). Component-based computing (Enterprise JavaBeans). Active Server Pages (ASP). Hypertext Preprocessor (PHP) Visual Basic Web Classes. Version 1.1 Nov 2008 Slide 4
OOSSE Week 7 Evolution of Applications – good exam question! One-tier - mainframe based/dumb terminal/central application source. Standalone/PC/Mini computers/multi copy of application. Two tier - client-server - distributed part of the application on the client(GUI) and part on the server. Version 1.1 Nov 2008 Slide 5
OOSSE Week 7 Evolution of Applications – good exam question! Two tier - client-server Early versions of client-server required installation of GUI and business logic. Recently, Java applets downloaded on demand. With server side servlets and JSP no download and the browser is used as the interface - this is a good solution! Version 1.1 Nov 2008 Slide 6
OOSSE Week 7 Evolution of Applications Three tier/n-tier - user interface(UI) layer; the business layer(BL) and the database layer(DL) UI - presentation of information browsers such as Netscape or Explorer on the client BL - logic of procedures about the business DL - where the data is stored - usually a relational database but could be an object oriented database Version 1.1 Nov 2008 Slide 7
OOSSE Week 7 Benefits of Web-Enabled n-tier Applications Web applications can be called self-service applications - they enable ‘things’ to be done from a web browser - e.g. online banking. Run from anywhere - installation free. Cost reduction: Browser based interface is easy to use - little training required. Development costs lower as using a standard interface. No installation. Use of open standards: Maintenance easier. Enhancement of applications easier. Version 1.1 Nov 2008 Slide 8
OOSSE Week 7 Web Enabling Techniques How to develop web-based applications and how can a server process multiple requests? Need a fast response! Scalability - HTTP protocol - works in a stateless request-response mode - means it does not bind a server to a client for an inordinate amount of time There are a number of techniques which can be used to concurrently deal with thousands of clients Version 1.1 Nov 2008 Slide 9
OOSSE Week 7 Stuff that we need Just like when using ASP or PHP, we need a server that “understands” our JSP code We need an http server which supports JSP We will use a (free) commercial server called Tomcat We could use Glassfish, in part based on Tomcat ( ) Version 1.1 Nov 2008 Slide 10
OOSSE Week 7 What are JSPs? They are HTML documents that are interleaved with Java which provides the dynamic content. JSPs are server-side - they accept a request and generate a response (an HTML document). JSP is the next generation on from Servlets - an extension of Servlets with more power and flexibility. JSPs are the equivalent of Microsoft’s Active Server Pages (ASP) technology and Zend’s PHP system. Version 1.1 Nov 2008 Slide 11
OOSSE Week 7 Facts about JSPs All JSPs have a file extension of.jsp JSP is meant to be easier than Servlets. A JSP page generates a Servlet (or more) Syntax of a typical JSP is on the next two slides. Over the next four weeks we will look at the syntax of JSPs in depth. Version 1.1 Nov 2008 Slide 12
OOSSE Week 7 simpleguestlist.jsp An Example of a Simple JSP Accessing a database Model A Listing Of Guests Firstname Lastname Version 1.1 Nov 2008 Slide 13
OOSSE Week 7 <% try {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:odbc:GuestBook"); Statement statement = conn.createStatement(); String sql = "SELECT * FROM GuestBook"; ResultSet rs = statement.executeQuery(sql); while (rs.next()) { %> <% } %> <%if (statement != null) statement.close(); if (conn != null) conn.close(); } catch (Exception e) {out.print(e);} %> Version 1.1 Nov 2008 Slide 14
OOSSE Week 7 Kit required What do you need to develop and run JSPs? Up-to-date JDK Tomcat HTTP server With database interaction we need a database – we will use MSAccess for convenience We could use mySQL or any other supported SQL-based database Version 1.1 Nov 2008 Slide 15
OOSSE Week 7 Demo of a JSP This example is a JSP which uses a DSN to the GuestBook database Normally we prefer DSN-less connections The application model is essentially a JSP connecting to a database Version 1.1 Nov 2008 Slide 16
OOSSE Week 7 JSP Translation process Version 1.1 Nov 2008 Slide 17 file (HTML interleaved with Java) HTTP Server TOMCAT Request:Browser page link to call.jsp (either via locator or a link on an HTML input page) Response: HTML for output sent from Server Response HTML document Request: JSP translated into a Servlet.java file and then compiled into a Servlet,class Backend database, usually on the server Query for data content
OOSSE Week 7 SimpleGuestList.jsp Version 1.1 Nov 2008 Slide 18
OOSSE Week 7 Summary We have established that JSP represents a technology that can be used for dynamic web applications The market for this type of technology is well established JSP demands some programming skills but is more and more seen as an integration technology with can dovetail with technologies such as XML Version 1.1 Nov 2008 Slide 19
OOSSE Week 7 Summary We used an HTTP server which supports JSP We highlighted the features of a JSP A.jsp file is essentially HTML with Java code inserts When the.jsp file is called via a browser a.java file is created to represent Servlet source code and then the.java file is compiled into a bytecode file with a.class extension. The.java file can be viewed via a text editor but MUST not be edited The compilation is only done once - subsequent calls to the JSP use the already compiled.class file Version 1.1 Nov 2008 Slide 20
OOSSE Week 7 Mal’s E-Commerce This is one of my sample E-Commerce sites that uses Mal’s E-Commerce shopping cart software From the source code of the site you can see how it it configured to use Mal’s system To use the software you must apply for an account and then it is a question of following the instructions given via the Mal’s websitewebsite Version 1.1 Nov 2008 Slide 21