Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP Revisited Instructors: Geoffrey Fox and Bryan Carpenter

Similar presentations


Presentation on theme: "JSP Revisited Instructors: Geoffrey Fox and Bryan Carpenter"— Presentation transcript:

1 JSP Revisited Instructors: Geoffrey Fox and Bryan Carpenter
Dept. of Computer Science School of Computational Science and Information Technology 400 Dirac Science Library Florida State University Tallahassee Florida 4/20/2019 it2ejb

2 it2ejb http://aspen.csit.fsu.edu/it2spring01
This Lecture Set Review of material on JSP from IT1 lectures. Extended discussion of customized tag libraries. Integration of JSP/servlet code with Enterprise Java Beans. 4/20/2019 it2ejb

3 it2ejb http://aspen.csit.fsu.edu/it2spring01
References Web Development with JavaServer Pages, Duane K. Fields and Mark A. Kolb, Manning Publications, 2000. Thorough, insightful, and current. Java Server Pages, Hans Bergsten, O’Reilly, 2001. Brand new! Looks good. Core Servlets and JavaServer Pages, Marty Hall, Prentice Hall, 2000. IT1 lectures on JSP, at: 4/20/2019 it2ejb

4 it2ejb http://aspen.csit.fsu.edu/it2spring01
Summary Java Server Pages, recall, allow special tags and Java code to be embedded in HTML files. These tags and code are processed by the Web server to dynamically produce a standard HTML page for the browser. Produce dynamic Web pages on the server side (like Servlets), but separate application logic from the appearance of the page. May also produce XML documents, instead of HTML. 4/20/2019 it2ejb

5 Translating and Executing JSP Pages
A JSP page is executed in a JSP container, generally installed in a Web server. The underlying semantic model is that of a servlet. A typical JSP container will translate the JSP page to a Java servlet. By default translation and compilation of a JSP page is likely to occur the first time it is accessed. 4/20/2019 it2ejb

6 Possible Architecture
URL JSP page request HTTP request Tag Library JSP container compiles to a servlet response HTTP response Enterprise Java Bean HTTP page DB Browser Web server 4/20/2019 it2ejb

7 it2ejb http://aspen.csit.fsu.edu/it2spring01
JSP elements reviewed JSP elements include: Scriptlet enclosed in <% and %> markers: a small script in Java to perform arbitrary functions. Executed in the underlying servlet context. Expression: anything between <%= and %> markers is evaluated by the JSP engine as a Java expression in the servlet context. JSP directive enclosed in and %> markers—passes information to the JSP engine (guides “compilation”). JSP actions or tags are a set of customizable XML-style tags for particular actions, e.g. predefine jsp:useBean instantiates a JavaBean class on the server. Here we will be most interested in custom tags, or “actions”. 4/20/2019 it2ejb

8 Actions vs Scripting Elements
They are distinguished by syntax: Scripting elements are largely unconstrained Java “inserts”, whereas actions follow an XML-like syntax (although JSP isn’t strict XML). Also distinguished at a software engineering level: Scripting elements provide a quick way to achieve an effect, but depend on knowledge of the underlying servlet model. Custom actions, and the associated tag libraries offer a potentially higher level of abstraction. As with all good libraries, actions should be easy to use. But they are relatively hard work to implement. 4/20/2019 it2ejb

9 Tag Libraries Revisited
4/20/2019 it2ejb

10 it2ejb http://aspen.csit.fsu.edu/it2spring01
Motivations There are two kinds of action in JSP: the standard actions, and custom actions. The standard actions are quite limited: around 5 tags, that enable interaction of the page with library code implemented as JavaBeans. As illustrated in earlier lectures, one can get quite a long way by creating JavaBeans, and getting and setting their properties. But the standard actions get clumsy when complex behaviors are needed. In complex situations, the JavaBeans approach doesn’t always give a good separation between presentation logic and application logic. . . .because the JSP interface is limited to getting and setting properties of the JavaBean. Conditional or iterative behavior is limited—we are forced back to scripting elements. 4/20/2019 it2ejb

11 it2ejb http://aspen.csit.fsu.edu/it2spring01
Customized Actions Unlike the standard actions, a customized action can accept multiple arguments (as attributes) from the HTML document. We no longer have to shoehorn all functionality into the get and set methods associated with JavaBean properties. A customized action element can do non-trivial processing on the text in its body. A customized action element can process the template text and nested tags in its body more than once. This allows repeating structures like menus and tables to be created dynamically. Without resorting to scripting elements involving explicit loops. 4/20/2019 it2ejb

12 it2ejb http://aspen.csit.fsu.edu/it2spring01
An Iterative Element taglib uri="selectlib.tld" prefix="test" %> <html><head></head><body> <table border cellspacing=0 cellpadding=5> <tr bgcolor=lightblue> <td>Login id</td> <td>Dept</td> </tr> <test:select columns="*" table="it1fall00"> <tr> <td> <test:field column="1"/> </td> <td> <test:field column="2"/> </td> <td> <test:field column="3"/> </td> <td> <test:field column="4"/> </td> <td> <test:field column="5"/> </td> </tr> </test:select> </table> </body></html> 4/20/2019 it2ejb

13 it2ejb http://aspen.csit.fsu.edu/it2spring01
The Generated HTML <html><head></head><body> <table border cellspacing=0 cellpadding=5> <tr bgcolor=lightblue> <td>Login id</td> <td>Dept</td> </tr> <tr> <td> wcao </td> <td> Cao </td> <td> Whenzhong </td> <td> </td> <td> CSIT </td> </tr> <td> flora </td> <td> Flora </td> <td> Albertine Mary </td> <td> </td> <td> EE </td> . . . </table> </body></html> 4/20/2019 it2ejb

14 it2ejb http://aspen.csit.fsu.edu/it2spring01
The Displayed Page 4/20/2019 it2ejb

15 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks The select action from selectlib.tld executes an SQL SELECT query on some database. Details of the query are specified by the table and columns attributes. The body of the element is processed once for every row returned by the query. In our page, each evaluation of the body generates one row of an HTML table. This is not part of the library—the library just provides iteration over a selected data set. The field action from the selectlib library returns the column value with index specified by the column attribute. All HTML generation is handled in the JSP page—the Java code is only responsible for accessing the data base and managing iteration over the result set. 4/20/2019 it2ejb

16 The Tag Library Directive
The jsp:taglib directive informs the JSP container that this page will be using a library of customized tags. The most important attribute of the directive is uri. The value of this directive is a URL referencing the Tag Library Descriptor file (TLD). This is an XML document describing the syntax of the custom elements, and specifying where to find the associated tag handler classes. The prefix attribute just specifies a prefix that will be appended to the tag names within this document—similar to a namespace declaration in standard XML. 4/20/2019 it2ejb

17 it2ejb http://aspen.csit.fsu.edu/it2spring01
Tag-Handling Classes In JSP tag libraries, each kind of element has an associated Tag-Handling class. In our example, the select element is handled by a class called SelectTag. A tag-handling class like SelectTag (for an element that does non-trivial processing on its body) generally extends the standard class javax.servlet.jsp.tagext.BodySupportTag. This implements the interface javax.servlet.jsp.tagext.BodyTag The simpler field element (which doesn’t have to process its body) is handled by a class called FieldTag, which can extend a standard class javax.servlet.jsp.tagext.TagSupport instead. This implements javax.servlet.jsp.tagext.Tag. BodyTag extends Tag. The association between JSP elements and tag-handling classes is defined in the TLD. 4/20/2019 it2ejb

18 Life Cycle of BodyTag Handler
setPageContext() setParent() Set context e.g., setColums() setTable(), . . . Set attributes doStartTag() EVAL_BODY_TAG Initialize body content setBodyContent() setInitBody() SKIP_BODY Process body doAfterBody() SKIP_BODY EVAL_BODY_TAG doEndTag() SKIP_PAGE EVAL_PAGE release() release() 4/20/2019 it2ejb

19 Life Cycle Methods from Tag
void setPageContext(PageContext pc) Called before doStartTag(). Informs handler page context. void setParent(Tag t) Called before doStartTag(). Informs handler its parent. int doStartTag() Tells handler to process the start tag for the element. Returns EVAL_BODY_TAG or SKIP_BODY. int doEndTag() Tells handler to process end tag for element. Returns EVAL_PAGE or SKIP_PAGE. If the latter, the remainder of the page is ignored. void release() Clean up. 4/20/2019 it2ejb

20 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks When a custom tag is encountered in a JSP page, setProperty() methods are invoked on the handler for each of the attributes defined in the tag. In our example there are two properties: columns and table. The doStartTag() method is then invoked. In general this can process the attributes, optionally generate some output, and decide what, if anything, should be done about the body of the element. The doStartTag() method may return Tag.SKIP_BODY or Tag.EVAL_BODY_TAG, telling the container respectively to ignore the body of the element or go ahead and process it. For simple tag-handlers—not implementing the extended BodyTag interface—another option is Tag.EVAL_BODY_INCLUDE, which tells the JSP container to process the body in the default way. 4/20/2019 it2ejb

21 More Life Cycle Methods for BodyTag
The BodyTag interface extends the Tag interface, and adds life-cycle methods relevant to tag handlers which need to do non-default processing of the text in their bodies: int doInitBody() Informs handler to prepare for (first) evaluation of body. Typically used for loop initialization code in elements that process their bodies repeatedly. int doAfterBody() Called after some body has been evaluation. Returns either EVAL_BODY_TAG or SKIP_BODY. If the former, the JSP container will process the body again. 4/20/2019 it2ejb

22 Handling Class for the select Tag
Our handler for the select element will be a class called SelectTag. The class SelectTag extends the utility class BodyTagSupport. The BodyTagSupport class implements the BodyTag interface, and provides convenient default implementations for methods like setPageContext(), setParent(), , etc. The SelectTag class only needs to define its application-specific property-setting methods—setColumns() and setTable()—and override the three methods doStartTag() and doAfterBody(), doEndTag(). 4/20/2019 it2ejb

23 it2ejb http://aspen.csit.fsu.edu/it2spring01
The SelectTag Class package mytags ; import javax.servlet.* ; import javax.servlet.http.*; import javax.servlet.jsp.* ; import javax.servlet.jsp.tagext.* ; import java.io.* ; import java.sql.* ; public class SelectTag extends BodyTagSupport { public int doStartTag() throws JspException { } public int doAfterBody() throws JspException { } public int doEndTag() throws JspException { } public void setTable(String table) {this.table = table ; } public void setColumns(String columns) {this.columns = columns ; } ResultSet rs ; private String columns, table ; } 4/20/2019 it2ejb

24 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks The tag-handling class belongs to a package mytags. Some JSP containers require that tag library classes are put in packages. The class has two properties, columns and table, who’s values are stored in private instance variables. The names of these properties correspond directly to the allowed attribute-names of the tag. The other instance variable, rs, has default, package-wide accessibility. This means it is accessible from other tag-handling classes in the same library. 4/20/2019 it2ejb

25 The doStartTag() Method
public int doStartTag() throws JspException { try { HttpSession session = pageContext.getSession() ; DBSession dbs = (DBSession) session.getAttribute(“dbs”) ; if(dbs == null) { dbs = new DBSession() ; session.setAttribute(“dbs”, dbs) ; . . . } rs = dbs.stat.executeQuery("SELECT " + columns + " FROM " + table) ; if (rs.next()) return EVAL_BODY_TAG ; else return SKIP_BODY ; } catch ( ) { } 4/20/2019 it2ejb

26 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks The method starts with some boilerplate, servlet session-tracking code. We recycle the DBSession from the servlet version of the student database example of the IT1 lectures. The pageContext instance variable allows us to retrieve JSP predefined variables like session. The instance variables columns and table correspond to the tag attributes. They were initialized in setColumns() and setTable(). The main business is in the executeQuery() method. Its result is placed in the instance variable, rs. The return value of doStartTag() is determined by rs.next(). On its first call, this returns true iff the result set is not empty. 4/20/2019 it2ejb

27 The doAfterBody() Method
public int doAfterBody() throws JspException { try { if(rs.next()) return EVAL_BODY_TAG ; else return SKIP_BODY ; } catch ( ) { . . . Rethrow exception as JspException . . . } 4/20/2019 it2ejb

28 it2ejb http://aspen.csit.fsu.edu/it2spring01
The doEndTag() Method public int doEndTag() throws JspException { try { BodyContent body = getBodyContent() ; body.writeOut(getPreviousOut()) ; return EVAL_PAGE ; } catch ( ) { . . . Rethrow exception as JspException . . . } 4/20/2019 it2ejb

29 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks Before returning the value EVAL_PAGE, which tells the JSP container to process the remainder of the page, the method outputs the accumulated result of (repeatedly) processing its body. Any tag handler that implements BodyTag has accepted this responsibility. 4/20/2019 it2ejb

30 The JspWriter and BodyContent Classes
In general, output generated by a JSP page is written to an object of type JspWriter. JspWriter is a subclass of the standard java.io.Writer. This object is accessible in scriptlet code through the variable out. When the JSP container encounters a custom tag implementing BodyTag, it reassigns out with a reference to an object of type BodyContent. BodyContent is a subclass of JspWriter that buffers content written to it. The container gives the tag handling class a reference to the new object by calling the life cycle method setBodyContent(). As the element is processed, output is now written to this BodyContent object instead of the original JspWriter. 4/20/2019 it2ejb

31 The BodyContent Stream
BodyContent provides methods that allows its buffered content to be extracted, and, for example, written to another stream, presumably when processing of the element is complete. One such method, used above, is writeOut(). In the fragment: BodyContent body = getBodyContent() body.writeOut(getPreviousOut()) ; the call getBodyContent() returns the BodyContent object for this element; getPreviousOut() returns the JspWriter associated with out in the surrounding text. In this example, the text generated while processing the body (of the select element) is accumulated in a BodyContent object. When doEndTag() is executed, all this text is copied unmodified to the main JSP output stream. 4/20/2019 it2ejb

32 Output from Nested BodyTag Elements
. . . <tag1> <tag2> <tag3> </tag3> </tag2> </tag1> body2 body3 4/20/2019 it2ejb

33 Non-default Processing of Body
The doEndTag() or doAfterBody() methods can subject the contents accumulated in the BodyContent stream to arbitrary processing, before copying it to the parent output stream, e.g.: BodyContent b = getBodyContent() ; JspWriter out = getPreviousOut() ; out.write( doTransformation(b.getString()) ) ; Here the function doTransformation() can do any desired transformation to the text accumulated by processing the body of the element. For example, the body might contain XML. The transformation function could parse this XML using DOM and apply some XSLT transformation to it. . . <transformXML stylesheet=“mystyle.xsl”> . . . Arbitrary XML . . . </transformXML> 4/20/2019 it2ejb

34 Handling Class for the field Tag
We still have to discuss the implementation of the handling class for the field element, which accesses column values in the rows enumerated by select. The handling class, FieldTag, does not need to process a body. It can therefore extend the simpler TagSupport utility class. It only needs to define the setter method for the column attribute, and doStartTag() or doEndTag(). But there is a new issue that needs to be addressed: somehow the handling objects for nested field elements need to access the ResultSet object generated by the select handler. 4/20/2019 it2ejb

35 Accessing Parents and Ancestors
This isn’t hard in principle. The container gives a tag handling object a reference to the handler for the most closely enclosing custom tag by invoking the setParent() life-cyle method. In the tag support classes, this value can be accessed by getParent(). Often one doesn’t necessarily want the most closely enclosing element; one wants the most closely enclosing element of a particular type. A utility method is provided for this. Using Java reflection, the method findAncestorWithClass() chases parent links until it finds an ancestor with a specified tag-handling class. 4/20/2019 it2ejb

36 it2ejb http://aspen.csit.fsu.edu/it2spring01
The FieldTag Class package mytags ; import javax.servlet.jsp.* ; import javax.servlet.jsp.tagext.* ; import java.sql.* ; public class FieldTag extends TagSupport { public int doStartTag() throws JspException { } public void setColumn(String column) {this.column = column ; } private String columns ; } 4/20/2019 it2ejb

37 The doStartTag() Method
public int doStartTag() throws JspException { try { JspWriter out = pageContext.getOut() ; SelectTag parent = (SelectTag) findAncestorWithClass(this, SelectTag.class) ; out.print(parent.rs.getString(Integer.parseInt(column))) ; } catch (. . .) { } return SKIP_BODY ; } 4/20/2019 it2ejb

38 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks The tag-handling object for the enclosing select element by passing this object and the class object for SelectTag to the findAncestorWithClass() utility. The column value can then be extracted from the rs field of that object—i.e. the ResultSet computed by the select element. 4/20/2019 it2ejb

39 The Tag Library Descriptor File
The TLD file is in XML format. It has some preamble describing the XML version and document type. The main body is a taglib element. This element in turn contains some global information about the library, then (most importantly) a series of tag elements. Each tag element defines the syntax and (indirectly) the semantics of a custom tag: Tag name. Handler class. Attribute names and properties. How to deal with the element body, if there is one. 4/20/2019 it2ejb

40 TLD File for the “selectlib” Example
<?xml version="1.0" encoding="ISO " ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" " <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>myselect</shortname> <uri></uri> <info> Simple SQL query library </info> tag elements </taglib> 4/20/2019 it2ejb

41 it2ejb http://aspen.csit.fsu.edu/it2spring01
A TLD tag Element <tag> <name>select</name> <tagclass>mytags.SelectTag</tagclass> <info> Do a simplified SQL select query, and iterated over results </info> <attribute> <name>columns</name> <required>true</required> </attribute> <name>table</name> <bodycontent>JSP</bodycontent> </tag> 4/20/2019 it2ejb

42 Another TLD tag Element
<name>select</name> <tagclass>mytags.FieldTag</tagclass> <info> Extract field from current row </info> <attribute> <name>column</name> <required>true</required> </attribute> <bodycontent>EMPTY</bodycontent> </tag> 4/20/2019 it2ejb

43 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks The preamble defines the XML version and an XML Document Type Definition file for the document. Required elements inside a taglib element are the tlibversion element which give a version number for the tag library, and the shortname element used to identify the library. A tag element specifies: Java classes that define the behavior of the action. attribute elements that define the tag’s attributes. An attribute may be “required” or optional. Its value may be required to be a constant string, or allowed to be a dynamic, request-time expression (“rtexprval” element). The body content of the the defined element may be specified to be EMPTY. It may also be specified to be JSP, or tagdependent. 4/20/2019 it2ejb

44 it2ejb http://aspen.csit.fsu.edu/it2spring01
Simple Deployment The TLD file is just an ordinary Web document, accessed with a relative or absolute URL. It may sit alongside the HTML and JSP files. The class files for the handler classes may be placed under the WEB-INF/classes subdirectory along with servlet classes. Note however that the usual rules about directories and packages apply: in our example the class files should go in WEB-INF/classes/mytags. 4/20/2019 it2ejb

45 Packaging a Tag Library
If you are distributing a tag library for use in multiple applications, you will want to create a jar archive. Rename selectlib.tld to taglib.tld (this seems to be necessary) and arrange the files in a directory structure like (e.g.): META-INF/ taglib.tld mytags/ SelectTag.class FieldTag.class DBSession.class DBConstants.class Create an archive by jar cvf selectlib.jar META-INF mytags Copy the jar file to a directory WEB-INF/lib/ in the context document root. You can then import the tag library by, e.g.: taglib uri=”/WEB-INF/lib/selectlib.jar" prefix="test" %> 4/20/2019 it2ejb

46 Tags that Define Scripting Variables
Some JSP actions define variables that are subsequently accessible in scriplet inserts. One example is the standard action jsp:usebean. In the earlier lectures we were rather purist about use of JavaBeans in JSP pages, and only accessed their properties through jsp:getProperty and jsp:setProperty methods, e.g. <jsp:useBean id=“now” class=“DateBean”/> <html><head></head><body> Now is <jsp:getProperty name=“now” property=“date”/> </body></html> In fact the jsp:useBean action also introduces a servlet-code variable, now, into the page, which can be be accessed directly in scriptlets; we could replace the jsp:getProperty action with: <%= now.getDate() %> 4/20/2019 it2ejb

47 Custom Tags that Declare Variables
One of the advantages of JSP actions is supposed to be that they reduce or eliminate the need for complex programming inserts in Web pages. On the other hand scripting elements can certainly be convenient if they are used in a disciplined way, e.g., just make an occasional call to a Java library method. For example, the mechanism of the previous page could be used to circumvent the restrictions of the standard actions, calling methods on a bean class besides property “getters” and “setters”. JSP accordingly provides a mechanism whereby custom tags can introduce scripting variables. 4/20/2019 it2ejb

48 it2ejb http://aspen.csit.fsu.edu/it2spring01
An Example Here is a relatively complex example adapted from The Java 2 Enterprise Edition Developer’s Guide: taglib uri=“taglib.tld” prefix=“j2ee” > <html><head></head><body> <% Converter converter ; %> <j2ee:ejb jndiName=“java:comp/env/ejb/MyConverter”, homeInterface=“ConverterHome”, homeVar=“converterHome”> <% converter = converterHome.create() ; %> </j2ee:ejb> dollarToYen: <%= converter.dollarToYen(100.00) %> <p> yenToEuro: <%= converter.yenToEuro(100.00) %> </body></html> 4/20/2019 it2ejb

49 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks This example actually uses scripting inserts rather freely—to explicitly declare a local variable converter, and to explicitly call methods in various places. The example is interesting in its own right, as it uses JSP—and in particular tag libraries—to manipulate an Enterprise Java Bean. However, the main point to notice here is the variable converterHome. Unlike converter, there is no explicit, scriplet declaration for converterHome—it is created and initialized by the custom tag, j2ee:ejb. In this example we can assume that the scope of the variable convertHome is the body of the j2ee:ejb element. This contrasts with jsp:useBean, where the scope of the introduced scripting variable is the part of the page following the element. 4/20/2019 it2ejb

50 Compilation of the JSP Page
How can a custom tag introduce or modify a variable that is local to servlet code? Recall the main code for the custom tag goes in tag handler classes, defined in some precompiled library. Somehow the tag library must influence the process of “compilation” of the JSP page to servlet code, in such a way that extra statements (declarations of, and assignments to, local variables) are added. One possibility might be to add the extra information to the tag library descriptor, since this already guides the compilation process. This might be inflexible though, because the name and type of the introduced variables generally isn’t known when the XML is written. Both jsp:useBean and j2ee:ejb take these from attribute values specified in the actual JSP. 4/20/2019 it2ejb

51 it2ejb http://aspen.csit.fsu.edu/it2spring01
Helper Classes Instead JSP allows the tag library implementer to provide extra information to guide compilation through “helper classes”. The helper classes extend TagExtraInfo. In particular, to effect introduction of new variables into the servlet code, one implements a helper class that overrides the getVariableInfo() method this class. The getVariableInfo() method returns an array of objects of type VariableInfo—one element for each variable to be created or updated by the action. VariableInfo is a simple bean describing the new variable. 4/20/2019 it2ejb

52 it2ejb http://aspen.csit.fsu.edu/it2spring01
Another Example Rather than starting with a whole new example, we will modify our selectlib example: taglib uri="select2lib.tld" prefix="test" %> <html><head></head><body> <table border cellspacing=0 cellpadding=5> <tr bgcolor=lightblue> <td>Login id</td> <td>Dept</td> </tr> <test:select columns="*" table="it1fall00” resultName=“row”> <tr> <td> <%= row.getString(1) %> </td> <td> <%= row.getString(2) %> </td> . . . <td> <%= row.getString(5) %> </td> </tr> </test:select> </table> </body></html> 4/20/2019 it2ejb

53 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks This version has no separate field tag. Instead the select tag scopes a variable of type java.sql.ResultSet, which can be accessed in scripting elements inside its body. The name of the declared variable is selected by the resultName attribute of the select tag. 4/20/2019 it2ejb

54 The TagExtraInfo Class
package mytags ; import javax.servlet.jsp.tagext.* ; public class Select2TagExtraInfo extends TagExtraInfo { public VariableInfo [] getVariableInfo(TagData data) { return new VariableInfo[] { new VariableInfo(data.getAttributeString("resultName"), "java.sql.ResultSet", true, VariableInfo.NESTED) } ; } 4/20/2019 it2ejb

55 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks Select2TagExtraInfo overrides getVariableInfo() to return an array holding one VariableInfo bean. The argument of getVariableInfo() contains information about the tag and attributes encountered at JSP “compile time”. The VariableInfo bean has 4 properties, passed to the constructor: varName, the name of the scripting variable, className, its type, declare, a boolean flag that specifies whether this tag declares the variable or simply modifies it, and scope, one of NESTED, AT_BEGIN or AT_END. This information controls the declaration of the script variable. 4/20/2019 it2ejb

56 Modified doStartTag() Method
public int doStartTag() throws JspException { try { HttpSession session = pageContext.getSession() ; . . . DB session initialization rs = dbs.stat.executeQuery("SELECT " + columns + " FROM " + table) ; if (rs.next()) { pageContext.setAttribute(resultName, rs) ; return EVAL_BODY_TAG ; } else return SKIP_BODY ; } catch ( ) { } 4/20/2019 it2ejb

57 it2ejb http://aspen.csit.fsu.edu/it2spring01
Remarks The tag handling class acquires a new field resultName, initialized from the tag attribute. The most important new feature is that the value of the query result set, rs, is stored in an attribute of the page context object, having the same name as the new scripting variable. The servlet code compiled from the JSP correspondingly contains statements like: java.sql.ResultSet varName ; varName = (java.sql.ResultSet) pageContext.findAttribute(“varName”) ; where varName and java.sql.ResultSet are the name and type handed to the container by the Select2TagExtraInfo class. This explains how the tag library class can modify a local variable of the servlet: they exchange information through the page context object, which is passed to the tag handler in the setPageContext() life-cycle method. 4/20/2019 it2ejb


Download ppt "JSP Revisited Instructors: Geoffrey Fox and Bryan Carpenter"

Similar presentations


Ads by Google