0% found this document useful (0 votes)
2 views11 pages

JSP Syntax Basics (Advanced Java)

The document provides an overview of JSP syntax, including scriptlets, declarations, expressions, comments, directives, actions, implicit objects, control-flow statements, and operators. It explains how to use these elements in JSP development with examples for each type. Additionally, it outlines the types of literals supported in JSP expression language.

Uploaded by

sekhon85578
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

JSP Syntax Basics (Advanced Java)

The document provides an overview of JSP syntax, including scriptlets, declarations, expressions, comments, directives, actions, implicit objects, control-flow statements, and operators. It explains how to use these elements in JSP development with examples for each type. Additionally, it outlines the types of literals supported in JSP expression language.

Uploaded by

sekhon85578
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Jsp syntax basics

Syntax in JSP. We will understand the basic use of simple syntax


(i.e, elements) involved with JSP development.

Elements of JSP
The elements of JSP have been described below −

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 the 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 −

<html>
<head><title>Hello World</title></head>

<body>
Hello World!<br/>
<%
[Link]("Your IP address is " +
[Link]());
%>
</body>
</html>
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 the above code in JSP file [Link] and put this file
in C:\apache-tomcat7.0.2\webapps\ROOT directory. Browse through
the same using URL [Link] The above code
will generate the 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 for JSP Declarations −

<%! declaration; [ declaration; ]+ ... %>

You can write the XML equivalent of the above syntax as follows

<jsp:declaration>
code fragment
</jsp:declaration>

Following is an 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 the XML equivalent of the above syntax as follows

<jsp:expression>
expression
</jsp:expression>

Following example shows a JSP Expression −

<html>
<head><title>A Comment Test</title></head>

<body>
<p>Today's date: <%= (new
[Link]()).toLocaleString()%></p>
</body>
</html>

The above code will generate the 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", a part of your JSP page.

Following is the syntax of the JSP comments −

<%-- This is JSP comment --%>

Following example shows the JSP Comments −

<html>
<head><title>A Comment Test</title></head>

<body>
<h2>A Test of Comments</h2>
<%-- This comment will not be visible in the page
source --%>
</body>
</html>

The above code will generate the 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 −

[Link]. Syntax & Purpose

<%-- comment --%>


1
A JSP comment. Ignored by the JSP engine.

<!-- comment -->


2
An HTML comment. Ignored by the browser.

<\%
3
Represents static <% literal.

%\>
4
Represents static %> literal.
\'
5
A single quote in an attribute that uses single quotes.

\"
6
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 −

[Link]. Directive & Description

<%@ page ... %>


1 Defines page-dependent attributes, such as scripting language, error page,
and buffering requirements.

<%@ include ... %>


2
Includes a file during the translation phase.

<%@ taglib ... %>


3
Declares a tag library, containing custom actions, used in the page

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. Following


table lists out the available JSP Actions −
[Link]. Syntax & Purpose

jsp:include
1
Includes a file at the time the page is requested.

jsp:useBean
2
Finds or instantiates a JavaBean.

jsp:setProperty
3
Sets the property of a JavaBean.

jsp:getProperty
4
Inserts the property of a JavaBean into the output.

jsp:forward
5
Forwards the requester to a new page.

jsp:plugin
6 Generates browser-specific code that makes an OBJECT or EMBED tag for
the Java plugin.

jsp:element
7
Defines XML elements dynamically.

jsp:attribute
8
Defines dynamically-defined XML element's attribute.

jsp:body
9
Defines dynamically-defined XML element's body.

jsp:text
10
Used to write template text in JSP pages and documents.

JSP Implicit Objects

JSP supports nine automatically defined variables, which are also


called implicit objects. These variables are −

[Link]. Object & Description

request
1
This is the HttpServletRequest object associated with the request.

2 response
This is the HttpServletResponse object associated with the response to the
client.

out
3
This is the PrintWriter object used to send output to the client.

session
4
This is the HttpSession object associated with the request.

application
5
This is the ServletContext object associated with the application context.

config
6
This is the ServletConfig object associated with the page.

pageContext
7 This encapsulates use of server-specific features like higher
performance JspWriters.

page
8 This is simply a synonym for this, and is used to call the methods defined
by the translated servlet class.

Exception
9 The Exception object allows the exception data to be accessed by
designated JSP.

Control-Flow Statements

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
the Scriptlet tags.

<%! int day = 3; %>


<html>
<head><title>IF...ELSE Example</title></head>

<body>
<% if (day == 1 || day == 7) { %>
<p> Today is weekend</p>
<% } else { %>
<p> Today is not weekend</p>
<% } %>
</body>
</html>

The above code will generate the following result −

Today is not weekend

Now look at the following switch...case block which has been written
a bit differentlty using [Link]() and inside Scriptletas −

<%! int day = 3; %>


<html>
<head><title>SWITCH...CASE Example</title></head>

<body>
<%
switch(day) {
case 0:
[Link]("It\'s Sunday.");
break;
case 1:
[Link]("It\'s Monday.");
break;
case 2:
[Link]("It\'s Tuesday.");
break;
case 3:
[Link]("It\'s Wednesday.");
break;
case 4:
[Link]("It\'s Thursday.");
break;
case 5:
[Link]("It\'s Friday.");
break;
default:
[Link]("It's Saturday.");
}
%>
</body>
</html>
The above code will generate the 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; %>


<html>
<head><title>FOR LOOP Example</title></head>

<body>
<%for ( fontSize = 1; fontSize <= 3; fontSize++){
%>
<font color = "green" size = "<%= fontSize
%>">
JSP Tutorial
</font><br />
<%}%>
</body>
</html>

The above code will generate the following result −

JSP Tutorial

JSP Tutorial

JSP Tutorial
Above example can be written using the while loop as follows −

<%! int fontSize; %>


<html>
<head><title>WHILE LOOP Example</title></head>

<body>
<%while ( fontSize <= 3){ %>
<font color = "green" size = "<%= fontSize
%>">
JSP Tutorial
</font><br />
<%fontSize++;%>
<%}%>
</body>
</html>

The above code will generate the following result −

JSP Tutorial

JSP Tutorial

JSP Tutorial

JSP Operators

JSP supports all the logical and arithmetic operators supported by


Java. Following table lists out 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 () [] . (dot operator) 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

You might also like