1 / 47

APPENDIX D OVERVIEW OF JSP, JSTL, and EL TAGS

APPENDIX D OVERVIEW OF JSP, JSTL, and EL TAGS. OVERVIEW OF JSP, JSTL, and EL TAGS.

morty
Télécharger la présentation

APPENDIX D OVERVIEW OF JSP, JSTL, and EL TAGS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. APPENDIX DOVERVIEW OF JSP, JSTL, and EL TAGS

  2. OVERVIEW OF JSP, JSTL, and EL TAGS • This appendix describes how to create and modify JSP pages in JD. It provides an introduction to JavaServer Pages (JSP), basic JSP tags, custom library tags, JD's JSP Standard Tag Library support (JSTL), JSP development requirements, and how to develop JSP pages using JD.

  3. Basic JSP Tags • A JSP tag is a piece of code that encapsulates a repeatable process (coded in Java) and serves as a small program within a JSP page. Its purpose is: • To reduce redundant code • To increase code legibility • To provide features that can be applied to multiple JSP pages with minimal alteration. • JSP tags use a tag syntax similar to other tag languages such as HTML. They are bracketed by "< >" symbols, and may have attributes that supply information to the tag and customize its behavior for a particular requirement.

  4. Basic JSP Tags • Two kinds of files have a .jsp extension, and both can be created using JD: • JSP pageContains HTML and JSP tags and uses JSP tag syntax (we will concentrate on this type file). • JSP 1.2 document An XML file with a .jsp extension containing XML tags and normal JSP and HTML tags.

  5. Where to Put the Code? • The preferred location for application logic is within a centralized later or business services layer. This makes the code more maintainable and reusable.

  6. Beginning and Ending Tags • When using JSP, JSTL, or EL tags, you need to provide an ending tag for each starting tag. The ending tag is coded using one of the following syntax examples: • <tag>body of tag</tag> • <tag></tag> • <tag /> (this ends the tag within the starting tag)

  7. Processing of Standard Tags • How code is processed (compiles into a servlet and rendered in HTML) depends on the type of tag. All JSP tags can be categorized as one of the following types: • Scripting elements • Directives • Actions

  8. Scripting Elements • Scripting elements are tags that the JSP container converts to Java code in the servlet that it generates from the JSP file. There are three kinds of scripting elements: • Expressions • Scriptlets • Declarations

  9. Expressions • Produce values that are output directly in the HTML page and have the following format: • <%= expression %> • The expression will be embedded inside a print statement in the servlet and can be as simple as a hard-coded value or mathematical expression like: • <%= 450 %> (displays “450”) • <%= 50*6 %> (displays “300”) • More often, the expression will be a return value from a Java method call or a variable value such as: • <%= new java.util.Date() %> (displays curerent date) • <%= salAmount %> (displays value of salAmount)

  10. Scriplets • Scriplets are snippets of standard Java code that you want the JSP container to insert into the servlet. They are designated using the delimiters "<% %>",as follows: • <% Java code; %> • This code is (inside the delimiters) is embedded into the Java servlet when the JSP page is translated. Scriptlets must be complete Java code statements (syntax errors are caught when compiled). Scriplets are used when you want to do something, such as loops and conditional tests, JSP tags cannot do.

  11. Scriplets • The following example shows how to do a conditional test of a Java int variable: <% int salAmount = 3000; if (salAmount > 0) { %> <h2>The salary is positive.</h2> <% } else { %> <table border=“1”> <tr align=“center”> <td><b>Warning!</b></td> </tr> <tr align=“center”> <td>Salary is negative or zero.<br /> Contact your Financial Advisor. </td> </tr> </table> <% } %> The space between br and the / is required.

  12. Scriplets • Output if the salAmount is positive: Output if the salAmount is zero or negative:

  13. Scriplets • Sometimes expressions and scriplets can be used for the same purpose. The following two tags will each display the same amount in the HTML page: <%= salAmount %> <% out.println( salAmount); %> • Scriplets are best used where there is a unique logic requirement for a JSP page. The do not promote the idea of reusable code because they are written specifically for a single JSP page. If you use a large scriplet in more than one JSP page, you should consider embedding the logic in a class that can be called from a scriplet or in a custom JSP tag. • See NOTE topic in text (page 750).

  14. Declarations • The JSP container inserts expressions and scriplets into the _jspService() method of the servlet. This method is called automatically when the JSP page is run. If you want to insert code outside this method, you can use a declaration, which is delimited by "<%! %>" symbols. As with scriplets, You also need to use valid Java code! Declarations do not cause any output in the browser. They are intended to generate Java code inside the class file but outside of the _jspService()method. • Declarations are useful when you need to declare a servlet-specific method that you will call once in scriplets or expressions.

  15. Declarations • For example, the following would create code for a method in the servlet class that is outside of the _jspService()method: • <%! • public static double calcRaise( int salary ) { • return ( salary * 1.3 ); • } • %> • The above method can be called from any expression or scriplet inside the JSP page, for example:<%= calcRaise( empSalary ) %> • You can also declare class variables (that are outside of any method) in the same way. By combining scriplets and declarations, you can add almost any type of code to the servlet class file.

  16. Types of Comments • A Java comment inside scriplet tags will appear in the Java servlet file. For example, the scriplet "<% // this is a comment %>" will be written as "// this is a comment" into the Java code. You can also use the multi-line comment "/*" and "*/" the same way. • The JSP file can contain a JSP comment (also called a page comment) that is not copied into the servlet file. It is used only for documentation in the JSP file. JSP comments use the delimiters "<%-- --%>". • You can embed HTML comments using the normal HTML form • "<!-- -->" which will appear in the browser's View Source window. The JSP container prints all HTML (including HTML comments) using a call to out.write().

  17. Types of Comments • You can dynamically generate an HTML comment by embedding an expression within the comment using the format: • "<!-- static comment <%= expression %> static -->"

  18. Scripting Elements in the Servlet • When the JSP container creates the Java servlet from the JSP file, it transforms the JSP tags into pure Java. Different types of scripting elements appear differently in the servlet. For example, consider the following code (from a JSP page called DemoTag.jsp):

  19. Scripting Elements in the Servlet • 01: <!-- Salary display --> • 02: <%-- scriptlets --%> • 03: <% • 04: int salAmount = 3000; • 05: if (salAmount > 0) { • 06: %> • 07: <h2>The salary is positive.</h2> • 08: <br> • 09: <% • 10: out.write("The new salary is " + calcRaise(salAmount)); • 11: %> • 12: <% • 13: } • 14: else { • 15: %> • 16: <h1>The salary is • 17: <%-- expression --%>

  20. Scripting Elements in the Servlet • 18: <%= salAmount %> • 19: </h1> • 20: <% • 21: } • 22: %> • 23: • 24: <br> • 25: <%// expression %> • 26: <%= "Salary is " + salAmount %> • 27: • 28: <%-- declaration --%> • 29: <%! • 30: public static double calcRaise(int salary) { • 31: return(salary * 1.3); • 32: } • 33: %>

  21. Scripting Elements in the Servlet • The JSP container will convert this JSP code to Java code in a servlet file called DemoTag.java. It will also compile the Java file into a .class file. • The JSP page corresponds to the following lines in the Java servlet. The line numbers refer to the lines in the JSP file that was the source for the code. The "\n" symbol creates a new line in the HTML page. Notice that lines 2 and 17 do not appear in the servlet because they are page comments that do not create code outside of the JSP page.

  22. Scripting Elements in the Servlet • 01: out.write("<!-- Salary display -->\n"): • 04: int salAmount = 3000; • 05: if (salAmount > 0) { • 07-08: out.write("<h2>The salary is positive.</h2>\n<br>\n"); • 10: out.write("The new salary is " + calcRaise(salAmount)); • 13: } • 14: else { • 16: out.write("<h1>The salary is \n"); • 18: out.print( salAmount ); • 19: out.write("\n</h1>\n"); • 21: } • 24: out.write("<br>"); • 25: // expression • 26: out.write("\n"); • 27: out.print( "Salary is " + salAmount ); • 28: out.write("\n"); • 30: public static double calcRaise(int salary) { • 31: return(salary * 1.3); • 32: }

  23. Scripting Elements in the Servlet • Lines 01-27 are written into the _jspService() method. Lines 30-32 appear in the servlet before the _jspService() method because the JSP lines that created them were written inside a declaration tag. The code uses both out.print() and out.write() to output text into the HTML that is displayed in the browser. Both methods are equivalent. • This converted code reflects the following rules for the different types of JSP code: • Scriplets are written as Java code into the _jspService()method. • Expressions are embedded into out.write() Java statements. • Declarations are written as Java code outside the _jspService() method. • Page comments are not copied into the servlet file.

  24. Scripting Elements in the Servlet • Viewing the source on the HTML page that this JSP produces, displays the following: <!—Salary display --> <h2>The Salary is positive.</h2> <br> The new salary is 3900.0 <br> • Salary is 3000 • Neither page comments ("<%-- --%>") nor Java comments • "<%// %>") are displayed in the HTML page or in the View Source window.

  25. Directives • The directive tag allows you to affect the structure of the Java servlet that is generated from the JSP file. A directive has the format "<%@directive_name%>" where "directive-name" is: • page • include • taglib

  26. page • This directive is used to specify file-level commands such as the imported classes or the page content type. Here are some examples: • <%@ page contentType="text/html;charset=windows-1252" %> • <%@ page import="java.util.*, oracle.jbo.*" errorPage="errorpage.jsp" %> • The first one specifies to the servlet the type of content—in this case, HTML ("text/html"). This generates the following servlet line in the • _jspService() method:response.setContentType( "text/html;charset=windows-1252" ); • The second line specifies the addition of the following to the servlet import section: • import java.util.*; • import oracle.jbo.*;

  27. page • It also designates which file will be displayed if an error occurs. The errorPage attribute adds the page name to the assignment of the pageControl variable in the servlet's _jspService() method, as shown below: • PageContext pageContext = JspFactory.getDefaultFactory().getPageContext( • this, request, response, "errorpage.jsp", true, • JspWriter.DEFAULT_BUFFER, true );

  28. include • This directive inserts the text from another file (for example, a JSP or HTML page) into the generated file. This is useful for a design element that will be shared by many pages. The following example inserts the output of a JSP file: • <%@ include file="TimeInfo.jsp" flush="true" %> • The TimeInfo.jsp file contains the following: • <!—Current Time is here --> • <br>The current time is: <%= new java.util.Date() %></p> • The JSP container inserts the entire file into the main JSP page before it is translated into a servlet.

  29. include • The code from the included file (the .jsp file above) is embedded into the servlet that is generated from the JSP page and therefore does not need to be compiles as a separate file. The included file is not run along with the main JSP page and does not need to be present at runtime. The source code to be included must be present at compile time. • If the included file is changed, the JSP page must be recompiled. A compile error will occur if the included source file is not available. • Another way to include a page inside another page is by using the • <jsp:include /> tag. Technically it is an action tag, but accomplishes the same thing.

  30. taglib • This directive specifies the name (alias) and location of the tag library descriptor (.tld) file – an XML file containing a list of tags, their attributes, and the classes that implement the tags. The uri (uniform resource identifier) attribute identifies the location of the tag library definition. The prefix attribute provides an alias for the tag library used in action tags. Here is an example: • <%@ taglib uri="/WEB-INF/struts-html.tld” prefix=“html” %>

  31. taglib • There is no corresponding code generated in the servlet for the taglib directive. However, the JSP container looks in the tag library identified in the tag for information about the class names of the action tags that are used in the JSP page. For example, the JSP page might have a call such as the following: • <html:form action=“/Dept.jsp.do”>

  32. taglib • The JSP container looks in the tag library definition file identified by the prefix “html" specified in the taglib for a reference to the class and path that represents the form tag (here, using the FormTag.class). It the generates code in the _jspService() method to instantiate the class (the form tag in the JSP page) and pass it parameters based on the attributes of the tag.

  33. Actions • Actions specify a component from a tag library or a standard tag. They may display output in the HTML page or write code into the servlet without showing output. The syntax for an action tag includes the tag prefix and the name of the action component as follows: • <prefix:action_name attribute=value /> • The tag_name is the actual tag used in the code and the tag has attributes with values. It is mapped to a Java class in the tag library as mentioned earlier and as shown in this example used earlier: • <html:form action=“/Dept.jsp.do”> • Much of the work done in JD with JSP pages uses action tags like above for the components.

  34. Other Standard Action Tags • JSP pages support a set of standard action tags. The JSP container needs to have CLASSPATH information that points to the core JSP JAR files. Standard tags use to prefix "jsp", which is automatically known by the JSP container and needs no taglib directive. Following is a brief description of the standard action tags:

  35. <jsp:fallback> • This tag must appear inside a plugin action tag (described later). It defines what will happen if the plugin fails to load. The example below loads the CalcErrorLoad.html page if the CalcSalary applet cannot be started: <jsp:plugin type=applet code="CalcSalary.class" > <jsp:fallback> http://www.download.com/CalcLoadError.html </jsp:fallback> </jsp:plugin>

  36. <jsp:forward> • This tag passes a request to another JSP page, servlet, or HTML file. The target location may be an expression that is dynamically loaded by other actions or Java code. The request may include parameters and values. Here is an example: • <jsp:forward page="EmpCalc.jsp" />

  37. <jsp:getProperty> • This tag returns the value of a bean property. The bean must be used before this tag (for example, using a useBean action) so that it has been instantiated. In this example, the value property of the item called "newItem" will be printed on the page: • <jsp:getProperty name="newItem" property="value" />

  38. <jsp:include> • This tag embeds a file inside the JSP page at runtime. Unlike the include directive the include file (JSP or HTML) does not need to be available when the main JSP page is compiled. However, it does need to be available when the main JSP page is run. If it is a JSP page, it needs to be compiled for the main JSP page to run correctly. Butif the included JSP page is not compiled, compilation will occur when the main JSP page is run. For example: • <jsp:include page="TimeInfo.jsp" flush="true" /> • This tag can specify a page dynamically if you embed an expression in the page attribute. This functionality is not possible with the include directive. For example, you assign the page file name to a variable includePage based upon some condition. The include tag looks like: • <jsp:include page="<%= includepage %>" flush="true" />

  39. <jsp:param> • This tag specifies a name/value pair that is embedded in (and must appear within) the fallback,include, andparamstags. Theparams tag description contains an example of the param action

  40. <jsp:params> • This action, like the fallback action, can only occur inside of a plugin action tag. It surrounds the <jsp:param> actions inside a plugin block. For example: • <jsp:plugin type=applet code="CalcSalary.class" > • <jsp:params> • <jsp:param name="id" value="101" /> • <jsp:param name="name" value="Tiger" /> • </jsp:params> • </jsp:plugin>

  41. <jsp:plugin> • This tag runs an applet or bean that may require a browser extension (plugin). The JSP engine returns an HTML tag ("embed" for Internet Explorer or "object" for Netscape). A number of attributes specify the plugin name, the type (bean or applet), the size of the display window, the directory that contains the plugin, and so on. Here is a short example: • <jsp:plugin type="applet" • code="ShowSalary.class" codebase="/devices/" • name="MainSalaryDisplay" align="bottom" • height=400 width=600> • </jsp:plugin>

  42. <jsp:setProperty> • This tag, like the getProperty tag, works with beans. It assigns the property of an existing bean object. The object must be instantiated before you call the setProperty tag. Here is an example that sets the value property of the newItem object to "Harry": • <jsp:setProperty name="newItem" property="value" • value="Harry" />

  43. <jsp:useBean> • This tag allows you to make a Java class available inside the JSP file. You can pass attributes to the object to alter its functionality and define its use. • For example: • <jsp:useBean id="fileBean" scope="page" class="oracle.jsp.webutil.fileaccess.HttpUploadBean"> </jsp:useBean>

  44. An Action Tag Example • Here is an example of a JSP action tag used to specify an HTML form that is processed by the Struts controller: • <html:form action="/Dept.jsp.do">

  45. Summary of JSP Tag Delimiters • The following summarizes the tag format shown in this section: • Type of TagFormat • Actions <prefix:action_name /> • Declaration <%! %> • Directive <%@ directive_name %> • Expression <%= %> • Page comment <%-- --%> • Scriplet <% %>

  46. Simple JSP Project • Go to the Handouts links, download, and unzip the project.

  47. An Action Tag Example JSP (LocEdit.jsp) <%@ taglib uri="/webapp/DataTags.tld" prefix="jbo" %> <jbo:ApplicationModule definition="LocJSP.Locbc4jModule" id="locAM" releasemode="Stateful" /> Class Library JAR (datatags.jar) Tag Library Definition (DataTags.tld) <tag> <name>ApplicationModule</name> <tagclass>oracle.jbo.html.jsp. datatags.ApplicationModuleTag </tagclass> <attribute> <name>id</name> <required>true</required> </attribute> . . .

More Related