topic 3 0 introduction to Javaserver pages 3


























- Slides: 26

topic 3. 0 introduction to Javaserver pages 3. 2 Use JSP in Application Prepared by : Pn. Nurul Zakiah Binti Kasnun

LEARNING OUTCOMES: At the end of this topic, students are able to: i. ii. Understand JSP tags Use JSP implicit objects

Understand JSP tags

JSP tags 1. 2. 3. 4. Scripting elements Comments Action elements Directive Elements

JSP tags : Scripting Elements ü JSP Scripting element are written inside <% %> tags. ü These code inside <% %> tags are processed by the JSP engine during translation of the JSP page. ü Any other text in the JSP page is considered as HTML code or plain text.

Example: <html> <head> <title>My First JSP Page</title> </head> <% int count = 0; %> <body> Page Count is <% out. println(++count); %> </body>

Scripting Element Example Comment <%-- comment --%> Directive <%@ directive %> Declaration <%! declarations %> Scriplet <% scriplets %> Expression <%= expression %>

JSP tags : Comments ü is used when you are creating a JSP page and want to put in comments about what you are doing. ü only seen in the JSP page. ü not included in servlet source code during translation phase, nor they appear in the HTTP response.

Example: <html> <head> <title>My First JSP Page</title> </head> <% int count = 0; %> <body> <%-- Code to show page count --%> Page Count is <% out. println(++count); %> </body>

JSP tags : Action Elements ü These action tags are used to remove or eliminate scriptlet code from your JSP page because scriplet code are technically not recommended nowadays. ü Standard tags begin with the jsp: prefix.

Action Element

Action Element (Cont…)

Action Element (Cont…) Source : https: //www. studytonight. com/jsp-action-element. php

Example: <html> <head> <title>Demo of JSP include Action Tag</title> </head> <body> <h 3>JSP page: Demo Include</h 3> <jsp: include page="sample. jsp" flush="false" /> </body> </html> Source : https: //beginnersbook. com/2013/06/jsp-tutorial-actions/

JSP tags : Directive Elements Syntax : <%@ directive attribute = "value" %> Type Description page Directive <%@ page. . . %> Defines page-dependent attributes, such as scripting language, error page, and buffering requirements. include Directive taglib Directive <%@ include. . . %> Includes a file during the translation phase. <%@ taglib. . . %> Declares a tag library, containing custom actions, used in the page Source : https: //www. tutorialspoint. com/jsp_directives. htm

Example : welcome. jsp <html> <head> <title>Welcome Page</title> </head> header. jsp <html> <body> <img src="header. jpg" alt="This is Header image" / > </body> </html> <body> <%@ include file="header. jsp" %> Welcome, User </body> </html>

Use JSP implicit objects

JSP implicit objects

JSP implicit objects Out: ü This is used for writing content to the client (browser). ü It has several methods which can be used for properly formatting output message to the browser and for dealing with the buffer.

JSP implicit objects Request: ü To get the data on a JSP page which has been entered by user on the previous JSP page. ü While dealing with login and signup forms in JSP we often prompts user to fill in those details, this object is then used to get those entered details on an another JSP page (action page) for validation and other purposes.

JSP implicit objects Response: ü It is basically used for modfying or delaing with the response which is being sent to the client(browser) after processing the request.

JSP implicit objects Session: ü It is most frequently used implicit object, which is used for storing the user’s data to make it available on other JSP pages till the user session is active.

JSP implicit objects Application: ü This is used for getting application-wide initialization parameters and to maintain useful data across whole JSP application.

Example: Other examples: https: //beginnersbook. com/2013/11/jsp-implicit-objects/

REFERENCES https: //www. studytonight. com/jsp-action-element. php https: //www. tutorialspoint. com/jsp_actions. htm https: //www. tutorialspoint. com/jsp_directives. htm https: //beginnersbook. com/2013/11/jsp-implicit-objects/

Thanks! Any questions?