Home Tutorial Jsp JSPs : Scriptlet

 
 

JSPs : Scriptlet
Posted on: December 3, 2012 at 12:00 AM
Advertisement
This tutorial contains description of scriptlet which is one of JSP page element.

JSPs : Scriptlet

This tutorial contains description of scriptlet which is one of JSP page element.

Scriptlet :

Scriptlet is one of JSP component which contains any Java code. Scriptlet is used to generate the output dynamically. All the code inside the scriptlet goes into the _jspService() method by the JSP engine. It is executed at request processing time. You have permission to access all the variables and methods declared in declarations in scriptlet. You can use it anywhere in your JSP page.

Scriptlets are defined by using <% and %> tags.

Some variables used in JSP scriptlets are -

request : It is subclass of HttpServletRequest and shows the clients request. You can use it to retrieve the data submitted along the request.

response: It is subclass of HttpServletResponse used to response the client request.

session: It shows the HTTP session object related with the request.

out: It is an object of output stream and is used to send any output to the client.

Syntax :

<% Java code%>

Example :


<%@page import="java.io.PrintWriter"%><html>
<head>
<title>Scriptlet Example</title>
</head>
<body>

<%!int a = 10;
int b = 20;%>


<%
int sum = a + b;
PrintWriter Out = response.getWriter();
Out.println("Sum of a = " + a + " and b= " + b + " is : " + sum);
%>

</body>
</html>

Output :

Advertisement

Related Tags for JSPs : Scriptlet:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 3, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com