Lecture 44

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Web Technology

(KCS-602)
Unit 5
Scripting, Standard Actions

Prepared By
Sachin Kumar Sonker
Assistant Professor,UCER Naini,Allahabad
Index
• JSP Scriptlet tag
• JSP Implicit object
JSP Scriptlet tag (Scripting elements)

In JSP, java code can be written inside the jsp


page using the scriptlet tag.
JSP Scripting elements
The scripting elements provides the ability to
insert java code inside the jsp.
There are three types of scripting elements:
• scriptlet tag
• expression tag
• declaration tag
JSP scriptlet tag
A scriptlet tag is used to execute java source code in
JSP. Syntax is as follows:
<% java source code %>
Example of JSP scriptlet tag
In this example, we are displaying a welcome
message.
<html>
<body>
<% out.print("welcome to jsp"); %>
</body>
</html>
Example of JSP scriptlet tag that prints the user name
File: index.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>
JSP expression tag
The code placed within JSP expression tag is written to the
output stream of the response.
So you need not write out.print() to write data. It is mainly used
to print the values of variable or method.
Syntax of JSP expression tag
<%= statement %>
Example of JSP expression tag
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
Note: Do not end your statement with semicolon in case of
expression tag.
Example of JSP expression tag that prints current time

index.jsp
<html>
<body>
Current Time:
<%= java.util.Calendar.getInstance().getTime() %>
</body>
</html>
JSP Declaration Tag
The JSP declaration tag is used to declare fields
and methods.
The code written inside the jsp declaration tag is
placed outside the service() method of auto
generated servlet.
So it doesn't get memory at each request.
Syntax of JSP declaration tag
The syntax of the declaration tag is as follows:
<%! field or method declaration %>
Example of JSP declaration tag that declares field
index.jsp
<html>
<body>
<%! int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>
Example of JSP declaration tag that declares method
index.jsp
<html>
<body>
<%!
int cube(int n){
return n*n*n*;
}
%>
<%= "Cube of 3 is:"+cube(3) %>
</body>
</html>
AKTU SEMESTER QUESTION
• Discuss JSP in Detail. [2019-20]
• Write a program in jsp to fetch the
details of user from html page using
tomcat server.

You might also like