0% found this document useful (0 votes)
8 views20 pages

Chapter 2 JSP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views20 pages

Chapter 2 JSP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Welcome to JSP

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
Faculty of Engineering & Computer Science

Department: Information Technology

Course: Java Server Page(JSP)

Semester: Seven

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
Chapter 2: JSP scripting

elements

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
Content of this chapter

2.1 JSP Scripting elements

2.2 JSP expression tag

2.3 JSP Declaration Tag

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
2.1 JSP Scripting elements

The scripting elements provides the ability to insert java


code inside the jsp.

There are three types of scripting elements:

A.scriptlet tag

B.expression tag

C.declaration tag

Copyright © 2018 Pearson Education,

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Inc.
2.1 JSP Scripting elements
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>

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
2.1 JSP Scripting elements
Example of JSP scriptlet tag that prints the user
name

In this example, we have created two files index.html and


welcome.jsp.

The index.html file gets the username from the user and
the welcome.jsp file prints the username with the
welcome message.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
2.1 JSP Scripting elements
File: index.html

•<html> <body>

•<form action="welcome.jsp">

•<input type="text" name="uname">

•<input type="submit" value="go"><br/>

•</form>

•</body>

•</html>

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
2.1 JSP Scripting elements

File: welcome.jsp

•<html> <body>

•<%

•String name=request.getParameter("uname");

•out.print("welcome "+name);

•%>

•</form>

•</body> </html>

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
2.2 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

In this example of jsp expression tag, we are simply


displaying a welcome message.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
2.2 JSP expression tag
<html>

<body>

<%= "welcome to jsp" %>

</body>

</html>

Note: Do not end your statement with semicolon in case


of expression tag.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
2.2 JSP expression tag
Example of JSP expression tag that prints current time

To display the current time, we have used the getTime() method of


Calendar class. The getTime() is an instance method of Calendar class,
so we have called it after getting the instance of Calendar class by the
getInstance() method.

index.jsp

<html> <body>

Current Time:<%= java.util.Calendar.getInstance().getTime() >

</body> </html>

Copyright © 2018 Pearson Education,

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Inc.
2.2 JSP expression tag
In this example, we are printing the username using the
expression tag. The index.html file gets the username and sends
the request to the welcome.jsp file, which displays the
username.

File: index.jsp

•<html> <body>

•<form action="welcome.jsp">

•<input type="text" name="uname"><br/>

•<input type="submit" value="go"></form></body>

•</html>

Copyright © 2018 Pearson Education,


Instructor: Ibrahim Yusuf Mohamed(Bulbul)
Inc.
2.2 JSP expression tag
File: welcome.jsp

•<html>

•<body>

•<%= "Welcome "+request.getParameter("uname") %>

•</body>

•</html>

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
2.3 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 %>

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
2.3 JSP Declaration Tag
Difference between JSP Scriptlet tag and
Declaration tag
Jsp Scriptlet Tag Jsp Declaration Tag

The jsp scriptlet tag can The jsp declaration tag can
only declare variables not declare variables as well as
methods. methods.

The declaration of scriptlet The declaration of jsp


tag is placed inside the declaration tag is placed
_jspService() method. outside the _jspService()
method.
Copyright © 2018 Pearson Education,

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Inc.
2.3 JSP Declaration Tag
In this example of JSP declaration tag, we are declaring the field and
printing the value of the declared field using the jsp expression tag.

index.jsp

<html>

<body>

<%! int data=50; %>

<%= "Value of the variable is:"+data %>

</body> </html>

Copyright © 2018 Pearson Education,

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Inc.
2.3 JSP Declaration Tag

Example of JSP declaration tag that declares method

In this example of JSP declaration tag, we are defining the


method which returns the cube of given number and calling this
method from the jsp expression tag.

But we can also use jsp scriptlet tag to call the declared method.

index.jsp

•<html>

•<body>

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
2.3 JSP Declaration Tag
• <%!

• int cube(int n){

• return n*n*n*;

•}

• %>

• <%= "Cube of 3 is:"+cube(3) %>

• </body>

• </html>

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
That is End

Any Questions
Thanks For your Listening

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.

You might also like