java script与jsp_jsp Java代码与html代码混合:JSP Scriptlet

JSP Scriptlets是在JSP页面中嵌入的Java代码,它们在生成Servlet时被复制到_jspService()方法中。每个客户端请求都会调用_jspService(),因此其中的代码会为每个请求执行。本文介绍了JSP Scriptlets的基本语法,包括如何使用它们来实现条件控制语句如if-else,并强调了在JSP中使用Scriptlets执行Java代码的优势。
摘要由CSDN通过智能技术生成

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: Welcome

Welcome 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 PAGE

5) { %>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值