[ Pobierz całość w formacie PDF ]
.For example, the line:[ Add Comment ]<%@ page language="java" %>says that the scripting language beingused within the JSP page is Java.In fact, the JSP specification onlydescribes the semantics of scripts for the language attribute equal to“Java.” The intent of this directive is to build flexibility intothe JSP technology.In the future, if you were to choose another language, sayPython (a good scripting choice), then that language would have to support theJava Run-time Environment by exposing the Java technology object model to thescripting environment, especially the implicit variables defined above,JavaBeans properties, and public methods.[ Add Comment ]The most important directive is the pagedirective.It defines a number of page dependent attributes and communicatesthese attributes to the JSP container.These attributes include:language, extends, import, session, buffer,autoFlush, isThreadSafe, info and errorPage.Forexample:<%@ page session=”true” import=”java.util.*” %>This line first indicates that the pagerequires participation in an HTTP session.Since we have not set the languagedirective the JSP container defaults to using Java and the implicit scriptlanguage variable named session is of typejavax.servlet.http.HttpSession.If the directive had been false then theimplicit variable session would be unavailable.If the sessionvariable is not specified, then it defaults to “true.”[ Add Comment ]The import attribute describes thetypes that are available to the scripting environment.This attribute is usedjust as it would be in the Java programming language, i.e., a comma-separatedlist of ordinary import expressions.This list is imported by thetranslated JSP page implementation and is available to the scriptingenvironment.Again, this is currently only defined when the value of thelanguage directive is “java.”[ Add Comment ]JSP scripting elementsOnce the directives have been used to setup the scripting environment you can utilize the scripting language elements.JSP 1.1 has three scripting language elements—declarations,scriptlets, and expressions.A declaration will declare elements,a scriptlet is a statement fragment, and an expression is a complete languageexpression.In JSP each scripting element begins with a“<%”.The syntax for each is:<%! declaration %><% scriptlet %><%= expression %>White space is optional after“<%!”, “<%”, “<%=”, and before“%>.”[ Add Comment ]All these tags are based upon XML; youcould even say that a JSP page can be mapped to a XML document.The XMLequivalent syntax for the scripting elements above would be:<jsp:declaration> declaration </jsp:declaration><jsp:scriptlet> scriptlet </jsp:scriptlet><jsp:expression> expression </jsp:expression>In addition, there are two types ofcomments:<%-- jsp comment --%><!-- html comment -->The first form allows you to add commentsto JSP source pages that will not appear in any form in the HTML that is sent tothe client.Of course, the second form of comment is not specific toJSPs—it’s just an ordinary HTML comment.What’s interesting isthat you can insert JSP code inside an HTML comment and the comment will beproduced in the resulting page, including the result from the JSP code.[ Add Comment ]Declarations are used to declarevariables and methods in the scripting language (currently Java only) used in aJSP page.The declaration must be a complete Java statement and cannot produceany output in the out stream.In the Hello.jsp example below, thedeclarations for the variables loadTime, loadDate andhitCount are all complete Java statements that declare and initialize newvariables.//:! c15:jsp:Hello.jsp<%-- This JSP comment will not appear in thegenerated html --%><%-- This is a JSP directive: --%><%@ page import="java.util.*" %><%-- These are declarations: --%><%!long loadTime= System.currentTimeMillis();Date loadDate = new Date();int hitCount = 0;%><html><body><%-- The next several lines are the result of aJSP expression inserted in the generated html;the '=' indicates a JSP expression --%><H1>This page was loaded at <%= loadDate %> </H1><H1>Hello, world! It's <%= new Date() %></H1><H2>Here's an object: <%= new Object() %></H2><H2>This page has been up<%= (System.currentTimeMillis()-loadTime)/1000 %>seconds</H2><H3>Page has been accessed <%= ++hitCount %>times since <%= loadDate %></H3><%-- A "scriptlet" that writes to the serverconsole and to the client page.Note that the ';' is required: --%><%System.out.println("Goodbye");out.println("Cheerio");%></body></html>///:~When you run this program you’llsee that the variables loadTime, loadDate and hitCount holdtheir values between hits to the page, so they are clearly fields and not localvariables.[ Add Comment ]At the end of the example is a scriptletthat writes “Goodbye” to the Web server console and“Cheerio” to the implicit JspWriter object out.Scriptlets can contain any code fragments that are valid Java statements.Scriptlets are executed at request-processing time.When all the scriptletfragments in a given JSP are combined in the order they appear in the JSP page,they should yield a valid statement as defined by the Java programming language.Whether or not they produce any output into the out stream depends uponthe code in the scriptlet.You should be aware that scriptlets can produce sideeffects by modifying the objects that are visible to them.[ Add Comment ]JSP expressions can found intermingledwith the HTML in the middle section of Hello.jsp.Expressions must becomplete Java statements, which are evaluated, coerced to a String, andsent to out.If the result of the expression cannot be coerced to aString then a ClassCastException is thrown.[ Add Comment ]Extracting fields and valuesThe following example is similar to oneshown earlier in the servlet section.The first time you hit the page it detectsthat you have no fields and returns a page containing a form, using the samecode as in the servlet example, but in JSP format.When you submit the form withthe filled-in fields to the same JSP URL, it detects the fields and displaysthem.This is a nice technique because it allows you to have both the pagecontaining the form for the user to fill out and the response code for that pagein a single file, thus making it easier to create and maintain.//:! c15:jsp:DisplayFormData.jsp<%-- Fetching the data from an HTML form.--%><%-- This JSP also generates the form.--%><%@ page import="java.util.*" %><html><body><H1>DisplayFormData</H1><H3><%Enumeration flds = request.getParameterNames();if(!flds.hasMoreElements()) { // No fields %><form method="POST"action="DisplayFormData
[ Pobierz całość w formacie PDF ]