We are moved to new domain
Click -> www.ehowtonow.com
Wednesday, 22 April 2015

JSP Syntax - overview

This tutorial give the overview of JSP syntax (Element of JSP).We will see the each in detail in upcoming chapters.

JSP Syntax

  1. JSP Scriptlet
  2. JSP Declarations
  3. JSP Expressions
  4. JSP Comments
  5. JSP Directives
  6. JSP Actions
  7. JSP Implicit Objects
  8. JSP Control-Flow Statements
  9. JSP Operators
  10. JSP Literals

JSP Scriptlet

Scriptlet used to write JAVA code inside JSP page.
Syntax of JSP Scriptlet :
<% JAVA code fragment %>

The xml equivalent of above syntax as follows
<jsp:scriptlet>
   JAVA code fragment
</jsp:scriptlet>

Sample scriptlet code give below

scriptlet.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Scriptlet</title>
</head>
<body>
Hello World JSP
<br/>
<%
out.println("Say Hello World");
out.println("Hello World JSP to find local IP address : Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>

Output






JSP Declarations

Declaration statements in JSP used to declare Java variables and instance of class for later usage inside the JSP page

Syntax of JSP Declaration:
<%! declaration1; declaration2; %>

The xml equivalent of above syntax as follows
<jsp:declaration>
   Java Declaration code fragment
</jsp:declaration>

Sample JSP Declaration Code :
<%! int a = 0; %> 
<%! int i, j, k; %> 
<%! JavaTutorialsCorner javaTutorialsCorner  = new JavaTutorialsCorner (1000); %>

JSP Expressions

A JSP expression is a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file.The expression can contain any expression that is valid according to the Java specification but you cannot use a semicolon to end an expression.
Syntax of JSP Expression:
<%= expression %>

The xml equivalent of above syntax as follows
<jsp:expression>
   expression
</jsp:expression>

Sample JSP Expression code :
expression.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Expression</title>
</head>
<body>
Hello World JSP
<br/>
Your IP Address is : <%= request.getRemoteAddr()%>
</body>
</html>

Output



JSP Comments

Syntax of JSP Comment:
<%-- comment --%>

Sample JSP  Comment code

comment.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Comment</title>
</head>
<body>
Hello World JSP Comment
<br/>
<%-- This JSP comment will not be visible in browser output --%> 
</body>
</html>

Output



Different type of JSP comments given below

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

JSP directives provide directions and instructions to the container, telling it how to handle of JSP processing.Directives can have a number of attributes which you can list down as key-value pairs and separated by commas.

Syntax of JSP Directive:
<%@ directive attribute="value" %>

Three types of directive tags:

Directive

Description
<%@ page ... %>Defines page-dependent attributes, such as scripting language, error page, and buffering requirements
<%@ include ... %>Includes a file during the translation phase.
<%@ taglib ... %>Declares a tag library, containing custom actions, used in the page

JSP Actions

JSP actions used to control the behavior of the servlet engine. Using JSP actions you can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plug-in.

Syntax of JSP Action:
<jsp:action_name attribute="value" />

JSP actions listed below

Syntax

 Description
jsp:includeIncludes a file at the time the page is requested
jsp:useBeanFinds or instantiates a JavaBean
jsp:setPropertySets the property of a JavaBean
jsp:getPropertyInserts the property of a JavaBean into the output
jsp:forwardForwards the requester to a new page
jsp:pluginGenerates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin
jsp:elementDefines XML elements dynamically
jsp:attributeDefines dynamically defined XML element's attribute
jsp:bodyDefines dynamically defined XML element's body
jsp:textUse to write template text in JSP pages and documents

JSP Implicit Objects

JSP supports nine predefined variables, which are also called implicit objects.

JSP Implicit Objects
Implicit Objects

Description
requestThis is the HttpServletRequest object associated with the request.
responseThis is the HttpServletResponse object associated with the response to the client.
outThis is the PrintWriter object used to send output to the client.
sessionThis is the HttpSession object associated with the request.
applicationThis is the ServletContext object associated with application context.
configThis is the ServletConfig object associated with the page.
pageContextThis encapsulates use of server-specific features like higher performance JspWriters.
pageThis is simply a synonym for this, and is used to call the methods defined by the translated servlet class.
ExceptionThe Exception object allows the exception data to be accessed by designated JSP.

JSP Control-Flow Statements

In JSP you can use the full power of Java including all the API supported by Java.Here we will see how to use control flow statement inside JSP page.

Decision Making Statement

 if…else example
ifelse.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP if…else statement</title>
</head>
<body>
    <%! int month = 3; %>
    <% if (month == 1) { %>
    <p>This is January</p>
    <% } else if (month == 2){ %>
    <p>This is February</p>
    <%} else if (month == 3){ %>
    <p>This is March</p>
    <% } %>
</body>
</html>

Output

This is March

switch…case example

We will see the same example with switch statement.
switch.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Switch Statement</title>
</head>
<body>
    <%! int month = 2; %>
    <% 
switch(month) {
case 0:
   out.println("This is January");
   break;
case 1:
   out.println("This is February");
   break;
case 2:
   out.println("This is March");
   break;
default:
   out.println("Month not matched");
}
%>
</body>
</html>

Output

This is March

Loop Statement

You can use all looping statement available in Java for, while, do…while.We will see the examples here.
for loop example
forloop.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP for loop</title>
</head>
<body>
    <%! int a; %>
    <%for ( a = 1; a <= 3; a++){ %>
    <p><%= a %></p>
    <br />
    <%}%>
</body>
</html>

Output

1
2
3


while loop example

We will see the same example with while loop.

whileloop.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP While loop</title>
</head>
<body>
    <%! int a=1; %>
    <%while ( a <= 3){ %>
    <p><%= a %></p>
    <br />
    <br />
    <%a++;%>
    <%}%>
</body>
</html>

Output

1
2
3


JSP Operators

JSP supports all the logical and arithmetic operators supported by Java.List of operators given below.

Category

Operator
Postfix () [] .
Unary++ - - ! ~
Multiplicative* / %
Additive+ -
Shift >> >>> <<
Relational> >= < <=
Equality == !=
Bitwise AND&
Bitwise XOR^
Bitwise OR|
Logical AND&&
Logical OR||
Conditional?:
Assignment= += -= *= /= %= >>= <<= &= ^= |=
Comma,

JSP Literals

Literals in JSP expression language

  1. Boolean
  2. Integer
  3. Floating point
  4. String
  5. Null

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JSP Syntax - overview Rating: 5 Reviewed By: eHowToNow