jsp Java代码与html代码混合:JSP Scriptlet

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.

Syntax of Scriptlet:

[code language=”java”]<% Executable java code%>[/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”]
<H3> Sample JSP </H3>
<% myMethod();%>
[/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("<H2>Sample JSP</H2>");
myMethod();
}[/code]

An example to learn about Scripting elements:

[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>

<%–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


转载于:https://my.oschina.net/u/2308739/blog/618511

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值