Scriptlets are nothing but java code enclosed within tags. JSP container moves the statements enclosed in it to _jspService() method while generating servlet from JSP. The reason of copying this code to service method is: For each client’s request the _jspService() method gets invoked, hence the code inside it executes for every request made by client.
Syntax of Scriptlet:
[code language=”java”][/code]
JSP to Servlet transition for Scriptlet –
As I stated in my previous tutorials that JSP doesn’t get executed directly, it first gets converted into a Servlet and then Servlet execution happens as normal. Also, I explained in first para that while translation from JSP to servlet, the java code is copied from scriptlet to _jspService() method. Lets see how that happens.
Sample JSP code:
[code language=”html”]
Sample JSP
[/code]
Note: Semicolon at the end of scriptlet.
Corresponding translated Servlet code for above JSP code:
[code language=”java”]
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
JspWriter out = response.getWriter();
out.println("
Sample JSP
");myMethod();
}[/code]
An example to learn about Scripting elements:
[code language=”java”]
JSP page: WelcomeWelcome to
Hi
else {%>
hello
out.println("Hi"+string2);
else
out.println("hello");
%>
[/code]
In the above example there are many type of JSP elements present such as Expression, JSP comment, Declaration element etc. We will see each one of them in upcoming JSP tutorials but as of now you can only focus on Scriptlets. The below are the scriptlets statements used in above example –
[code language=”java”]
out.println("Hi"+string2);
else
out.println("hello");
%>[/code]
The above code is a JSP scriptlet (notice starting tags). If you analyze above piece of code then you would find that the code inside tags is a pure java code so in order to execute java code in JSP we use scriptlets.
[code language=”java”][/code]
Like above set of statements this statement is a java initialization code which is enclosed within tags.
Apart from above two set of scriptlets there are many other scriptlet tags present in above example (notice if-else control flow logic). To use the if-else control flow statements of java, we have used scriptlet in above example. As this is the main advantage of using scriptlet so lets make it more clear with the help of an example – You must be aware how important are our If – else control statements.
An example to show use of if -else using scriptlet –
Suppose there is a variable num and you want to display “hi” on your webpage if it is greater than 5 otherwise you wanna display a message. Consider the below code for this scenario –
If you wanna write a code in java for above situation then it would look like this –
[code language=”java”]
if (num > 5)
{
out.println("hi");
}
else
{
out.println("num value should not be less than 6");
}[/code]
To write the similar code in JSP we need to use JSP scriptlets – Code would be like this –
[code language=”html”]
MY JSP PAGE5) { %>
hi
num value should not be less than 6
[/code]
Important Point to remember:Since the code inside it is a java code it must end with a semicolon(;).Now notice all the statements – you may find thatall few scriptlet where we give semicolon in java, needs it here too and ends with a semicolon.
具体语法可以参考
http://www.tutorialspoint.com/jsp/jsp_syntax.htmhttp://www.tutorialspoint.com/jsp/jsp_syntax.htm