using javaserver pages harry r. erwin, phd cit304/cse301

19
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Upload: darcy-melton

Post on 17-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Using JavaServer Pages

Harry R. Erwin, PhD

CIT304/CSE301

Page 2: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Review

• Last lecture, we examined the use of Java servlets as a way to provide dynamic content.

• Problems:– Requires fully-qualified Java programmers (£)– Tedious and error-prone (£)– Time-consuming and requires frequent update (££)– Adding a new servlet involves editing XML and restarting

the application (£)– Mixing static HTML with servlets does not scale (£).

• This lecture, we will examine the use of JavaServer Pages to overcome these problems.

Page 3: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Resources

• Farley, Crawford, and Flanagan, 2002, Java Enterprise in a Nutshell, second edition, O’Reilly, ISBN: 0-596-00152-5. For programmers.

• Bergsten, 2002, JavaServer Pages, second edition, O’Reilly, ISBN: 0-596-00317-X. Covers JSP 1.2, for both programmers and web page authors. Has lots of worked examples. Recommended. If you have to manage web application development, it won’t hurt you to know something about the process.

Page 4: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

JavaServer Pages (JSP)

• Allow you to embed the dynamic elements into the static content.

• Can be edited using a text editor or HTML authoring tool.

• Can be previewed live in the various browsers that you plan to support.

Page 5: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Template Engines

ResourcesTemplateProcessor

WebServer

WebBrowser

InstructionTemplate

Page 6: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Java and JSP

• JSP pages are compiled into Java servlets before being executed.

• Benefits– More efficient lifecycle– Integration with Java– Easy use of JavaBeans without writing code– Java code can be completely avoided.– Avoids programming.

Page 7: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

JSP Basics

• Individual JSP pages are text files stored on the web server.

• When a page is first requested, the JSP engine uses the page to generate a servlet.

• The compiled servlet is saved and used to handle later requests.

• When a page is modified, the servlet is recreated.• Precompilation of pages is also feasible.

Page 8: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

A Simple JSP (from Java Enterprise in a Nutshell)

<HTML><BODY>Hello, sailor, it is now <%= new java.util.Date().toString() %></BODY></HTML>

The only non-self-evident element is the: “new java.util.Date().toString()”This creates a date object and converts it to a String thatcan be displayed. The <%= %> tag is are used to display a Java variable. XML syntax can also be used, if you want tobe open to the future.

Page 9: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Points to Note

• More generally, the <% %> elements (HTML/XML comments) can be used to insert regular Java code to control the flow of a page.

• We will discuss this in more detail next lecture.• The next slide gives an example of an ‘if, then,

else’ flow control construct used in a JSP page. This is also from Java Enterprise in a Nutshell.

Page 10: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Java Flow Control

<HTML><BODY><% java.util.Date theDate = new java.util.Date(); %><% int theHour = theDate.getHours(); %><% if(theHour<12) { %> Good morning,<% } else { %> Good afternoon,<% } %> sailor. It is now <%= theDate.toString() %>.</BODY></HTML>

Page 11: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Java APIs Available

• JDBC (=ODBC)

• RMI and CORBA

• Java Naming and Directory Interface

• Enterprise JavaBeans

• Java Message Service

• Java Transaction API

• JAXP

• JavaMail

Page 12: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

JSP Directives

• Affect the entire page.• Begin with <%@ or <% @• <% @include file=“data.html”%>

– Used to include static content

• <% @page … %>– Used to set page parameters

• <% @taglib … %>– Declares a tag library (later…)

Page 13: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Important Page Directives

• contentType– MIME type of the page, default: text/html

• extends– JspPage class to be used if not default.

• import– Java classes or packages to import.

• info– A description of JSP page.

• session– Indicates whether page participates in a user session.

Page 14: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Declaration Elements

• This is another type of tag used to create variables.• <%! global variable declaration=value; %>• These are defined at the level of the JSP rather

than at the session level. They persist across invocations.

• They can be used to count hits and detect hacking. Not all that useful. Use other approaches, if the data need to persist whenever the JSP is rebuilt and reloaded.

Page 15: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Built-ins

• Your web server provides classes that the JSP can use. These include:– Configuration data– The standard output stream– The request that created the invocation– The response being generated– The current user’s session record.

Page 16: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

What’s in it for the Web Designer?

• JSP supports action tags.

• These are regular HTML tags of two types:– Built-in functions– Custom tags

• Built-in functions use XML namespace-enabled tag syntax. They work with JavaBeans, too.

Page 17: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Standard Action Tags• <jsp:useBean> imports a JavaBeans component.• <jsp:getProperty> gets a property value and adds it

to the response.• <jsp:setProperty> sets a property value.• <jsp:include> includes the response from a servlet

or JSP page that is called for the request.• <jsp:forward> forwards the call to a servlet or JSP

page for service.• <jsp:param> adds a parameter to a request.• <jsp:plugin> generates the OBJECT or EMBED

tag for an applet.

Page 18: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

taglib Directives

• If the web site architect wants to extend the JSP tag syntax, she can also define custom action tags. These are organized into Tag Libraries and can be loaded using the taglib directive.

• This allows her to remove almost all Java code from the JSPs. Her web page developers use these custom tags to replace the calls to Java functions, beans and programs.

• The programmers can then do their jobs separately from everyone else.

Page 19: Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

Conclusions

• JSP is a solution to providing dynamic content.• Equivalent to ASP.NET but currently more

popular.• Can be used to avoid Java scripting.• Next lecture:

– Java programming basics—just enough to let you organize your JSP pages—there is no requirement to learn class or method syntax unless you want to provide programming support to a web design organization.