tutorialspoint-jsp
tutorialspoint-jsp
JSP - Syntax
Advertisements
In this chapter, we will discuss Syntax in JSP. We will understand the basic use of simple syntax (i.e,
elements) involved with JSP development.
Elements of JSP
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.
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/>
<%
%>
</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 hello.jsp and put this file in C:\apache-tomcat7.0.2\webapps\
ROOT directory. Browse through the same using URL http://localhost:8080/hello.jsp. The above code
will generate the following result −
Hello World
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.
You can write the XML equivalent of the above syntax as follows −
<jsp:declaration>
code fragment
</jsp:declaration>
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.
You can write the XML equivalent of the above syntax as follows −
<jsp:expression>
expression
</jsp:expression>
Following example shows a JSP Expression −
<html>
<body>
</body>
</html>
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.
<html>
<%-- This comment will not be visible in the page source --%>
</body>
</html>
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 −
<\%
Represents static <% literal.
%\>
\'
\"
JSP Directives
A JSP directive affects the overall structure of the servlet class. It usually has the following form −
1
<%@ page ... %>
Defines page-dependent attributes, such as scripting language, error page, and buffering requirements.
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 −
Action elements are basically predefined functions. Following table lists out the available JSP Actions −
S.No. Syntax & Purpose
jsp:include
jsp:useBean
jsp:setProperty
jsp:getProperty
jsp:forward
jsp:plugin
Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin.
jsp:element
jsp:attribute
jsp:body
10
jsp:text
JSP supports nine automatically defined variables, which are also called implicit objects. These variables
are −
request
response
This is the HttpServletResponse object associated with the response to the client.
out
session
This is the HttpSession object associated with the request.
application
config
pageContext
page
This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.
Exception
The Exception object allows the exception data to be accessed by designated JSP.
We would explain JSP Implicit Objects in a separate chapter JSP - Implicit Objects.
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.
<html>
<head><title>IF...ELSE Example</title></head>
<body>
<% } %>
</body>
</html>
Now look at the following switch...case block which has been written a bit differentlty using out.println()
and inside Scriptletas −
<html>
<head><title>SWITCH...CASE Example</title></head>
<body>
<%
switch(day) {
case 0:
out.println("It\'s Sunday.");
break;
case 1:
out.println("It\'s Monday.");
break;
case 2:
out.println("It\'s Tuesday.");
break;
case 3:
out.println("It\'s Wednesday.");
break;
case 4:
out.println("It\'s Thursday.");
break;
case 5:
out.println("It\'s Friday.");
break;
default:
out.println("It's Saturday.");
%>
</body>
</html>
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.
<html>
<body>
JSP Tutorial
</font><br />
<%}%>
</body>
</html>
JSP Tutorial
JSP Tutorial
JSP Tutorial
<body>
JSP Tutorial
</font><br />
<%fontSize++;%>
<%}%>
</body>
</html>
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.
JSP Literals
Integer − as in Java
String − with single and double quotes; " is escaped as \", ' is escaped as \', and \ is escaped as \\.
Null − null
Advertisements
Tutorials Point