Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 APPENDIX D OVERVIEW 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.

Similar presentations


Presentation on theme: "1 APPENDIX D OVERVIEW 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."— Presentation transcript:

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

2 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 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 codeTo reduce redundant code To increase code legibilityTo increase code legibility To provide features that can be applied to multiple JSP pages with minimal alteration.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 4 Basic JSP Tags Two kinds of files have a.jsp extension, and both can be created using JD: JSP page Contains HTML and JSP tags and uses JSP tag syntax (we will concentrate on this type file).JSP page Contains 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.JSP 1.2 document An XML file with a.jsp extension containing XML tags and normal JSP and HTML tags.

5 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 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: body of tag body of tag (this ends the tag within the starting tag) (this ends the tag within the starting tag)

7 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 elementsScripting elements DirectivesDirectives ActionsActions

8 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: ExpressionsExpressions ScriptletsScriptlets DeclarationsDeclarations

9 9 Expressions Produce values that are output directly in the HTML page and have the following format: 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: (displays “450”) (displays “450”) (displays “300”) (displays “300”) More often, the expression will be a return value from a Java method call or a variable value such as: (displays curerent date) (displays curerent date) (displays value of salAmount) (displays value of salAmount)

10 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: 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 11 Scriplets The following example shows how to do a conditional test of a Java int variable: <% int salAmount = 3000; if (salAmount > 0) { %> The salary is positive. <% } else { %> Warning! Salary is negative or zero. Contact your Financial Advisor. <% } %> The space between br and the / is required.

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

13 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: 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 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 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 ) { public static double calcRaise( int salary ) { return ( salary * 1.3 ); return ( salary * 1.3 ); }%> The above method can be called from any expression or scriplet inside the JSP page, for example: The above method can be called from any expression or scriplet inside the JSP page, for example: 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 16 Types of Comments A Java comment inside scriplet tags will appear in the Java servlet file. For example, the scriplet " " 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 17 Types of Comments You can dynamically generate an HTML comment by embedding an expression within the comment using the format: " static -->" " static -->"

18 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 19 Scripting Elements in the Servlet 01: 02: 03: <% 04: int salAmount = 3000; 05: if (salAmount > 0) { 06: %> 07: The salary is positive. 08: 09: <% 10: out.write("The new salary is " + calcRaise(salAmount)); 11: %> 12: <% 13: } 14: else { 15: %> 16: The salary is 17:

20 20 Scripting Elements in the Servlet 18: 19: 20: <% 21: } 22: %> 23: 24: 25: 26: 27: 28: 29: <%! 30: public static double calcRaise(int salary) { 31: return(salary * 1.3); 32: } 33: %>

21 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 22 Scripting Elements in the Servlet 01: out.write(" \n"): 04: int salAmount = 3000; 05: if (salAmount > 0) { 07-08: out.write(" The salary is positive. \n \n"); 10: out.write("The new salary is " + calcRaise(salAmount)); 13: } 14: else { 16: out.write(" The salary is \n"); 18: out.print( salAmount ); 19: out.write("\n \n"); 21: } 24: out.write(" "); 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 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.Scriplets are written as Java code into the _jspService() method. Expressions are embedded into out.write() Java statements.Expressions are embedded into out.write() Java statements. Declarations are written as Java code outside the _jspService() method.Declarations are written as Java code outside the _jspService() method. Page comments are not copied into the servlet file.Page comments are not copied into the servlet file.

24 24 Scripting Elements in the Servlet Viewing the source on the HTML page that this JSP produces, displays the following: The Salary is positive. The new salary is 3900.0 Salary is 3000 Salary is 3000 Neither page comments (" ") nor Java comments " ") are displayed in the HTML page or in the View Source window.

25 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 "" where "" is: 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 " " where "directive-name" is: pagepage includeinclude taglibtaglib

26 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: 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: _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 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 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: The TimeInfo.jsp file contains the following: The current time is: The JSP container inserts the entire file into the main JSP page before it is translated into a servlet.

29 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 tag. Technically it is an action tag, but accomplishes the same thing. tag. Technically it is an action tag, but accomplishes the same thing.

30 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:

31 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:

32 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 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: 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: Much of the work done in JD with JSP pages uses action tags like above for the components.

34 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 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: http://www.download.com/CalcLoadError.html

36 36 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:

37 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:

38 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: 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: " flush="true" /> " flush="true" />

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

40 40 <jsp:params> This action, like the fallback action, can only occur inside of a plugin action tag. It surrounds the actions inside a plugin block. For example:

41 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" <jsp:plugin type="applet" code="ShowSalary.class" codebase="/devices/" code="ShowSalary.class" codebase="/devices/" name="MainSalaryDisplay" align="bottom" name="MainSalaryDisplay" align="bottom" height=400 width=600> height=400 width=600>

42 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" <jsp:setProperty name="newItem" property="value" value="Harry" /> value="Harry" />

43 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:

44 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:

45 45 Summary of JSP Tag Delimiters The following summarizes the tag format shown in this section: Type of TagFormat Actions Actions Declaration Declaration Directive Directive Expression Expression Page comment Page comment Scriplet Scriplet

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

47 47 An Action Tag Example JSP (LocEdit.jsp) Tag Library Definition (DataTags.tld) ApplicationModule oracle.jbo.html.jsp. datatags.ApplicationModuleTag id true... Class Library JAR (datatags.jar)


Download ppt "1 APPENDIX D OVERVIEW 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."

Similar presentations


Ads by Google