30
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming

JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Embed Size (px)

DESCRIPTION

JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA. James Faeldon CS 119 Enterprise Systems Programming. Overview. Introduction to JSP Understanding the need for JSP Evaluating the benefits of JSP Installing JSP Files JSP Scripting Elements JSP Expressions JSP Scriplets - PowerPoint PPT Presentation

Citation preview

Page 1: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

JAVA SERVER PAGESCREATING DYNAMIC WEB PAGES USING JAVA

James FaeldonCS 119 Enterprise Systems Programming

Page 2: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Overview

Introduction to JSP Understanding the need for JSP Evaluating the benefits of JSP Installing JSP Files

JSP Scripting Elements JSP Expressions JSP Scriplets JSP Declarations

JSP Page Directive Understanding the purpose of the page directive Designating which classes are imported Specifying the MIME type of the page

Page 3: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Introduction to JSP

Java Server Pages (JSP) technology enables you to mix regular, static HTML with dynamically generated content.

OrderConfirmation.jsp

Page 4: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Introduction to JSP

Dynamic Pages can change in response to different context or conditions.

HTTP request parameters used as dynamic content

Page 5: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Servlets vs. JSP

Servlets are good for data processing while JSP is good for presentation.

Benefits of JSP It is easier to write and maintain the HTML You can use standard Web-site development tools You can divide up your development team

Page 6: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Reading 3 Parameters (Example)

SERVLETHTML code in JAVA

JSPJAVA code in HTML

ThreeParams.java ThreeParams.jsp

Page 7: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Reading 3 Parameters (Example)

Page 8: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Servlets vs. JSP

JSP doesn’t provide any capabilities that couldn’t be accomplished with Servlets.

JSP documents are automatically translated into servlets behind the scenes.

The issue is not the power of the technology, it is the convenience, productivity, and maintainability of one or the other.

Choose the right tool for the job.

Page 9: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Installation of JSP files

JSP Directories for Tomcat

Main Location:

tomcat_install_dir/webapps/ROOT Corresponding URL:

http://localhost:8080/SomeFile.jsp

More Specific Location (Arbitrary Subdirectory):

tomcat_install_dir/webapps/ROOT/SomeDirectory Corresponding URL:

http://localhost:8080/SomeDirectory/SomeFile.jsp

Page 10: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Types of JSP Scripting Elements

Expressions of the form <%= Java Expression %>, which are evaluated and inserted into the servlet’s output.

Scriptlets of the form <% Java Code %>, which are inserted into the servlet’s _jspService method (called by service).

Declarations of the form <%! Field/Method Declaration %>, which are inserted into the body of the servlet class, outside any existing methods.

Page 11: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Using JSP Expressions

A JSP expression is used to insert values directly into the output. It has the following form:

<%= Java Expression %>

Example:

Current time: <%= new java.util.Date() %>

Page 12: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Predefined Variables

request, the HttpServletRequest. response, the HttpServletResponse. session, the HttpSession associated with the request (unless

disabled with the session attribute of the page directive out, the Writer (a buffered version of type JspWriter) used to send

output to the client. application, the ServletContext. This is a data structure shared

by all servlets and JSP pages in the Web application and is good for storing shared data.

Example:

Your hostname: <%= request.getRemoteHost() %>

Page 13: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Example: JSP Expressions

Expressions.jsp

Page 14: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Example: JSP Expressions

Page 15: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Writing JSP Scriplets

If you want to do something more complex than output the value of a simple expression, JSP scriptlets let you insert arbitrary code. It has the following form:

<% Java Code %>

Example:

<%

String queryData = request.getQueryString();

out.println("Attached GET data: " + queryData);

%>

Page 16: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Example: JSP Scriplets

BgColor.jsp

Page 17: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Example: JSP Scriplets

Page 18: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Another Example: JSP Scriplets

DayWish.jsp

Page 19: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Another Example: JSP Scriplets

Page 20: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Using JSP Declarations

A JSP declaration lets you define methods or fields that get inserted into the main body of the servlet class. It has the following form:

<%! Field or Method Definition %>

Example:

<H1>Some Heading</H1><%!private String randomHeading() {

return("<H2>" + Math.random() + "</H2>");}

%><%= randomHeading() %>

Page 21: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Example: JSP Declarations

AccesCounts.jsp

Page 22: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Example: JSP Declarations

Page 23: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

JSP Directive

A JSP Directive affects the overall structure of the JSP page. It has the form:

<%@ directive attribute="value" %>

<%@ directive attribute1="value1"

attribute2="value2"

...

attributeN="valueN" %>

There are three main types of directives: page, include and taglib.

Page 24: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

The import Attribute

The import attribute of the page directive lets you specify the packages that should be imported into the JSP page.

<%@ page import="package.class" %>

<%@ page import="package.class1,...,package.classN" %>

Example:

<%@ page import="java.util.*" %>

Date today: <%= new Date() %>

Page 25: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

The contentType Attribute

The contentType attribute sets the Content-Type response header, indicating the MIME type of the document being sent to the client.

Use of the contentType attribute takes one of the following two forms.

<%@ page contentType="MIME-Type" %><%@ page contentType="MIME-Type; charset=Character-Set" %>

Example:First Last Email AddressJames Gosling [email protected] Brown [email protected] Balmer [email protected] McNealy [email protected]<%@ page contentType="application/vnd.ms-excel" %><%-- There are tabs, not spaces, between columns. --%>

Page 26: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Example: Conditionally Generating Excel Spreadsheets

ApplesAndOranges.jsp

Page 27: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Example: Conditionally Generating Excel Spreadsheets

Page 28: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Strategies for invoking dynamic code in JSP

Call Java code directly. Place all Java code in JSP page. Appropriate only for very small amounts of code.

Call Java code indirectly. Develop separate utility classes. Insert into JSP page only the Java code needed to invoke the utility classes.

Use beans. Develop separate utility classes structured as beans. Use jsp:useBean, jsp:getProperty, and jsp:setProperty to invoke the code.

Use the MVC architecture. Have a servlet respond to original request, look up data, and store results in beans.Forward to a JSP page to present results. JSP page uses beans.

Use the JSP expression language. Use shorthand syntax to access and output object properties. Usually used in conjunction with beans and MVC.

Use custom tags. Develop tag handler classes. Invoke the tag handlers with XML-like custom tags.

Page 29: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Tips

Limit the amount of Java code that is in JSP pages. At the very least, use helper classes that are invoked from the JSP pages.

Put all your classes in packages.

Define most methods with separate Java classes, not JSP declarations.

Page 30: JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA

Exercise:

Create a web page that will display the contents of the PERSON table in the owners database.

You should be able to display the records in an HTML table or in an Excel spreadsheet.

Note: If your browser does not support displaying Excel spreadsheet a prompt will show just choose to open the file.