JSP - SYNTAX. Any text, HTML tags, or JSP elements you write must be outside the scriptlet. Following is the simple and first example for JSP:

Similar documents
Database Systems Lab. 11. JSP I 충남대학교컴퓨터공학과 데이타베이스시스템연구실

JSP - ACTIONS. There is only one syntax for the Action element, as it conforms to the XML standard:

DEZVOLTAREA APLICATIILOR WEB LAB 4. Lect. Univ. Dr. Mihai Stancu

JavaServer Pages (JSP)

SNS COLLEGE OF ENGINEERING, Coimbatore

Java Server Pages JSP

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer JSP

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer JSP

01KPS BF Progettazione di applicazioni web

JavaServer Pages. What is JavaServer Pages?

PSD1B Advance Java Programming Unit : I-V. PSD1B- Advance Java Programming

COMP9321 Web Application Engineering

A Gentle Introduction to Java Server Pages

Java Server Page (JSP)

CSc31800: Internet Programming, CS-CCNY, Spring 2004 Jinzhong Niu May 9, JSPs 1

COMP9321 Web Application Engineering

JSP. Basic Elements. For a Tutorial, see:

Fast Track to Java EE 5 with Servlets, JSP & JDBC

Java Server Pages. JSP Part II

COMP9321 Web Application Engineering

Unit 5 JSP (Java Server Pages)

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

JAVA 2 ENTERPRISE EDITION (J2EE)

COMP201 Java Programming

Introduction to Java Server Pages. Enabling Technologies - Plug-ins Scripted Pages

a. Jdbc:ids://localhost:12/conn?dsn=dbsysdsn 21. What is the Type IV Driver URL? a. 22.

CHAPTER 1. Core Syntax Reference

Java Server Pages. Copyright , Xiaoping Jia. 7-01/54

Java E-Commerce Martin Cooke,

ADVANCED JAVA COURSE CURRICULUM

Sun Sun Certified Web Component Developer for J2EE 5 Version 4.0

Enterprise Computing with Java MCA-305 UNIT II. Learning Objectives. JSP Basics. 9/17/2013MCA-305, Enterprise Computing in Java

Session 21. Expression Languages. Reading. Java EE 7 Chapter 9 in the Tutorial. Session 21 Expression Languages 11/7/ Robert Kelly,

JSP MOCK TEST JSP MOCK TEST IV

CE212 Web Application Programming Part 3

Unit 4 Java Server Pages

Fast Track to Java EE

Java Server Pages(JSP) Unit VI

directive attribute1= value1 attribute2= value2... attributen= valuen %>

C H A P T E RJSP.2. JSP.2.1 Syntax of expressions in JSP pages: ${} vs #{}

Trabalhando com JavaServer Pages (JSP)

DVS WEB INFOTECH DEVELOPMENT TRAINING RESEARCH CENTER

This course is intended for Java programmers who wish to write programs using many of the advanced Java features.

1. Introduction. 2. Life Cycle Why JSP is preferred over Servlets? 2.1. Translation. Java Server Pages (JSP) THETOPPERSWAY.

112. Introduction to JSP

Introduction to JSP and Servlets Training 5-days

Ch04 JavaServer Pages (JSP)

Trabalhando com JavaServer Pages (JSP)

JavaServer Pages. Juan Cruz Kevin Hessels Ian Moon

GUJARAT TECHNOLOGICAL UNIVERSITY

112-WL. Introduction to JSP with WebLogic

Servlet and JSP Review

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM Advanced Internet Technology Lab.

JSP CSCI 201 Principles of Software Development

First Simple Interactive JSP example

Advantage of JSP over Servlet

About the Authors. Who Should Read This Book. How This Book Is Organized

A JavaBean is a class file that stores Java code for a JSP

Advance Java. Configuring and Getting Servlet Init Parameters per servlet

Peers Techno log ies Pv t. L td. Core Java & Core Java &Adv Adv Java Java

JSP: Servlets Turned Inside Out

Java4340r: Review. R.G. (Dick) Baldwin. 1 Table of Contents. 2 Preface

CSC Web Programming. Introduction to JavaScript

LTBP INDUSTRIAL TRAINING INSTITUTE

Experiment No: Group B_2

RTL Reference 1. JVM. 2. Lexical Conventions

Database. Request Class. jdbc. Servlet. Result Bean. Response JSP. JSP and Servlets. A Comprehensive Study. Mahesh P. Matha

UNIT -5. Java Server Page

Basic Principles of JSPs

PES INSTITUTE OF TECHNOLOGY, SOUTH CAMPUS DEPARTMENT OF MCA INTERNAL TEST (SCHEME AND SOLUTION) II

JSP Scripting Elements

Chapter 10 Servlets and Java Server Pages

A.1 JSP A.2 JSP JSP JSP. MyDate.jsp page contenttype="text/html; charset=windows-31j" import="java.util.calendar" %>

COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY- VIRUDHUNAGAR

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

AN ISO 9001:2008 CERTIFIED COMPANY ADVANCED. Java TRAINING.

JSP MOCK TEST JSP MOCK TEST III

More JSP. Advanced Topics in Java. Khalid Azim Mughal Version date: ATIJ More JSP 1/42

Advanced Java Programming

20/08/56. Java Technology, Faculty of Computer Engineering, KMITL 1

Session 11. Expression Language (EL) Reading

Modernizing Java Server Pages By Transformation. S h a n n o n X u T h o m a s D e a n Q u e e n s U n i v e r s i t y

Java Server Pages, JSP

LTBP INDUSTRIAL TRAINING INSTITUTE

Unit 4. CRM - Web Marketing 4-1

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Server and WebLogic Express

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Core Java. Basics of Java. String Handling. OOPS Concepts. Exception Handling

Writing Servlets and JSPs p. 1 Writing a Servlet p. 1 Writing a JSP p. 7 Compiling a Servlet p. 10 Packaging Servlets and JSPs p.

Adv. Web Technology 3) Java Server Pages

Integrating Servlets and JavaServer Pages Lecture 13

JSP source code runs on the web server via JSP Servlet Engine. JSP files are HTML files with special Tags

sessionx Desarrollo de Aplicaciones en Red EL (2) EL (1) Implicit objects in EL Literals José Rafael Rojano Cáceres

Servlets and JSP (Java Server Pages)

CISH-6510 Web Application Design and Development. JSP and Beans. Overview

Java Programming Course Overview. Duration: 35 hours. Price: $900

Transcription:

http://www.tutorialspoint.com/jsp/jsp_syntax.htm JSP - SYNTAX Copyright tutorialspoint.com This tutorial will give basic idea on simple syntax ie. elements involved with JSP development: The Scriptlet: A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language. Following is the syntax of Scriptlet: <% code fragment %> You can write XML equivalent of the above syntax as follows: <jsp:scriptlet> code fragment </jsp:scriptlet> Any text, HTML tags, or JSP elements you write must be outside the scriptlet. Following is the simple and first example for JSP: <head><title>hello World</title></head> Hello World!<br/> <% out.println("your IP address is " + request.getremoteaddr()); %> NOTE: Assuming that Apache Tomcat is installed in C:\apache-tomcat-7.0.2 and your environment is setup as per environment setup tutorial. Let us keep above code in JSP file hello.jsp and put this file in C:\apache-tomcat- 7.0.2\webapps\ROOT directory and try to browse it by giving URL http://localhost:8080/hello.jsp. This would generate following result: JSP Declarations: A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file. Following is the syntax of JSP Declarations: <%! declaration; [ declaration; ]+... %> You can write XML equivalent of the above syntax as follows:

<jsp:declaration> code fragment </jsp:declaration> Following is the simple example for JSP Declarations: <%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %> JSP Expression: A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML, in a JSP file. The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression. Following is the syntax of JSP Expression: <%= expression %> You can write XML equivalent of the above syntax as follows: <jsp:expression> expression </jsp:expression> Following is the simple example for JSP Expression: <head><title>a Comment Test</title></head> <p> Today's date: <%= (new java.util.date()).tolocalestring()%> </p> This would generate following result: Today's date: 11-Sep-2010 21:24:25 JSP Comments: JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful when you want to hide or "comment out" part of your JSP page. Following is the syntax of JSP comments: <%-- This is JSP comment --%> Following is the simple example for JSP Comments: <head><title>a Comment Test</title></head>

<h2>a Test of Comments</h2> <%-- This comment will not be visible in the page source --%> This would generate following result: A Test of Comments There are a small number of special constructs you can use in various cases to insert comments or characters that would otherwise be treated specially. Here's a summary: Syntax Purpose <%-- comment --%> A JSP comment. Ignored by the JSP engine. <!-- comment --> An HTML comment. Ignored by the browser. <\% Represents static <% literal. %\> Represents static %> literal. \' A single quote in an attribute that uses single quotes. \" A double quote in an attribute that uses double quotes. JSP Directives: A JSP directive affects the overall structure of the servlet class. It usually has the following form: <%@ directive attribute="value" %> There are three types of directive tag: Directive <%@ page... %> <%@ include... %> <%@ taglib... %> Description Defines page-dependent attributes, such as scripting language, error page, and buffering requirements. Includes a file during the translation phase. Declares a tag library, containing custom actions, used in the page We would explain JSP directive in separate chapter JSP - Directives JSP Actions: JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin. There is only one syntax for the Action element, as it conforms to the XML standard: <jsp:action_name attribute="value" /> Action elements are basically predefined functions and there are following JSP actions available:

Syntax jsp:include jsp:usebean jsp:setproperty jsp:getproperty jsp:forward jsp:plugin jsp:element jsp:attribute jsp:body jsp:text Purpose Includes a file at the time the page is requested Finds or instantiates a JavaBean Sets the property of a JavaBean Inserts the property of a JavaBean into the output Forwards the requester to a new page Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin Defines XML elements dynamically. Defines dynamically defined XML element's attribute. Defines dynamically defined XML element's body. Use to write template text in JSP pages and documents. We would explain JSP actions in separate chapter JSP - Actions JSP Implicit Objects: JSP supports nine automatically defined variables, which are also called implicit objects. These variables are: Objects request response out session application config pagecontext page Exception Description This is the HttpServletRequest object associated with the request. This is the HttpServletResponse object associated with the response to the client. This is the PrintWriter object used to send output to the client. This is the HttpSession object associated with the request. This is the ServletContext object associated with application context. This is the ServletConfig object associated with the page. This encapsulates use of server-specific features like higher performance JspWriters. This is simply a synonym for this, and is used to call the methods defined by the translated servlet class. The Exception object allows the exception data to be accessed by designated JSP. We would explain JSP Implicit Objects in separate chapter JSP - Implicit Objects. Control-Flow Statements: JSP provides full power of Java to be embedded in your web application. You can use all the APIs and building blocks of Java in your JSP programming including decision making statements, loops etc.

Decision-Making Statements: The if...else block starts out like an ordinary Scriptlet, but the Scriptlet is closed at each line with HTML text included between Scriptlet tags. <%! int day = 3; %> <head><title>if...else Example</title></head> <% if (day == 1 day == 7) { %> <p> Today is weekend</p> <% } else { %> <p> Today is not weekend</p> <% } %> This would produce following result: Today is not weekend Now look at the following switch...case block which has been written a bit differentlty using out.println and inside Scriptletas: <%! int day = 3; %> <head><title>switch...case Example</title></head> <% switch(day) { case 0: out.println("it\'s Sunday."); case 1: out.println("it\'s Monday."); case 2: out.println("it\'s Tuesday."); case 3: out.println("it\'s Wednesday."); case 4: out.println("it\'s Thursday."); case 5: out.println("it\'s Friday."); default: out.println("it's Saturday."); } %> This would produce following result: It's Wednesday. Loop Statements:

You can also use three basic types of looping blocks in Java: for, while,and do while blocks in your JSP programming. Let us look at the following for loop example: <%! int fontsize; %> <head><title>for LOOP Example</title></head> <%for ( fontsize = 1; fontsize <= 3; fontsize++){ %> <font color="green" size="<%= fontsize %>"> </font><br /> <%}%> This would produce following result: Above example can be written using while loop as follows: <%! int fontsize; %> <head><title>while LOOP Example</title></head> <%while ( fontsize <= 3){ %> <font color="green" size="<%= fontsize %>"> </font><br /> <%fontsize++;%> <%}%> This would also produce following result: JSP Operators: JSP supports all the logical and arithmetic operators supported by Java. Following table give a list of all the operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Category Operator Associativity Postfix []. dotoperator Left to right Unary ++ - -! ~ Right to left Multiplicative * / % Left to right Additive + - Left to right Shift >> >>> << Left to right Relational > >= < <= Left to right Equality ==!= Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR Left to right Logical AND && Left to right Logical OR Left to right Conditional?: Right to left Assignment = += -= *= /= %= >>= <<= &= ^= = Right to left Comma, Left to right JSP Literals: The JSP expression language defines the following literals: Boolean: true and false Integer: as in Java Floating point: as in Java String: with single and double quotes; " is escaped as \", ' is escaped as \', and \ is escaped as \\. Null: null Loading [MathJax]/jax/output/HTML-CSS/jax.js