Jsp Scriptlet

Jsp Scriptlet


Posted in : Java Posted on : January 23, 2012 at 7:02 PM Comments : [ 0 ]

In this tutorial you will learn about the JSP Scriptlet.

Jsp Scriptlet

In this tutorial you will learn about the JSP Scriptlet.

Scriptlet in JSP contains the Java code. These java codes can be written inside the scriptlets in different fragments. Java codes written in different fragments are executed as the code you have written in the service method of a servlet because the code you have written inside the scriptlets the web server keeps this code inside the service method.

Syntax

<% code %>

Here 'code' is a java code that is written inside the <%  %> scriptlet.

Here I am giving a simple example which will demonstrate you to how can you write your java code inside the scriptlet.

Example :

sciptlet.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 Example</title>
</head>
<body>
<form>
Enter the last number till you want to print :
<input type="text" name="text" />
<input type="submit" value="submit" />
</form>
<%! int num = 0; %>
<%
if (request.getParameter("text") != null)
{
num = Integer.parseInt(request.getParameter("text"));
}
%>
<table>
<tr><td>Number</td></tr>
<%
for (int i = 0; i < num+1; i++)
{
%>
<tr>
<td align="right"><% out.print(i); %></td>
</tr>
<%
}
%>

</table>
</body>
</html>

Output :

When you will execute this example output will be as following :

1. A page will be displayed with containing text box where you will have to enter the value

2. When you will enter the value in text box and after clicking on submit button a list of number till you have entered in text box will be displayed to you.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics