Launch your tech mastery with us—your coding journey starts now!
Course Content
Advanced Java

Elements involved with JSP development:

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 %>

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/>

<%

out.println(“Your IP address is ” + request.getRemoteAddr());%>

</body></html>

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 of JSP Declarations:

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

Following is the simple example for JSP Declarations:

<%! int i = 0; %> 

<%! int a, b, c; %> 

<%! Circle a = new Circle(2.0); %> 

<%! public String sayHello()

{

Return “Hello!!”;

}

 %> 

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 %>

Following is the simple example for JSP Expression:

<html> 

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

<body>

<p>

   Today’s date: <%= new java.util.Date()%>

</p>

</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” part of your JSP page.

Following is the syntax of JSP comments:

<%– This is JSP comment –%>

Following is the simple example for 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> 

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:

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.