This tutorial give the overview of JSP syntax (Element of JSP).We will see the each in detail in upcoming chapters.
Syntax of JSP Scriptlet :
The xml equivalent of above syntax as follows
Sample scriptlet code give below
scriptlet.jsp
Output
Syntax of JSP Declaration:
The xml equivalent of above syntax as follows
Sample JSP Declaration Code :
Syntax of JSP Expression:
The xml equivalent of above syntax as follows
Sample JSP Expression code :
expression.jsp
Output
Sample JSP Comment code
comment.jsp
Output
Different type of JSP comments given below
Syntax of JSP Directive:
Three types of directive tags:
Syntax of JSP Action:
JSP actions listed below
JSP Implicit Objects
ifelse.jsp
Output
switch…case example
We will see the same example with switch statement.
switch.jsp
Output
for loop example
forloop.jsp
Output
We will see the same example with while loop.
whileloop.jsp
Output
JSP Syntax
- JSP Scriptlet
- JSP Declarations
- JSP Expressions
- JSP Comments
- JSP Directives
- JSP Actions
- JSP Implicit Objects
- JSP Control-Flow Statements
- JSP Operators
- 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 pageSyntax 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:include | Includes a file at the time the page is requested |
jsp:useBean | Finds or instantiates a JavaBean |
jsp:setProperty | Sets the property of a JavaBean |
jsp:getProperty | Inserts the property of a JavaBean into the output |
jsp:forward | Forwards the requester to a new page |
jsp:plugin | Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin |
jsp:element | Defines XML elements dynamically |
jsp:attribute | Defines dynamically defined XML element's attribute |
jsp:body | Defines dynamically defined XML element's body |
jsp:text | Use 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
|
request | This is the HttpServletRequest object associated with the request. |
response | This is the HttpServletResponse object associated with the response to the client. |
out | This is the PrintWriter object used to send output to the client. |
session | This is the HttpSession object associated with the request. |
application | This is the ServletContext object associated with application context. |
config | This is the ServletConfig object associated with the page. |
pageContext | This encapsulates use of server-specific features like higher performance JspWriters. |
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. |
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 exampleifelse.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
while loop example
1
2
3
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- Boolean
- Integer
- Floating point
- String
- Null
0 comments:
Post a Comment