Scriptlets are nothing but java code enclosed within <% and %> 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.html
Syntax of Scriptlet:java
[code language=」java」]<% Executable java code%>[/code]web
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.session
Sample JSP code:app
[code language=」html」]
<H3> Sample JSP </H3>
<% myMethod();%>
[/code]less
Note: Semicolon at the end of scriptlet.jsp
Corresponding translated Servlet code for above JSP code:ide
[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("<H2>Sample JSP</H2>");
myMethod();
}[/code]this
[code language=」java」]
<%– A jsp example to learn the JSP scripting elements–%>
<%String string1 ="JSP scriptlet";%>
<%!String string2 ="";%>
<html>
<head>
<title> JSP page: Welcome </title>
</head>
<body>
<h1>
<%–This is an Expression statement–%>
Welcome to <%=string1%>
</h1>spa
<%–sciptlet example–%>
<%if(localstring.equals("JSP scriptlet")){%>
Hi
<%}
else {%>
hello
<%} %>
<%–same thing can be done in this way also–%>
<%if(localstring.equals("JSP scriptlet"))
out.println("Hi"+string2);
else
out.println("hello");
%>
</body>
</html>[/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」]
<%if(localstring.equals("JSP scriptlet"))
out.println("Hi"+string2);
else
out.println("hello");
%>[/code]
The above code is a JSP scriptlet (notice starting <% and ending %> 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」]<%String string1 ="JSP scriptlet";%>[/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」]
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.0 translation //EN">
<HTML>
<HEAD>
<TITLE> MY JSP PAGE </TITLE>
</HEAD>
<BODY>
<% if (num > 5) { %>
<H3> hi </H3>
<%} else {%>
<h3> num value should not be less than 6 </h3>
<% } %>
</BODY>
</HTML>
[/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