appendix d overview of jsp, jstl, and el tags

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

Upload: morty

Post on 09-Jan-2016

47 views

Category:

Documents


0 download

DESCRIPTION

APPENDIX D OVERVIEW OF JSP, JSTL, and EL TAGS. OVERVIEW OF JSP, JSTL, and EL TAGS. - PowerPoint PPT Presentation

TRANSCRIPT

1

APPENDIX DAPPENDIX DOVERVIEW OF JSP, JSTL, OVERVIEW OF JSP, JSTL,

and EL TAGSand EL TAGS

2

OVERVIEW OF JSP, JSTL, and EL OVERVIEW OF JSP, JSTL, and EL TAGSTAGS

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

3

Basic JSP TagsBasic JSP Tags

A JSP tag is a piece of code that encapsulates a A JSP tag is a piece of code that encapsulates a repeatable process (coded in Java) and serves as a repeatable process (coded in Java) and serves as a small program within a JSP page. Its purpose is: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 To provide features that can be applied to multiple

JSP pages with minimal alteration.JSP pages with minimal alteration.

JSP tags use a tag syntax similar to other tag JSP tags use a tag syntax similar to other tag languages such as HTML. They are bracketed by "languages such as HTML. They are bracketed by "< >< >" " symbols, and may have symbols, and may have attributesattributes that supply that supply information to the tag and customize its behavior for a information to the tag and customize its behavior for a particular requirement.particular requirement.

4

Basic JSP TagsBasic JSP Tags

Two kinds of files have a Two kinds of files have a .jsp.jsp extension, and both can extension, and both can be created using JD:be created using JD:

• JSP pageJSP page Contains HTML and JSP tags and uses Contains HTML and JSP tags and uses JSP tag syntax (we will JSP tag syntax (we will concentrate on this type file).concentrate on this type file).

• JSP 1.2 documentJSP 1.2 documentAn XML file with a An XML file with a .jsp.jsp extension extension

containing XML tags andcontaining XML tags and normal JSP and HTML tags.normal JSP and HTML tags.

5

Where to Put the Code?Where to Put the Code?

The preferred location for application logic is within a The preferred location for application logic is within a centralized later or business services layer. This makes centralized later or business services layer. This makes the code more maintainable and reusable. the code more maintainable and reusable.

6

Beginning and Ending TagsBeginning and Ending Tags

When using JSP, JSTL, or EL tags, you need to provide When using JSP, JSTL, or EL tags, you need to provide an ending tag for each starting tag. The ending tag is an ending tag for each starting tag. The ending tag is coded using one of the following syntax examples:coded using one of the following syntax examples:

• <tag>body of tag</tag><tag>body of tag</tag>

• <tag></tag><tag></tag>

• <tag /> <tag /> (this ends the tag within the starting (this ends the tag within the starting tag)tag)

7

Processing of Standard TagsProcessing of Standard Tags

How code is processed (compiles into a servlet and How code is processed (compiles into a servlet and rendered in HTML) depends on the type of tag. All JSP rendered in HTML) depends on the type of tag. All JSP tags can be categorized as one of the following types:tags can be categorized as one of the following types:

• Scripting elementsScripting elements

• DirectivesDirectives

• ActionsActions

8

Scripting Elements Scripting Elements

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

• ExpressionsExpressions

• ScriptletsScriptlets

• DeclarationsDeclarations

9

Expressions Expressions Produce values that are output directly in the HTML Produce values that are output directly in the HTML page and have the following format:page and have the following format:

<%= expression %><%= expression %>

The expression will be embedded inside a print The expression will be embedded inside a print statement in the servlet and can be as simple as a statement in the servlet and can be as simple as a hard-coded value or mathematical expression like:hard-coded value or mathematical expression like:

<%= 450 %><%= 450 %> (displays “450”)(displays “450”)

<%= 50*6 %><%= 50*6 %> (displays “300”)(displays “300”)

More often, the expression will be a return value from More often, the expression will be a return value from a Java method call or a variable value such as:a Java method call or a variable value such as:

<%= new java.util.Date() %> <%= new java.util.Date() %> (displays curerent (displays curerent date)date)

<%= salAmount %> <%= salAmount %> (displays value of salAmount)(displays value of salAmount)

10

ScripletsScripletsScriplets are snippets of standard Java code that you Scriplets are snippets of standard Java code that you want the JSP container to insert into the servlet. They want the JSP container to insert into the servlet. They are designated using the delimiters "are designated using the delimiters "<% %><% %>",", as as follows:follows:

<% Java code; %><% Java code; %>

This code is (inside the delimiters) is embedded into This code is (inside the delimiters) is embedded into the Java servlet when the JSP page is translated. the Java servlet when the JSP page is translated. Scriptlets must be complete Java code statements Scriptlets must be complete Java code statements (syntax errors are caught when compiled). Scriplets (syntax errors are caught when compiled). Scriplets are used when you want to do something, such as loops are used when you want to do something, such as loops and conditional tests, JSP tags cannot do. and conditional tests, JSP tags cannot do.

11

ScripletsScripletsThe following example shows how to do a conditional The following example shows how to do a conditional test of a Java test of a Java intint variable: 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

ScripletsScripletsOutput if the Output if the salAmountsalAmount is positive: is positive:

Output if the salAmount is zero or negative:

13

ScripletsScripletsSometimes expressions and scriplets can be used for Sometimes expressions and scriplets can be used for the same purpose. The following two tags will each the same purpose. The following two tags will each display the same amount in the HTML page:display the same amount in the HTML page:

<%= salAmount %><%= salAmount %>

<% out.println( salAmount); %><% out.println( salAmount); %>

Scriplets are best used where there is a unique logic Scriplets are best used where there is a unique logic requirement for a JSP page. The do not promote the requirement for a JSP page. The do not promote the idea of reusable code because they are written idea of reusable code because they are written specifically for a single JSP page. If you use a large specifically for a single JSP page. If you use a large scriplet in more than one JSP page, you should scriplet in more than one JSP page, you should consider embedding the logic in a class that can be consider embedding the logic in a class that can be called from a scriplet or in a custom JSP tag. called from a scriplet or in a custom JSP tag.

See See NOTENOTE topic in text (page 750). topic in text (page 750).

14

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

Declarations are useful when you need to declare a Declarations are useful when you need to declare a servlet-specific method that you will call once in servlet-specific method that you will call once in scriplets or expressions.scriplets or expressions.

15

DeclarationsDeclarationsFor example, the following would create code for a method For example, the following would create code for a method in the servlet class that is outside of the in the servlet class that is outside of the _jspService()_jspService() method: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 The above method can be called from any expression or scriplet inside the JSP page, for example:scriplet inside the JSP page, for example:

<%= calcRaise( empSalary ) %><%= calcRaise( empSalary ) %>

You can also declare class variables (that are outside of any You can also declare class variables (that are outside of any method) in the same way. By combining scriplets and method) in the same way. By combining scriplets and declarations, you can add almost any type of code to the declarations, you can add almost any type of code to the servlet class file.servlet class file.

16

Types of CommentsTypes of CommentsA Java comment inside scriplet tags will appear in the Java A Java comment inside scriplet tags will appear in the Java servlet file. For example, the scriplet servlet file. For example, the scriplet ""<% // this is a <% // this is a comment %>comment %>" will be written as " will be written as ""// this is a comment// this is a comment" " into the Java code. You can also use the multi-line comment into the Java code. You can also use the multi-line comment ""/*/*" and "" and "*/*/" the same way." the same way.

The JSP file can contain a JSP comment (also called a The JSP file can contain a JSP comment (also called a page page commentcomment) that is not copied into the servlet file. It is used ) that is not copied into the servlet file. It is used only for documentation in the JSP file. JSP comments use the only for documentation in the JSP file. JSP comments use the delimiters "delimiters "<%-- --%><%-- --%>".".

You can embed HTML comments using the normal HTML You can embed HTML comments using the normal HTML formform

""<!-- --><!-- -->" which will appear in the browser's View Source " which will appear in the browser's View Source window. The JSP container prints all HTML (including window. The JSP container prints all HTML (including HTML comments) using a call to HTML comments) using a call to out.write()out.write(). .

17

Types of CommentsTypes of CommentsYou can dynamically generate an HTML comment by You can dynamically generate an HTML comment by embedding an expression within the comment using embedding an expression within the comment using the format:the format:

""<!-- static comment <%= expression %> static --><!-- static comment <%= expression %> static -->""

18

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

19

Scripting Elements in the ServletScripting Elements in the Servlet01: <!-- 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 ServletScripting Elements in the Servlet18: <%= 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 ServletScripting Elements in the ServletThe JSP container will convert this JSP code to Java The JSP container will convert this JSP code to Java code in a servlet file called code in a servlet file called DemoTag.javaDemoTag.java. It will also . It will also compile the Java file into a compile the Java file into a .class.class file. file.

The JSP page corresponds to the following lines in the The JSP page corresponds to the following lines in the Java servlet. The line numbers refer to the lines in the Java servlet. The line numbers refer to the lines in the JSP file that was the source for the code. The "JSP file that was the source for the code. The "\n\n" " symbol creates a new line in the HTML page. Notice symbol creates a new line in the HTML page. Notice that lines that lines 22 and and 1717 do not appear in the servlet do not appear in the servlet because they are page comments that do not create because they are page comments that do not create code outside of the JSP page.code outside of the JSP page.

22

Scripting Elements in the ServletScripting Elements in the Servlet01: 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 ServletScripting Elements in the ServletLines Lines 01-2701-27 are written into the are written into the _jspService()_jspService() method. Lines method. Lines 30-32 30-32 appear in the servlet before the appear in the servlet before the _jspService()_jspService() method method because the JSP lines that created them were written inside a because the JSP lines that created them were written inside a declaration tag. The code uses both declaration tag. The code uses both out.print()out.print() and and out.write()out.write() to output text into the HTML that is displayed in to output text into the HTML that is displayed in the browser. Both methods are equivalent.the browser. Both methods are equivalent.

This converted code reflects the following rules for the different This converted code reflects the following rules for the different types of JSP code:types of JSP code:

• Scriplets are written as Java code into the Scriplets are written as Java code into the _jspService()_jspService() method.method.

• Expressions are embedded into Expressions are embedded into out.write()out.write() Java Java statements.statements.

• Declarations are written as Java code outside the Declarations are written as Java code outside the _jspService()_jspService() method. method.

• Page comments are not copied into the servlet file. Page comments are not copied into the servlet file.

24

Scripting Elements in the ServletScripting Elements in the ServletViewing the source on the HTML page that this JSP Viewing the source on the HTML page that this JSP produces, displays the following:produces, displays the following:

<!—Salary display -->

<h2>The Salary is positive.</h2>

<br>

The new salary is 3900.0

<br>

Salary is 3000Salary is 3000

Neither page comments ("Neither page comments ("<%-- --%><%-- --%>") nor Java ") nor Java commentscomments

""<%// %><%// %>") are displayed in the HTML page or in the ") are displayed in the HTML page or in the View Source window.View Source window.

25

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

• pagepage

• includeinclude

• taglibtaglib

26

pagepageThis directive is used to specify file-level commands such as This directive is used to specify file-level commands such as the imported classes or the page content type. Here are some the imported classes or the page content type. Here are some examples:examples:<%@ page contentType="text/html;charset=windows-1252" %><%@ page contentType="text/html;charset=windows-1252" %>

<%@ page import="java.util.*, oracle.jbo.*" errorPage="errorpage.jsp" %><%@ page import="java.util.*, oracle.jbo.*" errorPage="errorpage.jsp" %>

The first one specifies to the servlet the type of content—in The first one specifies to the servlet the type of content—in this case, HTML ("text/html"). This generates the following this case, HTML ("text/html"). This generates the following servlet line in the servlet line in the _jspService()_jspService() method: method:

response.setContentType( "text/html;charset=windows-1252" );

The second line specifies the addition of the following to the The second line specifies the addition of the following to the servlet import section:servlet import section:import java.util.*;

import oracle.jbo.*;

27

pagepageIt also designates which file will be displayed if an It also designates which file will be displayed if an error occurs. The error occurs. The errorPageerrorPage attribute adds the page attribute adds the page name to the assignment of the name to the assignment of the pageControlpageControl variable in variable in the servlet's the servlet's _jspService()_jspService() method, as shown below: method, as shown below:

PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(

this, request, response,"errorpage.jsp", true,

JspWriter.DEFAULT_BUFFER, true );

28

includeincludeThis directive inserts the text from another file (for This directive inserts the text from another file (for example, a JSP or HTML page) into the generated file. example, a JSP or HTML page) into the generated file. This is useful for a design element that will be shared This is useful for a design element that will be shared by many pages. The following example inserts the by many pages. The following example inserts the output of a JSP file:output of a JSP file:

<%@ include file="TimeInfo.jsp" flush="true" %><%@ include file="TimeInfo.jsp" flush="true" %>

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

29

includeincludeThe code from the included file (the The code from the included file (the .jsp.jsp file above) is file above) is embedded into the servlet that is generated from the JSP embedded into the servlet that is generated from the JSP page and therefore does not need to be compiles as a page and therefore does not need to be compiles as a separate file. The included file is not run along with the separate file. The included file is not run along with the main JSP page and does not need to be present at runtime. main JSP page and does not need to be present at runtime. The source code to be included must be present at compile The source code to be included must be present at compile time.time.

If the included file is changed, the JSP page must be If the included file is changed, the JSP page must be recompiled. A compile error will occur if the included recompiled. A compile error will occur if the included source file is not available.source file is not available.

Another way to include a page inside another page is by Another way to include a page inside another page is by using the using the

<jsp:include /><jsp:include /> tag. Technically it is an action tag, but tag. Technically it is an action tag, but accomplishes the same thing.accomplishes the same thing.

30

taglibtaglibThis directive specifies the name (alias) and location of This directive specifies the name (alias) and location of the the tag library descriptortag library descriptor ( (.tld.tld) file – an XML file ) file – an XML file containing a list of tags, their attributes, and the containing a list of tags, their attributes, and the classes that implement the tags. The classes that implement the tags. The uriuri ( (uniform uniform resource identifierresource identifier) attribute identifies the location of ) attribute identifies the location of the tag library definition. The the tag library definition. The prefixprefix attribute provides attribute provides an alias for the tag library used in action tags. Here is an alias for the tag library used in action tags. Here is an example:an example:

<%@ taglib uri="/WEB-INF/struts-html.tld” prefix=“html” %>

31

taglibtaglibThere is no corresponding code generated in the There is no corresponding code generated in the servlet for the servlet for the taglibtaglib directive. However, the JSP directive. However, the JSP container looks in the tag library identified in the tag container looks in the tag library identified in the tag for information about the class names of the action for information about the class names of the action tags that are used in the JSP page. For example, the tags that are used in the JSP page. For example, the JSP page might have a call such as the following:JSP page might have a call such as the following:

<html:form action=“/Dept.jsp.do”>

32

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

33

ActionsActionsActions specify a component from a tag library or a Actions specify a component from a tag library or a standard tag. They may display output in the HTML page or standard tag. They may display output in the HTML page or write code into the servlet without showing output. The write code into the servlet without showing output. The syntax for an action tag includes the tag syntax for an action tag includes the tag prefixprefix and the and the name of the action component as follows:name of the action component as follows:

<prefix:action_name attribute=value />

The The tag_nametag_name is the actual tag used in the code and the is the actual tag used in the code and the tag has attributes with values. It is mapped to a Java class tag has attributes with values. It is mapped to a Java class in the tag library as mentioned earlier and as shown in this in the tag library as mentioned earlier and as shown in this example used earlier:example used earlier:

<html:form action=“/Dept.jsp.do”>

Much of the work done in JD with JSP pages uses action Much of the work done in JD with JSP pages uses action tags like above for the components. tags like above for the components.

34

Other Standard Action TagsOther Standard Action TagsJSP pages support a set of standard action tags. The JSP pages support a set of standard action tags. The JSP container needs to have JSP container needs to have CLASSPATHCLASSPATH information information that points to the core that points to the core JSP JARJSP JAR files. Standard tags use files. Standard tags use to prefix "to prefix "jspjsp", which is automatically known by the JSP ", which is automatically known by the JSP container and needs no container and needs no taglibtaglib directive. Following is a directive. Following is a brief description of the standard action tags:brief description of the standard action tags:

35

<jsp:fallbackjsp:fallback>

This tag must appear inside a plugin action tag This tag must appear inside a plugin action tag (described later). It defines what will happen if the (described later). It defines what will happen if the plugin fails to load. The example below loads the plugin fails to load. The example below loads the CalcErrorLoad.htmlCalcErrorLoad.html page if the page if the CalcSalaryCalcSalary applet applet cannot be started: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, This tag passes a request to another JSP page, servlet, or HTML file. The target location may be an expression or HTML file. The target location may be an expression that is dynamically loaded by other actions or Java that is dynamically loaded by other actions or Java code. The request may include parameters and values. code. The request may include parameters and values. Here is an example:Here is an example:

<jsp:forward page="EmpCalc.jsp" />

37

<jsp:getProperty><jsp:getProperty>

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

<jsp:getProperty name="newItem" property="value" />

38

<jsp:include><jsp:include>

This tag embeds a file inside the JSP page at runtime. Unlike This tag embeds a file inside the JSP page at runtime. Unlike the the include directiveinclude directive the include file (JSP or HTML) does not the include file (JSP or HTML) does not need to be available when the main JSP page is compiled. need to be available when the main JSP page is compiled. However, it does need to be available when the main JSP page 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 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 JSP page to run correctly. Butif the included JSP page is not compiled, compilation will occur when the main JSP page is compiled, compilation will occur when the main JSP page is run. For example: run. For example:

<jsp:include page="TimeInfo.jsp" flush="true" />

This tag can specify a page dynamically if you embed an This tag can specify a page dynamically if you embed an expression in the page attribute. This functionality is not expression in the page attribute. This functionality is not possible with the include directive. For example, you assign possible with the include directive. For example, you assign the page file name to a variable the page file name to a variable includePageincludePage based upon based upon some condition. The include tag looks like:some condition. The include tag looks like:

<jsp:include page="<%= includepage %>" flush="true" /><jsp:include page="<%= includepage %>" flush="true" />

39

<jsp:param><jsp:param>

This tag specifies a name/value pair that is embedded This tag specifies a name/value pair that is embedded in (and musin (and must t appear withinappear within) ) the the fallbackfallback,, includeinclude, , andand paramsparams tags. Thetags. The paramsparams tag description tag description contains an example of the contains an example of the paramparam action action

40

<jsp:params><jsp:params>

This action, like the This action, like the fallbackfallback action, can only occur action, can only occur inside of a inside of a pluginplugin action tag. It surrounds the action tag. It surrounds the <jsp:param><jsp:param> actions inside a actions inside a pluginplugin block. For block. For example:example:

<jsp:plugin type=applet code="CalcSalary.class" ><jsp:plugin type=applet code="CalcSalary.class" >

<jsp:params><jsp:params>

<jsp:param name="id" value="101" /><jsp:param name="id" value="101" />

<jsp:param name="name" value="Tiger" /><jsp:param name="name" value="Tiger" />

</jsp:params></jsp:params>

</jsp:plugin></jsp:plugin>

41

<jsp:plugin><jsp:plugin>

This tag runs an applet or bean that may require a This tag runs an applet or bean that may require a browser extension (browser extension (pluginplugin). The JSP engine returns an ). The JSP engine returns an HTML tag ("HTML tag ("embedembed" for Internet Explorer or "" for Internet Explorer or "objectobject" " for Netscape). A number of attributes specify the for Netscape). A number of attributes specify the plugin name, the type (bean or applet), the size of the plugin name, the type (bean or applet), the size of the display window, the directory that contains the plugin, display window, the directory that contains the plugin, and so on. Here is a short example: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>

</jsp:plugin></jsp:plugin>

42

<jsp:setProperty><jsp:setProperty>

This tag, like the This tag, like the getPropertygetProperty tag, works with beans. tag, works with beans. It assigns the property of an existing bean object. The It assigns the property of an existing bean object. The object must be instantiated before you call the object must be instantiated before you call the setPropertysetProperty tag. Here is an example that sets the tag. Here is an example that sets the value property of the value property of the newItemnewItem object to " object to "HarryHarry":":

<jsp:setProperty name="newItem" property="value"<jsp:setProperty name="newItem" property="value"

value="Harry" />value="Harry" />

43

<jsp:useBean><jsp:useBean>

This tag allows you to make a Java class available This tag allows you to make a Java class available inside the JSP file. You can pass attributes to the object inside the JSP file. You can pass attributes to the object to alter its functionality and define its use.to alter its functionality and define its use.

For example:For example:

<jsp:useBean id="fileBean" scope="page"<jsp:useBean id="fileBean" scope="page" class="oracle.jsp.webutil.fileaccess.HttpUploadBean"> class="oracle.jsp.webutil.fileaccess.HttpUploadBean"> </jsp:useBean> </jsp:useBean>

44

An Action Tag ExampleAn Action Tag ExampleHere is an example of a JSP action tag used to specify Here is an example of a JSP action tag used to specify an HTML form that is processed by the Struts an HTML form that is processed by the Struts controller:controller:

<html:form action="/Dept.jsp.do"><html:form action="/Dept.jsp.do">

45

Summary of JSP Tag DelimitersSummary of JSP Tag DelimitersThe following summarizes the tag format shown in this The following summarizes the tag format shown in this section:section:

Type of TagType of Tag FormatFormatActionsActions <prefix:action_name /><prefix:action_name />

DeclarationDeclaration <%! %><%! %>

DirectiveDirective <%@ directive_name %><%@ directive_name %>

ExpressionExpression <%= %><%= %>

Page commentPage comment <%-- --%><%-- --%>

ScripletScriplet <% %><% %>

46

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

47

An Action Tag ExampleAn Action Tag Example JSP (LocEdit.jsp)

<%@ taglib uri="/webapp/DataTags.tld" prefix="jbo" %>

<jbo:ApplicationModule definition="LocJSP.Locbc4jModule" id="locAM" releasemode="Stateful" />

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>

. . .

Class Library JAR (datatags.jar)