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.

Slides:



Advertisements
Similar presentations
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
Advertisements

Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
The Web Warrior Guide to Web Design Technologies
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
DT211/3 Internet Application Development
CS320 Web and Internet Programming JSP Scripting Elements and Page Directives Chengyu Sun California State University, Los Angeles.
DT228/3 Web Development JSP: Directives and Scripting elements.
Java Server Pages B.Ramamurthy. Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content.
Java Server Pages Russell Beale. What are Java Server Pages? Separates content from presentation Good to use when lots of HTML to be presented to user,
Comp2513 Java Server Pages Daniel L. Silver, Ph.D.
1 CS6320 – JSP L. Grewe 2 Java Server Pages Servlets require you to write out entire page delivered with print statements Servlets require you to write.
Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Slides © Marty Hall, book © Sun Microsystems Press 1 Including Files & Applets in JSP Documents Core Servlets & JSP book:
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Introduction to Java Server Pages (JSPs) Robert Thornton.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages (JSP)
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
Web Technologies Java Beans & JSP By Praveen Kumar G.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
DT228/3 Web Development JSP: Actions elements and JSTL.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
JSP in Action. JSP Standard Actions forward, include, useBean, setProperty, getProperty, text, element, and plugin Additional actions param, params,
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
JSP: Actions elements and JSTL
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
Java Server Pages.
Java Server Pages By: Tejashri Udavant..
Scripted Page Web App Development (Java Server Pages)
Including Files and Applets in JSP Pages
Java Server Pages B.Ramamurthy.
Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph’s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction.
Scripted Page Web Application Development (Java Server Pages)
Presentation transcript:

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. 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 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 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 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: body of tag body of tag (this ends the tag within the starting 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 elementsScripting elements DirectivesDirectives ActionsActions

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 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 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 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 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: 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 ) { 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 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 Types of Comments You can dynamically generate an HTML comment by embedding an expression within the comment using the format: " static -->" " 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: 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 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 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(" \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 Scripting Elements in the Servlet Lines are written into the _jspService() method. Lines 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 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 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 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 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 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: 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 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 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 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 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: 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 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:

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 <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 <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 <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 <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 <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 <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 <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 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 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 Simple JSP Project Go to the Handouts links, download, and unzip the project.

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)