0% found this document useful (0 votes)
20 views

Java Unit 3 Lecture 29

Kushagra Agrawal

Uploaded by

manudev8924
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java Unit 3 Lecture 29

Kushagra Agrawal

Uploaded by

manudev8924
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

UNIVERSITY INSTITUTE OF ENGINEERING

DEPARTMENT OF AIT - CSE


Bachelor of Engineering (CSE)
Programming in Java (21CSH-244)
By: Kushagra Agrawal (E13465)

Lecture - 29 DISCOVER . LEARN . EMPOWER


JAVA SERVER PAGES
Chapter Course Objectives

● To understand about Java Server Pages (JSP).


● To understand about JSP Architecture.
19.
● To understand about JSP Life Cycle.

Chapter Course Outcomes

After completion of this course, student will be able to


● Learn the basic JSP and its Architecture.
19. ● Learn about JSP Life Cycle.

2
JAVA SERVER PAGE (JSP)
What is JSP?
• Java Server Pages (JSP) is a technology which is used to develop web
pages by inserting Java code into the HTML pages by making special JSP
tags.
• The JSP tags which allow java code to be included into it are <% ----java
code----%>.
• It is a server side technology.
• It is used for creating web application.
• It is used to create dynamic web content.
• It is an advanced version of Servlet Technology.
• It is a Web based technology helps us to create dynamic and platform
independent web pages.
• In this, Java code can be inserted in HTML/ XML pages or both.
• JSP is first converted into servlet by JSP container before processing the
client’s request.
Servlet Vs JSP

Servlets – JSP –
• Servlet is a Java program which • JSP program is a HTML code which
supports HTML tags too. supports java statements too.To be
• Generally used for developing more precise, JSP embed java in
business layer(the complex html using JSP tags.
computational code) of an • Used for developing presentation
enterprise application. layer of an enterprise application
• Servlets are created and maintained • Frequently used for designing
by Java developers. websites and used by web
developers.
JSP syntax
1.Declaration Tag :-It is used to declare variables.
Syntax:- <%! Dec var %>
Example:- <%! int var=10; %>

2.Java Scriplets :- It allows us to add any number of JAVA


code, variables and expressions.
Syntax:- <% java code %>
3.JSP Expression :- It evaluates and convert the expression to a string.

Syntax:- <%= expression %>


Example:- <% num1 = num1+num2 %>

4.JAVA Comments :- It contains the text that is added for information


which has to be ignored.
Syntax:- <% -- JSP Comments %>
JSP Architecture
JSP Architecture(cont..)
JSP Processing
The following steps explain how the web server creates the Webpage using JSP −
• As with a normal page, your browser sends an HTTP request to the web server.
• The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine.
This is done by using the URL or JSP page which ends with .jsp instead of .html.
• The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is
very simple in which all template text is converted to println( ) statements and all JSP elements are
converted to Java code. This code implements the corresponding dynamic behavior of the page.
• The JSP engine compiles the servlet into an executable class and forwards the original request to a
servlet engine.
• A part of the web server called the servlet engine loads the Servlet class and executes it. During
execution, the servlet produces an output in HTML format. The output is furthur passed on to the web
server by the servlet engine inside an HTTP response.
• The web server forwards the HTTP response to your browser in terms of static HTML content.
• Finally, the web browser handles the dynamically-generated HTML page inside the HTTP response
exactly as if it were a static page.
JSP Life Cycle
The following are the paths followed by a JSP −
• Compilation
• Initialization
• Execution
• Cleanup
JSP tag

• 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 Scriplet tag
• A scriptlet tag is used to execute java source code in JSP.
Syntax is as follows:
• <% java source code %>

Example:
• <html>
• <body>
• <% out.print("welcome to jsp"); %>
• </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:
• <html>
• <body>
• <%= "welcome to jsp" %>
• </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.
• The syntax of the declaration tag is as follows:
• <%! field or method declaration %>
• Example:
• <html>
• <body>
• <%! int data=50; %>
• <%= "Value of the variable is:"+data %>
• </body>
• </html>
Summary
. Discussed about Java Server Pages (JSP).
. Discussed about JSP Architecture.
. Discussed about JSP Life Cycle.

15
Home Work
Q1. What are the life-cycle methods for a JSP?

Q2. List out some advantages of using JSP.

16
References

Online Video Link


• https://nptel.ac.in/courses/106/105/106105191/
• https://www.coursera.org/courses?query=java
• https://www.coursera.org/specializations/object-oriented-programming
• https://www.youtube.com/watch?v=aqHhpahguVY

Text Book
• Herbert Schildt (2019), “Java The Complete Reference, Ed. 11, McGraw-Hill .

17
THANK YOU

For queries
Email: kushagra.e13465@cumail.in

You might also like