0% found this document useful (0 votes)
2 views10 pages

Unit 3 Chapter 2

The document provides an overview of JSP comments and scripting elements, including scriptlets, expression tags, and declarations. It explains how to use JSP comments to hide code, and details the syntax for scriptlets and expressions that allow Java code to be embedded within JSP pages. Additionally, it includes examples demonstrating the use of JSP expressions and declarations to output dynamic content and declare variables or methods.

Uploaded by

tapasyahate3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Unit 3 Chapter 2

The document provides an overview of JSP comments and scripting elements, including scriptlets, expression tags, and declarations. It explains how to use JSP comments to hide code, and details the syntax for scriptlets and expressions that allow Java code to be embedded within JSP pages. Additionally, it includes examples demonstrating the use of JSP expressions and declarations to output dynamic content and declare variables or methods.

Uploaded by

tapasyahate3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit 3

Chapter 2
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.
Following is the syntax of the JSP comments −
<%-- This is JSP comment --%>
Eg:
<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 −
Elements of JSP
The elements of JSP have been described below −

JSP Scripting elements


The scripting elements provides the ability to insert java code inside the jsp. There are three types of scripting elements:

● scriptlet tag
● expression tag
● declaration tag

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 %>
You can write the XML equivalent of the above syntax as follows −
<jsp:scriptlet>

code fragment

</jsp:scriptlet>
1. <html>
2. <body>
3. <form>
4. <%
5. String name=request.getParameter("uname");
6. out.print("welcome "+name);
7. %>
8. </form>
9. </body>
10. </html>
JSP expression tag
The code placed within JSP expression tag is written to the output stream of the response. So you need not write out.print() to write data.
It is mainly used to print the values of variable or method.

Syntax of JSP expression tag


1. <%= statement %>
2. <html>
3. <body>
4. <%= "welcome to jsp" %>
5. </body>
6. </html>

Example of JSP expression tag that prints current time


1. <html>
2. <body>
3. Current Time: <%= java.util.Calendar.getInstance().getTime() %>
4. </body>
5. </html>
A simple JSP example page example is:

home.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII" pageEncoding="US-ASCII"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html> <head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>First JSP</title> </head>
<%@ page import="java.util.Date" %>
<body> <h3>Hi Pankaj</h3><br>
<strong>Current Time is</strong>: <%=new Date() %>
</body>
</html>
test.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII" pageEncoding="US-ASCII"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Test JSP</title>
</head>
<body> Test JSP Page inside WEB-INF folder.<br> Init Param "test" value =<%=config.getInitParameter("test") %><br>
HashCode of this object=<%=this.hashCode() %>
</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 for JSP Declarations −
<%! declaration; [ declaration; ]+ ... %>
Following is an example for JSP Declarations −
<%! int i = 0; %>

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

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


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 example shows a JSP Expression −


<html>

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

<body>

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

</body>

</html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy