DOC PREVIEW
USC CSCI 571 - JSPLectureDW2

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

AgendaPurpose of the page DirectiveThe import AttributeExample of import AttributeExample of import Attribute (Continued)Example of import Attribute: ResultThe contentType AttributeGenerating Excel SpreadsheetsThe isThreadSafe AttributeExample of Non-Threadsafe Code (IDs Must Be Unique)Is isThreadSafe Needed Here?Other Attributes of the page DirectiveIncluding Files in JSP DocumentsIncluding Pages at Request TimeIncluding Pages: Example CodeIncluding Pages: Example ResultIncluding Files at Page Translation TimeReusable JSP Content: ContactSection.jspUsing the JSP ContentUsing the JSP Content: ResultUsing JavaBeans Components with JSPBackground: What Are Beans?Basic Bean Use in JSPAccessing Bean PropertiesSetting Bean Properties: Simple CaseJSP ReviewReview: JSP IntroductionUses of JSP ConstructsReview: Calling Java Code Directly: JSP Scripting ElementsReview: The JSP page Directive: Structuring Generated ServletsReview: Including Files in JSP DocumentsReview: Using JavaBeans Components with JSPMore InformationJSP1www.corewebprogramming.comAgenda•The JSP page Directive: Structuring Generated ServletsTM•Including Files in JSP Documents•Using JavaBeans™ components with JSP•Review of JSPJSP2www.corewebprogramming.comPurpose of the page Directive•Give high-level information about the servlet that will result from the JSP page•Can control–Which classes are imported–What class the servlet extends–What MIME type is generated–How multithreading is handled–If the servlet participates in sessions–The size and behavior of the output buffer–What page handles unexpected errorsJSP3www.corewebprogramming.comThe import Attribute•Format–<%@ page import="package.class" %>–<%@ page import="package.class1,...,package.classN" %>•Purpose–Generate import statements at top of servlet•Notes–Although JSP pages can be almost anywhere on server, classes used by JSP pages must be in normal servlet dirs–For us, this is/HOME/jswdk-1.0.1/webpages/WEB-INF/servlets or/HOME/jswdk-1.0.1/examples/WEB-INF/servletsJSP4www.corewebprogramming.comExample of import Attribute...<BODY><H2>The import Attribute</H2><%-- JSP page directive --%><%@ page import="java.util.*,cwp.*" %><%-- JSP Declaration --%><%!private String randomID() { int num = (int)(Math.random()*10000000.0); return("id" + num);}private final String NO_VALUE = "<I>No Value</I>";%>JSP5www.corewebprogramming.comExample of import Attribute (Continued)<%Cookie[] cookies = request.getCookies();String oldID = ServletUtilities.getCookieValue(cookies, "userID", NO_VALUE);String newID;if (oldID.equals(NO_VALUE)) { newID = randomID();} else { newID = oldID;}LongLivedCookie cookie = new LongLivedCookie("userID", newID);response.addCookie(cookie);%><%-- JSP Expressions --%>This page was accessed at <%= new Date() %> with a userIDcookie of <%= oldID %>. </BODY></HTML>JSP6www.corewebprogramming.comExample of import Attribute: Result•First access•SubsequentaccessesJSP7www.corewebprogramming.comThe contentType Attribute•Format–<%@ page contentType="MIME-Type" %>–<%@ page contentType="MIME-Type; charset=Character-Set"%>•Purpose–Specify the MIME type of the page generated by the servlet that results from the JSP page–Remember default text-htmlJSP8www.corewebprogramming.comGenerating Excel SpreadsheetsFirst Last Email AddressMarty Hall [email protected] Brown [email protected] Gates [email protected] Ellison [email protected]<%@ page contentType="application/vnd.ms-excel" %><%-- There are tabs, not spaces, between columns. --%>JSP9www.corewebprogramming.comThe isThreadSafe Attribute•Format–<%@ page isThreadSafe="true" %> <%-- Default --%>–<%@ page isThreadSafe="false" %>•Purpose–To tell the system when your code is not threadsafe, so that the system can prevent concurrent access•Instructs servlet to implement SingleThreadModel•Notes–Default is true -- system assumes you have synchronized updates to fields & other shared data–Supplying a value of false can degrade performance (means each access creates its own thread)JSP10www.corewebprogramming.comExample of Non-Threadsafe Code (IDs Must Be Unique)•What's wrong with this code?<%! private int idNum = 0; %><% String userID = "userID" + idNum;out.println("Your ID is " + userID + ".");idNum = idNum + 1; %>JSP11www.corewebprogramming.comIs isThreadSafe Needed Here?•No<%! private int idNum = 0; %><% synchronized(this) { String userID = "userID" + idNum; out.println("Your ID is " + userID + "."); idNum = idNum + 1; }%>•Totally safe, better performance in high-traffic environmentsJSP12www.corewebprogramming.comOther Attributes of the page Directive•session–Lets you choose not to participate in sessions–<%@ page session="false" %> default is true•buffer–Changes min size of buffer used by JspWriter–<%@ page buffer="32kb" %>•autoflush–Requires developer to explicitly flush buffer•extends–Changes parent class of generated servlet–<%@ page extends="package.class" %>•errorPage–Designates a page to handle unplanned errors•isErrorPage–Stipulates that this page can be used as error page13© 2001-2002 Marty Hall, Larry Brown http://www.corewebprogramming.comWebcoreprogra mmingIncluding Files in JSP DocumentsJSP14www.corewebprogramming.comIncluding Pages at Request Time•Format–<jsp:include page="Relative URL" flush="true" />•Purpose–To reuse JSP, HTML, or plain text content–Included page must not have JSP content that affect main page: only output of included page is used–To permit updates to the included content without changing the main JSP page(s)Remember: This is done at request time—the original requested JSP page has already been translated.JSP15www.corewebprogramming.comIncluding Pages: Example Code...<BODY><TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> What's New at JspNews.com</TABLE><P>Here is a summary of our three most recent news stories:<OL> <LI><jsp:include page="news/Item1.html" flush="true" /> <LI><jsp:include page="news/Item2.html" flush="true" /> <LI><jsp:include page="news/Item3.html" flush="true" /></OL></BODY></HTML>JSP16www.corewebprogramming.comIncluding Pages: Example ResultJSP17www.corewebprogramming.comIncluding Files at Page Translation Time•Format–<%@ include file="Relative URL" %>•Purpose–To reuse JSP content in multiple pages, where JSP content affects


View Full Document

USC CSCI 571 - JSPLectureDW2

Download JSPLectureDW2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view JSPLectureDW2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view JSPLectureDW2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?