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

UNIT-3

SYLLABUS
JAVA SERVER PAGES

Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web-based
applications. JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.
Java Server Pages (JSP) is a technology that allows developers to create dynamic web pages using a combination of HTML, XML, and Java code. JSP pages are
executed on a web server, and the resulting output is sent to the client's web browser. JSP provides a way to easily access Java code and objects from within a web page,
simplifying the creation of dynamic web pages. JSP pages are typically used in conjunction with Java servlets, which handle data processing and client requests. JSP is
part of the Java EE platform and is supported by most web servers and servlet containers.
• 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.
• JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI,
EJB, JAXP, etc.
• JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.
Finally, JSP is an integral part of Java EE, a complete platform for enterprise class applications. This means that JSP can play a part in the simplest applications to the
most complex and demanding.
Facts About JSP
• JSP stands for Java Server Pages.
• JSP is a technology to build dynamic web applications.
• JSP is a part of Java Enterprise Edition (Java EE).
• JSP is similar to HTML pages, but they also contain Java code executed on the server side.
• Server-side scripting means the JSP code is processed on the web server rather than the client machine.
• A JSP page is a file with a ".jsp" extension that can contain a combination of HTML Tags and JSP codes.
• To create a web page, JSP uses a combination of HTML or XML markup, JSP tags, expressions, and
Java code.
• JSP tags begin with <% and end with %>.
• JSP expressions are used to insert dynamic content into the page and begin with <%= and end with %>.
• JSP can use JavaBeans to store and retrieve data.
• JSP requires a Java development environment and a Java Servlet Container such as Apache Tomcat or
Jetty.
• JSP is widely used in the industry for creating enterprise web applications.
• JSP is an improved extended version of Servlet technology.
Applications of JSP

• JSP vs. Active Server Pages (ASP)


The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more
powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.
• JSP vs. Pure Servlets
It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML.
• JSP vs. Server-Side Includes (SSI)
SSI is really only intended for simple inclusions, not for "real" programs that use form data, make database connections, and the like.
• JSP vs. JavaScript
JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database
access and image processing etc.
• JSP vs. Static HTML
Regular HTML, of course, cannot contain dynamic information.
Advantages of JSP

• JSP offers an efficient and more straightforward approach to coding dynamic web pages.
• JSP provides a wide variety of pre-built tags and custom tags, which can be used to add functionality to
web pages
• JSP allows developers to separate the presentation of the web page from the logic and processing,
making it easier to maintain the web application.
• Web Container (or application server like tomcat) handles changes when changes are done in the JSP
code and does not need recompilation.
• JSP allows developers to follow the Model-View-Controller (MVC) design pattern, which separates a
web application's presentation, logic, and data. This makes it easier to create scalable and maintainable
web applications.
• JSP is commonly used with other Java technologies, such as JavaServer Faces (JSF), which provides a
framework for building web applications, making the development process more streamlined.
• Provides good security features like session tracking, user authentication, and access restriction.
• JSP can use standard Java code and libraries, which increases the potential for code reuse.
Advantages of JSP OVER SERVLET

• JSP is considered to be better than servlet; there are many reasons why JSP is more beneficial than
servlet:
• JSP pages allow web designers to work with HTML or XML markup, while Servlets require a
deeper understanding of Java.
• JSP doesn't need additional files such as java class files, web.xml, etc.
• JSPs are compiled into servlets by the JSP engine, which improves performance compared to
interpreting servlets at runtime.
• Many IDEs and tools are available for developing JSPs, which can make development quicker and
easier compared to servlets.
• JSPs are more accessible to debug and troubleshoot than servlets because they have better error
reporting and debugging facilities.
• JSP is considered easier to learn than servlet.
JSP architecture is a 3-tier architecture that separates a web application's presentation, logic,
and data layers.
The presentation layer, or client side, is responsible for displaying the user interface and handling
user interaction.
The logic layer, or server-side, is responsible for processing user requests and handling business
logic.
The data layer is responsible for storing and retrieving data from a database or other storage
system. This separation of concerns allows for better maintainability and scalability of the
application.
JSP ENGINE JSP ARCHITECTURE
• A JSP-based web application requires a JSP engine,
also known as a web container, to process and execute JSP architecture is a web application development model that defines the structure
the JSP pages. The web container is a web server and organization of a JSP-based web application. It typically consists of the
component that manages the execution of web
programs such as servlets, JSPs, and ASPs. following components:
•JSP pages: These are the main building blocks of a JSP application. They contain
• When a client sends a request for a JSP page, the web
container intercepts it and directs it to the JSP engine.
a combination of HTML, XML, and JSP elements (such as scriptlets, expressions,
The JSP engine then converts the JSP page into a and directives) that generate dynamic content.
servlet class, compiles it, and creates an instance of the •Servlets: JSP pages are converted into servlets by the JSP engine. Servlets are
class. The service method of the servlet class is then Java classes that handle HTTP requests and generate dynamic content.
called, which generates the dynamic content for the JSP
page. •JSP engine (web container): This web server component is responsible for
processing JSP pages. It converts JSP pages into servlets, compiles them, and runs
• The web container also manages the lifecycle of the
JSP pages and servlets, handling tasks such as
them in the Java Virtual Machine (JVM).
instantiating, initializing and destroying them. •JavaBeans: These are reusable Java classes that encapsulate business logic and
Additionally, it provides security, connection pooling, data. They are used to store and retrieve information from a database or other data
and session management services to the JSP-based web sources.
application.
•JSTL (JavaServer Pages Standard Tag Library): This is a set of predefined tags
that can be used in JSP pages to perform common tasks such as iterating over
collections, conditional statements, and internationalization.
•Custom Tag Libraries: JSP allows the creation of custom tags that can be used on
JSP pages. These reusable Java classes encapsulate complex logic and can
generate dynamic content cleanly and consistently.
JSP ARCHITECTURE FLOW

JSP architecture flow refers to the sequence of steps a JSP-based web application goes through to process and execute JSP pages. The general
flow of a JSP architecture can be described as follows:
1. A client (such as a web browser) sends a request for a JSP page to a web server.
2. The web server forwards the request to the JSP engine responsible for processing JSP pages.
3. The JSP engine checks if the requested JSP page has been compiled into a servlet. If not, it compiles the JSP page into a servlet class. This is
done by parsing the JSP page and converting its elements (such as scriptlets, expressions, and directives) into Java code.
4. The JSP engine then compiles the servlet class, which creates a Java class file that can be executed by the Java Virtual Machine (JVM).
5. The JSP engine then creates an instance of the servlet class and calls the service() method, which generates the dynamic content for the JSP
page. Within the service() method, the JSP engine generates the HTML code for the response by combining the static template in the JSP
page with the dynamic content generated by the Java code.
6. The JSP engine sends the generated HTML code back to the web server, which then sends it back to the client as a response.
7. The JSP engine also maintains a cache of the compiled servlet classes so subsequent requests for the same JSP page can be handled more
efficiently.
1.TRANSLATION PHASE
JSP LIFECYCLE 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.
1. Translation Phase The webserver checks if a compiled version of the JSP page already exists.
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
2. Compilation Phase (with .java extension). This process is known as translation.
The web container automatically translates the JSP page into a servlet. So as a developer, you don't have to worry about
3. Initialization Phase converting it manually.
4. Execution Phase
2.COMPILATION PHASE
5. Destruction(Cleanup) 1.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.
Phase 2.In this compilation phase, the Translated .java file of the servlet is compiled into the Java servlet .class file.

3. INITIALIZATION PHASE
In this Initialization phase: Here, the container will:
3.Load the equivalent servlet class.
4.Create instance.
5.Call the jspInit() method for initializing the servlet instance.
This initialization is done only once with the servlet's init method where database connection, opening files, and creating lookup
tables are done.

4.EXECUTION PHASE
This Execution phase is responsible for all JSP interactions and logic executions for all requests until the JSP gets destroyed. As
the requested JSP page is loaded and initiated, the JSP engine has to invoke the _jspService() method. This method is invoked as
per the requests, responsible for generating responses for the associated requests, and responsible for all HTTP methods.

5.DESTRUCTION(CLEANUP) PHASE
This destruction phase is invoked when the JSP has to be cleaned up from use by the container. The jspDestroy() method is
invoked. You can incorporate and write cleanup codes inside this method for releasing database connections or closing any file.
JSP SCRIPTING ELEMENTS COMMENTS
Comments are marked as text or statements that are ignored by the JSP container. They are useful when
Scripting elements in JSP must be written within the < you want to write some useful information or logic to remember in the future.
% %> tags. The JSP engine will process any code you
write within the pair of the <% and %> tags, and any
other texts within the JSP page will be treated as
HTML code or plain text while translating the JSP
page.
Here are five different scripting elements:
• Comment <%-- Set of comment statements --%>
• Directive <%@ directive %>
• Declaration <%! declarations %>
• Scriptlet <% scriplets %>
• Expression <%= expression %>

DIRECTIVES
These tags are used to provide specific instructions to the web
container when the page is translated. It has three subcategories:
•Page: <%@ page ... %>
•Include: <%@ include ... %>
•Taglib: <%@ taglib ... %>
Declaration
JSP SCRIPTLET TAG EXPRESSIONS
As the name suggests, it is used to declare methods and
The scriptlet tag allows writing Java code statements within Expressions elements are responsible for
variables you will use in your Java code within a JSP file.
the JSP page. This tag is responsible for implementing the containing scripting language expression, which
According to the rules of JSP, any variable must be declared
functionality of _jspService() by scripting the java code. gets evaluated and converted to Strings by the
before it can be used.
JSP engine and is meant to the output stream of
the response. Hence, you are not required to
write out.print() for writing your data. This is
mostly used for printing the values of variables or
methods in the form of output.

You might also like