Instagram Widget

JSP scripting element Example | Scriptlet Tag vs Declaration Tag vs Expression Tag | Example

In this post I am going to post an example depicting the use of JSP scripting Elements.


Scriptlet Tag vs Declaration Tag vs Expression Tag | Example

Overview

Scriptlet Tag
Scriptlet Tag is used to execute Java Code in JSP.

Expression Tag
It is mainly used to print the value of the variable or method. We don't need to use out.print.

Declaration Tag
it is used to declare variables and methods.

Output of this Example:



Welcome


Square



 index.jsp Code


<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>JSP Exmaples</title>
</head>
<body>
<h1 align="center">
<%
out.println("Welcom to JSP Page");
%>

</h1>
<hr>
<h2>Scriptlet Tag is used to execute Java Code in JSP</h2>
<h3>Example</h3>

<hr>

<form action="welcome.jsp">
<input type="text" name="user_name"> <input type="submit"
value="Submit">

</form>

<hr>

<h2>Expression Tag</h2>
<p>It is mainly used to print the value of the variable or method.
We don't need to use out.print.</p>
<h3>Example</h3>

Current Time is
<%=Calendar.getInstance().getTime()%>

<hr>

<h2>Declaration Tag</h2>

<p>it is used to declare variables and methods.</p>

<h3>Example</h3>

<form action="square.jsp">
<label> Enter a number to find square</label>

<input type="number" name="number">
<input type="submit" value="Get The Square">
</form>
<hr>
</body>
</html>


welcome.jsp Code



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome</title>
</head>
<body>

<% String name=request.getParameter("user_name");

out.println("Welcome "+name);
%>

</body>
</html>



square.jsp Code



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>

<%
String n = request.getParameter("number");
int num = Integer.parseInt(n);
%>
<meta charset="ISO-8859-1">
<title>Square of <%= num %></title>
</head>
<body>

<%!
int square(int num) {
return num*num;
}

%>

<h2> The Square of <%= num %> is <%= square(num) %></h2>

</body>
</html>


If you have issues regarding how to create JSP page in Eclipse with Tomcat follow this post

Create First JSP

Post a Comment

0 Comments