Scriptlet is one of the three JSP coding (constructs) elements and the other two being Expression and Declaration. Scriptlet includes maximum dynamic code. It is the processing zone where Programmer writes actual JSP coding. Scriptlet is almost equivalent to the service() method of a Servlet (interms of coding volume and callback). This tutorial focuses on Scriptlet.
Client-side HTML Login Validation Form
File Name: SignIn.html
Sign in
<form method="get" action="http://localhost:8888/india/Login.jsp">
In the action attribute Login.jsp file is given to invoke. Assume localhost is the server. The port number on which Tomcat is running is 8888. india is the folder created in Tomcat. The attribute method can be given two values of either get or post; here, get is given.
Following is the server-side JSP file that includes a Scriptlet to understand what and how to code Scriptlet.
File Name: Login.jsp
Log in Validation
<%
String str1=request.getParameter("t1");
String str2=request.getParameter("t2");
if(str1.equalsIgnoreCase("snrao") && str2.equals("java"))
{
out.println("Your Login is Successful
");
}
else
{
out.println("Sorry, your Login is Failed
");
}
%>
Screenshot of HTML file SignIn.html when values are entered.
Screenshot of response when correct values are entered.
Screenshot of response when wrong values are entered.
<h2 align="center">Log in Validation </h2>
The above line constitutes static code of JSP. It is nothing but simply any HTML code.
The code placed within <% and %> is known as Scriptlet. It is the actual coding arena of JSP. In the above Scriptlet, the Login values sent by the client are retrived, then validated and validation result is sent back to client as response.
String str1=request.getParameter("t1");
String str2=request.getParameter("t2");
getParameter(String) is a method of request object. The parameter t1 is nothing but the name of HTML text field (observe, client HTML form). The getParameter("t1") returns the value entered by the client in t1 field. In the Scriptlet, it is assigned to string str1. Similarly, the password entered by client in t2 is assigned to string str2. request object is used in JSP to retrieve the data sent by client.
request is one of the 9 implicit objects, the JSP container gifts to the Programmer to use directly. request represents an object of javax.servlet.http.HttpServletRequest. That is, an object of HttpServletRequest is created implicitly by JSP container and supplied to the Programmer to use in his code.
if(str1.equalsIgnoreCase("snrao") && str2.equals("java"))
It is just some validation code. Client ID is compared with snrao and password to java. snrao and java are taken as some standard values for validation. In practice, they are retrieved from many sources like database, access control lists etc.
out.println("<h3>Your Login is Successful</h3>");
out is another implicit object of JSP. out represents an object of javax.servlet.jsp.JspWriter, a subclass of java.io.Writer. JspWriter is equivalent to PrintWriter (a subclass of java.io.Writer) of a Servlet. Any data placed in println() method of out object goes to client as response.
Many ways to say what is Scriptlet
- There are three JSP coding elements – Scriptlet, Expression and Declaration. All represents the dynamic code only.
- Scriptlet carries maximum dynamic code.
- Scriptlet syntax,
<%
// dynamic processing code
%> - Or to say, <% some code %> is known as scriptlet.
- Otherway, the brackets <% and %> separates dynamic code from HTML static code.
- Scriptlet is a piece of Java code put in HTML page.
- <% some code %> represents a block Java code intended for processing. One JSP file may contain any number of Scriptlets scattered in running HTML code.
- Scriptlet can do mainly the following:
- Declare variables and objects to be used in JSP code.
- Define expressions used in JSP code.
- Wrtting some processing code to fulfil client request etc.
- When Scriptlet is translated, it goes into the service() (actually it is, _jspService()) method of generated servlet. The _jspService() method is invoked automatically when a generated servlet is loaded for execution. That is, Scriptlet code is invoked every time when the client request arrives on server.
One more simple example of Scriptlet
<%
String rad = "10"; // this you can obtain from client
int radius = Integer.parseInt(rad);
out.println("Circle Area: " + (Math.PI * Math.pow(radius, 2)));
%>
Here, radius rad is assigned a value as a string because the data received from client is always in string (retrieved with getParameter()) form. The rad is parsed to radius to be used in arithmetic operations in the Scriptlet. With out object’s println() method, the circle area is sent to client.