This tutorial contains description of scriptlet which is one of JSP page element.
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.
<% Java code%>
<%@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>
Posted on: December 3, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles