Boston Computing Review - Java Server Pages

38
Boston Computing Review 2007 Boston Computing Review Pragmatic Architectural Overview Java Server Pages John Brunswick

description

This presentation gives a high level overview of the components of Java Server Pages.

Transcript of Boston Computing Review - Java Server Pages

Page 1: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Boston Computing ReviewPragmatic Architectural Overview

Java Server Pages

John Brunswick

Page 2: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

• Why? This is so old school.

• History and Pains of Web Development

• Foundation

• Development Tools

• Anatomy

• Nuts and Bolts

• Over the Horizon

Page 3: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Why? It is not sexy.

• Presentation Layer for J2EE• Nobody owns it• It is here and not going anywhere• What do non-Microsoft projects get developed

with? Yup.– Oracle, BEA, IBM, SAP, EMC, HP… and on and on.

• Want to code in the enterprise?– API heaven

• Frameworks– JSF and others

Page 4: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

HistoryStep into the time machine

Page 5: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Common Gateway Interface (CGI)

• CGI is not a programming language

• Most CGI programs are written in Perl

• Issues– Scalability– Security– Debugging– Seperation of Presentation and Logic

Page 6: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Classic ASP (3.0)

• Single platform (MS)

• Mixed presentation and logic

• Run time interpretation

Page 7: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Hypertext Preprocessor (PHP)

• Lacks OO Design

• Runtime interpretation

• Mixed presentation and logic

Page 8: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Coldfusion Markup Language (CFM)• Depth

• Scalability

• Enterprise Interoperability

Page 9: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

ASP.NET

• Platform (MS)

• Lacks MVC

• Cost

Page 10: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Emerging Web Application Frameworks• ROR

• Django

Page 11: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Digging In

Page 12: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Foundation Overview

• JSP is actually servlets!

• Application / HTTP Server

• Acronym Overload

• How do we develop?

• Application Anatomy 101

• JDBC

Page 13: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Servlet JSP• HelloWorld Servlet• package org.apache.jsp;• import javax.servlet.*;• import javax.servlet.http.*;• import javax.servlet.jsp.*;• public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase• implements org.apache.jasper.runtime.JspSourceDependent {• private static java.util.Vector _jspx_dependants;• public java.util.List getDependants() {• return _jspx_dependants;• }• 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;• JspWriter _jspx_out = null;• PageContext _jspx_page_context = null;• try {• _jspxFactory = JspFactory.getDefaultFactory();• response.setContentType("text/html");• pageContext = _jspxFactory.getPageContext(this, request, response,• null, true, 8192, true);• _jspx_page_context = pageContext;• application = pageContext.getServletContext();• config = pageContext.getServletConfig();• session = pageContext.getSession();• out = pageContext.getOut();• _jspx_out = out;• out.write("<html>\n");• out.write(" <head>\n");• out.write(" <title>JSP and Beyond Hello World</title>\n");• out.write(" </head>\n");• out.write(" <body>\n");• out.write(" Hello World \n");• out.write(" </body>\n");• out.write("</html>\n");• } catch (Throwable t) {• if (!(t instanceof SkipPageException)){• out = _jspx_out;• if (out != null && out.getBufferSize() != 0)• out.clearBuffer();• if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);• }• } finally {• if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);• }• }• }

• <html>• <head>• <title>JSP and Beyond Hello World</title>• </head>• <body>• Hello World • </body>• </html>

Page 14: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Server Architecture (container)

Page 15: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Application Servers

• Tomcat– Apache who?

• JBOSS

• Resin

• BEA WebLogic

• WebSphere

• JRUN… (not so much)

Page 16: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Hmmm… Acronyms Abound

• JDK

• J2SE

• SDK

• JRE

• J2EE

• Java 5

• JVM

Page 17: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Start Coding

• Netbeans

• Eclipse

Page 18: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Anatomy 101• YourWebApp/

– Within this directory all static content should reside. This includes JSP, HTML and image files.

• YourWebApp /WEB-INF/– The Web Application deployment descriptor (web.xml) lives within this directory. This

deployment descriptor maintains all of your application settings that dictate how the container delivers your application. This directory is private and not externally accessible by end users.

• YourWebApp /WEB-INF/classes– Java classes and servlets should reside in this directory. This directory is private and not

externally accessible by end users.

• YourWebApp /WEB-INF/lib– Place JAR file and tag libraries here. This directory is private and not externally accessible

by end users.

FYI…. During application development it is most advantageous to work with your application files in what is called an exploded format. Once the application is ready for distribution it can be packaged into a WAR file for easy portability.

Page 19: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

WARs JARs and EARs?

• WAR– One big zip file for an application, no fighting!

• JAR– Zip file with classes

• EAR– Lots of WARs, JARs– For Enterprise (not Kirk and Spock)

Page 20: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

JSP Dissected

Page 21: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Page Elements

Page 22: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Scriptlet <% … %>

• Inline Code (yuck)<table> <% String months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}; for(int i = 0; i < months.length; i++ ) { %> <tr> <td> Month: <%= months[i] %> </td> </tr> <% } %></table>

Page 23: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Directives <%@ … %>

• Directives do not send output to the screen, but set configuration values for the JSP page

• Page– <%@ page import="java.util.*, java.lang.*" %> – errorPage / isErrorPage– Buffering… etc…

• Include Directive• Taglib Directive• http://java.sun.com/products/jsp/syntax/1.2/synta

xref1210.html.

Page 24: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Expression <%= … %>

<%@ page import="java.util.*" %><%! String sName = "Bill Smith"; %><table> <tr> <td> Welcome <%= sName %>! </td> </tr></table>

• Straight to screen output

Page 25: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Decleration <%! … %>

• Used to set variables and define methods within the JSP <%@ page import="java.util.*" %>

<%!// This is the method that will return the current timeString GetCurrentTime(){

Date date = new Date();return date.toString();

}%><table><tr><td>What time is it? <%= GetCurrentTime() %></td></tr></table>

Page 26: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Comment <%-- … --%>

• With the JSP style comments we can keep comments inline with the code, but they will not be sent to the requestor’s browser.

Page 27: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Programming Elements

Page 28: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Implicit Objects

• Request– Cookies, querystring variables and other pieces of data are

readily accessible from the request object.

• Response– Response is used to send information to the client. A good

example might be setting a cookie. The following block of code send a cookie to the client that can be retrieved at a later time <% response.addCookie(myCookie) %>

• Session– The session object is useful for storing small amounts of

information that will be accessible throughout a users visit to your application or web site. A good example might be their user ID so you will not have to continually query a database to find this information

• Etc…..

Page 29: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Coffee Time - JavaBeans

• Provide a simple interface for storing, retrieving information and other complex operations through a very simple XML tag

• By using this level of integration with JSP JavaBeans can help us to separate the presentation (HTML) from the business logic that the Beans handle

• Value VS Utility• Reuse• Reuse

Page 30: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Get and Set (encapsulate)public class OurSampleBean{

String sOurTestValue;public OurSampleBean() {}

public String getSometValue(){

Return someValue;}public void setSomeValue (String sSomeValue){

this.value = sSomeValue;}

}

Page 31: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

JDBC – Get our DB on

// Import the namespace for the database connection

import java.sql// Create the database connection objectConnection cnDB = null;// Load the driver that will be used for the database connection

Class.forName("com.mysql.jdbc.Driver").newInstance();

// Establish the connectioncnDB = DriverManager.getConnection("jdbc:mysql:///DatabaseName","user", "password");

Page 32: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

JDBC Cont…

// Prepare a statement object that will be used to request the data

Statement stmt = cnDB.createStatement(); // Create an object to hold the results setResultSet results;// Populate the results object with the data from the SQL statement above

results = stmt.executeQuery("SELECT * FROM tblCustomer ORDER BY CustomerName");

Page 33: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

One more for the road

// Obtain a statement objectStatement stmt = cnDB.createStatement();

// Execute the block of SQL codestmt.executeUpdate("INSERT INTO tblCustomer VALUES ('Smith', 'Boston', 2006)");

// Close the result setstmt.close();// Close the connectioncnDB.close();

Page 34: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

JSTL – Messy but fun

• Hmmm… CFM anyone?

• Good for prototyping

<c:if test="${book.orderQuantity > book.inStock}"> The book <c:out value="${book.title}"/> is currently out of stock. </c:if>

Page 35: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

And then some…

Page 36: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

And then…

• Error handling<%@ page errorPage="CatchError.jsp"%>

<%@ page isErrorPage="true" %>

• Email– Grab a library

• File upload– Use a bean

Page 37: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Next Level

• MVC and Frameworks– JSF, Struts

Page 38: Boston Computing Review - Java Server Pages

Boston Computing Review 2007

Thanks for your time!