JavaServer Pages (JSP) is a technology that allows developers to embed Java code in HTML pages to create dynamic web content. JSP pages combine HTML code with JSP actions and commands. At runtime, JSP pages are translated into Java servlets that generate the web page content dynamically. This provides better performance than CGI and allows embedding of dynamic elements directly into HTML pages.
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
JSP.pptx
1. JavaServer Pages (JSP) is a technology for developing
Webpages that supports dynamic content.
This helps developers insert java code in HTML pages by
making use of special JSP tags, most of which start with <%
and end with %>.
A JavaServer Pages component is a type of Java servlet that is
designed to fulfill the role of a user interface for a Java web
application.
Web developers write JSPs as text files that combine HTML or
XHTML code, XML elements, and embedded JSP actions and
commands.
JSP Overview
2. Using JSP, you can collect input from users through Webpage
forms, present records from a database or another source,
and create Webpages dynamically.
JSP tags can be used for a variety of purposes, such as
retrieving information from a database or registering user
preferences, accessing JavaBeans components, passing
control between pages, and sharing information between
requests, pages etc.
JSP Overview
3. Why Use JSP?
JavaServer Pages often serve the same purpose as programs
implemented using the Common Gateway Interface (CGI).
But JSP offers several advantages in comparison with the
CGI.
Performance is significantly better because JSP allows
embedding Dynamic Elements in HTML Pages itself instead
of having separate CGI files.
JSP are always compiled before they are processed by the
server unlike CGI/Perl which requires the server to load an
interpreter and the target script each time the page is
requested.
4. The life cycle of a JSP page can be divided into the
following phase:
1.Translation Phase
2.Compilation Phase
3.Initialization Phase
4.Execution Phase
5.Destruction(Cleanup) Phase
5.
6. Translation phase
1.When a user navigates to a page ending with a .jsp extension in their web
browser, the web browser sends an HTTP request to the webserver.
2.The webserver checks if a compiled version of the JSP page already exists.
3.If the JSP page's compiled version does not exist, the file is sent to the JSP
Servlet engine, which converts it into servlet content (with .java extension).
This process is known as translation.
4.The web container automatically translates the JSP page into a servlet. So
as a developer, you don't have to worry about converting it manually.
7. Compilation Phase
1. In this compilation phase, the Translated .java file of the servlet is
compiled into the Java servlet .class file.
2. In case the JSP page was not compiled previously or at any point in
time, then the JSP page is compiled by the JSP engine.
8. Initialization Phase
1. In this Initialization phase: Here, the container will:
2. Load the equivalent servlet class.
3. Create instance.
4. Call the jspInit() method for initializing the servlet instance.
5. This initialization is done only once with the servlet's init method
where database connection, opening files, and creating lookup tables
are done.
9. Execution Phase
1. This Execution phase is responsible for all JSP interactions and
logic executions for all requests until the JSP gets destroyed.
2. As the requested JSP page is loaded and initiated, the JSP engine
has to invoke the jspService() method.
3. This method is invoked as per the requests, responsible for
generating responses for the associated requests, and responsible for
all HTTP methods.
10. Destruction(Cleanup) Phase
1. This destruction phase is invoked when the JSP has to be cleaned
up from use by the container.
2. The jspDestroy() method is invoked. You can incorporate and write
cleanup codes inside this method for releasing database
connections or closing any file.
11. JSP syntax
Syntax available in JSP are following:
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 %>
12. Elements of JSP in detail:
The elements of JSP have been described below −
The Scriptlet
A scriptlet can contain any number of JAVA language statements, variable or
method declarations, or expressions that are valid in the page scripting
language.
Following is the syntax of Scriptlet −
<% code fragment %>
You can write the XML equivalent of the above syntax as follows −
<jsp:scriptlet> code fragment </jsp:scriptlet>
14. JSP Declarations
A declaration declares one or more variables or methods that you can use in
Java code later in the JSP file. You must declare the variable or method
before you use it in the JSP file.
Following is the syntax for JSP Declarations −
<%! declaration; [ declaration; ]+ ... %>
You can write the XML equivalent of the above syntax as follows −
<jsp:declaration> code fragment </jsp:declaration>
Following is an example for JSP Declarations −
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
15. JSP Expression
The expression element can contain any expression that is
valid according to the Java Language Specification but you
cannot use a semicolon to end an expression.
Following is the syntax of JSP Expression −
<%= expression %>
You can write the XML equivalent of the above syntax as follows −
<jsp:expression> expression </jsp:expression>
18. Advantages of using JSP
•It does not require advanced knowledge of JAVA
•It is capable of handling exceptions
•Easy to use and learn
•It can tags which are easy to use and understand
•Implicit objects are there which reduces the length of code
•It is suitable for both JAVA and non JAVA programmer
Disadvantages of using JSP
•Difficult to debug for errors.
•First time access leads to wastage of time
•It’s output is HTML which lacks features.
19. Following steps are involved in the JSP life cycle:
Translation of JSP page to Servlet
Compilation of JSP page(Compilation of JSP into test.java)
Classloading (test.java to test.class)
Instantiation(Object of the generated Servlet is created)
Initialization(jspInit() method is invoked by the container)
Request processing(_jspService()is invoked by the container)
JSP Cleanup (jspDestroy() method is invoked by the
container)
20. JSP - Directives
Directives provide directions and instructions to the container,
telling it how to handle certain aspects of the JSP processing.
A JSP directive affects the overall structure of the servlet class. It
usually has the following form −
<%@ directive attribute = "value" %>
Directives can have a number of attributes which you can list
down as key-value pairs and separated by commas.
The blanks between the @ symbol and the directive name, and
between the last attribute and the closing %>, are optional.
21. No Directive & Description
1
<%@ page ... %> Ex: <%@ page attribute = "value" %>
Defines page-dependent attributes, such as scripting language,
error page, and buffering requirements.
2
<%@ include ... %> Ex: <%@ include file = "relative url" >
Includes a file during the translation phase.
3
<%@ taglib ... %> Ex: <%@ taglib uri="uri" prefix = "prefixOfTag" >
Declares a tag library, containing custom actions, used in the page
There are three types of directive tag −
22. JSP | Implicit Objects – request and
response
Implicit objects are a set of Java objects that the JSP
Container makes available to developers on each page.
These objects may be accessed as built-in variables via
scripting elements and can also be accessed
programmatically by JavaBeans and Servlets.
JSP provide you Total 9 implicit objects which are as follows:
1.request: This is the object of HttpServletRequest class
associated with the request.
2.response: This is the object of HttpServletResponse class
associated with the response to the client.
23. 3. config: This is the object of ServletConfig class associated
with the page.
4. application: This is the object of ServletContext class
associated with the application context.
5. session: This is the object of HttpSession class associated
with the request.
6. page context: This is the object of PageContext class that
encapsulates the use of server-specific features. This object
can be used to find, get or remove an attribute.
24. 7. page object: The manner we use the keyword this for current
object, page object is used to refer to the current translated
servlet class.
8. exception: The exception object represents all errors and
exceptions which is accessed by the respective jsp. The
exception implicit object is of type java.lang.Throwable.
9. out: This is the PrintWriter object where methods like print
and println help for displaying the content to the client.