an introduction to javaserver™ pages prepared by nicole swan

19
An Introduction to JavaServer™ Pages Prepared by Nicole Swan

Upload: kevin-underwood

Post on 18-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

An Introduction to JavaServer™ Pages An Introduction to JavaServer™ Pages

Prepared by Nicole Swan

Page 2: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

General JSP InformationGeneral JSP Information Most Common JSP Components

– Static HTML/XML components– Special JSP tags– Optionally, snippets of code written in Java called

“scriptlets”

JSPs are parsed on the server side– Translation phase occurs only once unless JSP is

updated– Compiled class is then served to each client upon

request

Page 3: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

Advantages of JSPAdvantages of JSP

Separation of static from dynamic content Write once run anywhere Dynamic content can be served in a variety of formats Recommended web access layer for n-tier architecture Completely leverages the Servlet API

(Source: http://developer.java.sun.com/developer/onlineTraining/JSPIntro/contents.html )

Page 4: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

JSP vs. ASPJSP vs. ASP

(Source: http://developer.java.sun.com/developer/onlineTraining/JSPIntro/contents.html )

JSP ASP

Web Server SupportMost popular web servers including Apache, Netscape, Microsoft IIS.

Native support only within Microsoft IIS or Personal Web Server.

Platform SupportPlatform independent. Runs on all Java-enabled platforms.

Is fully supported under Windows.

Component ModelReusable, cross-platform components like JavaBeans, EJB, and custom tag libraries.

Uses the Win32-based COM component model.

Scripting Java or JavaScript. Supports VBScript and JScript for scripting.

Security Java security model. Windows NT security architecture.

Database Access JDBC Active Data Objects

Customizable TagsExtensible with custom tag libraries.

Not extensible and cannot use custom tag libraries.

Page 5: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

Basic JSP SyntaxBasic JSP Syntax

Directives Declarations Expressions Comments Scriptlets

Page 6: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

DirectivesDirectives

page directives are found at the top of most JSP pages.

Specify java packages to include, the page language type, or the content type.

Examples<%@ page import=”java.util.*, com.foo.*”%>

<%@ page language=”java” %>

<%@ page contentType=“text/html” %>

Page 7: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

DirectivesDirectives

include directives are used to include static html content or other JSP content.

Example<%@ include file=”menu.html” %>

<%@ include file=“menu.jsp”%>

Page 8: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

DeclarationsDeclarations

Declarations are found within the <%! … %> tag.

Anything within this tag must be valid Java code including all appropriate syntax.

Example<%! int i = 0; %>

<%! public void foo(){

out.println( i );

}

%>

Page 9: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

ExpressionsExpressions

With expressions, the result of evaluating the expression is directly included in the output of the JSP page.

Examples<%= i %>

<%= fooBean.getName() %>

Page 10: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

CommentsComments

JSP comments are enclosed in the <%-- … --%> tag.

Any comments in this format are not viewable when viewing the page’s source in a browser.

Example<%-- This is a comment --%>

Note: Can still use HTML comments though they are viewable to client

Page 11: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

ScriptletsScriptlets

Scriptlets are used to embed code fragments within a JSP page.

The <% … %> tag is used. Example

<% for ( int i = 0; i < a.length; i++ ) { %>

<option value=”<%=a[i]%>”><%=a[i]%></option>

<% } %>

Page 12: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

JSP Tag LibrariesJSP Tag Libraries

Allow Java developers to decouple static HTML and complex server-side behaviors

In a sense, are a replacement for scriptlets Required use by many companies in JSP

development Example

<%@ taglib uri=“mytags-taglib.tld” prefix=“mytag” %>

<mytag:if><mytag:condition>true</mytag:condition><mytag:then>Condition was true</mytag:then><mytag:else>Condition was false</mytag:then>

</mytag:if>

Page 13: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

JavaBeansJavaBeans

JavaBeans topic is broad and complex For our purposes, allow JSP developers to

decouple static HTML and complex server-side behaviors through the use of classes

Page 14: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

JSP Development EnvironmentsJSP Development Environments

Forte for Java (Community or Enterprise edition)– Provides syntax coloring which is nice

Notepad– Simple and readily available

WYSIWYG environments– Macromedia DreamWeaver– Adobe GoLive

Macromedia ColdFusion Studio

Page 15: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

Web Servers/Servlet ContainersWeb Servers/Servlet Containers

Apache Tomcat (Open Source)– http://jakarta.apache.org/tomcat/

Jetty (Open Source)– http://jetty.mortbay.org/jetty/index.html

Bajie (free)– http://www.geocities.com/gzhangx/websrv/– Created by student at University of Texas – Dallas

Various third-party servlet containers for various web servers– http://java.sun.com/products/servlet/industry.html

Page 16: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

Tomcat Servlet ContainerTomcat Servlet Container

Can run as standalone web server Can serve static HTML as well as dynamic

JSP content

Page 17: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

Installing and Running Tomcat 4.1.18Installing and Running Tomcat 4.1.18

Download <jakarta-tomcat-4.1.18.zip> from http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.1.18/bin/

Using WinZip (or some other similar tool) extract contents to C:\ (or wherever you choose)

Set environmental variables (see handout) Start it up! (see handout) Check to see that it was installed properly

(see handout)

Page 18: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

Additional JSP ResourcesAdditional JSP Resources

JSP Tutorial – http://developer.java.sun.com/developer/onlineTraining/JSPIntro/

Java Developer Connection– http://forum.java.sun.com/

JavaServer™ Pages– http://java.sun.com/products/jsp/

JSP Syntax Reference– http://java.sun.com/products/jsp/technical.html#syntax

Google

Page 19: An Introduction to JavaServer™ Pages Prepared by Nicole Swan

Questions & Comments?Questions & Comments?