JSP Tutorial - JSP Syntax








Scriptlet

A JSP scriptlet can contain Java language statements, variable or method declarations, or expressions that are valid in the page scripting language.

We can use the following syntax to include Scriptlet in JSP.

<% code fragment %>

For example,

<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>

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

<jsp:scriptlet>
   code fragment
</jsp:scriptlet>

Scriptlet is like a Java code island in the JavaServer Pages. Inside the island we can write Java code.





JSP Declarations

A declaration in JSP declares one or more variables or methods that we can use in Java code later in the JSP file.

We must declare the variable or method before using it in the JSP file, like in Java code.

Following is the syntax of JSP Declarations:

<%! 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; %> 
<%! double money; %> 
<%! YourClass a = new YourClass(); %>

In the following example of JSP declaration tag, we are declaring the field and printing the value of the declared field using the jsp expression tag.

<html>
<body>

<%! int data=50; %>
<%= "Value of the variable is:"+data %>

</body>
</html>

The following example of JSP declaration tag defines the method which returns the cube of given number and calls this method from the jsp expression tag.

<html>
<body>

<%! 
int cube(int n){
return n*n*n*;
}
%>

<%= "Cube of 3 is:"+cube(3) %>

</body>
</html>




JSP Expression

A JSP expression element contains a scripting language expression that is evaluated, converted to a String.

The expression element can have any valid Java expression without a semicolon at the end.

The following code shows the syntax of JSP Expression:

<%= expression %>

In the following example of jsp expression tag, we display a welcome message.

<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>

The XML equivalent of the above syntax is as follows:

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

The following code shows how to use JSP Expression.

<html> 
<body>
<p>
   Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
</body> 
</html>

This would generate following result:

<p> Today's date: 11-Jan-2015 17:02:25 </p>

JSP Comments

The following code shows the syntax of JSP comments:

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

The following JSP has a JSP Comment.

<html> 
<body> 
<%-- This is a comment in the page.--%> 
</body> 
</html>

<!-- comment --> is an HTML comment. Ignored by the browser.

JSP Directives

A JSP directive sets the overall structure of the servlet class. It usually has the following form:

<%@ directive attribute="value" %>

There are three types of directive tag:

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

JSP Actions

JSP actions use commands in XML syntax to control the behavior of the servlet engine which controls how to generate the JavaServer Pages.

We can insert a file, use JavaBeans code, forward the user to another page, or generate HTML for the Java plugin by using the JSP Actions.

JSP Actions use the following syntax:

<jsp:action_name attribute="value" />

The following table lists the JSP actions available.

Syntax Purpose
jsp:include Includes a file
jsp:useBean 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.
jsp:attribute Defines XML element's attribute.
jsp:body Defines XML element's body.
jsp:text Write template text in JSP pages and documents.

JSP Implicit Objects

JSP supports nine implicit objects. They are listed in the following table.

Objects Description
request Represents the HttpServletRequest object associated with the request.
response Represents the HttpServletResponse object associated with the response to the client.
out References to the PrintWriter object to send output to the client.
session HttpSession object associated with the request.
application ServletContext object associated with application context.
config ServletConfig object associated with the page.
pageContext page context object.
page It is a synonym for this, and is used to call the methods in translated servlet class.
Exception Used Exception object to access the exception data.