JSP -Java Server Pages
Definition:
In Java, JSP stands for Java Server Pages. I
t is a server-side technology which is used for creating web applications. It is used to
create dynamic web content. JSP consists of both HTML tags and JSP tags. In t
his, JSP tags are used to insert JAVA code into HTML pages.
It is an advanced version of Servlet Technology i.e. a web-based technology that 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 a servlet by the JSP container before processing the client’s
request. JSP has various features like JSP Expressions, JSP tags, JSP Expression
Language, etc.
JSP life cycle:
1
Explain JSP Elements:
We will learn about the various elements available in JSP with suitable examples. In JSP
elements can be divided into 4 different types.
These are:
Expression
Scriplets
Directives
Declarations
Expression:
We can use this tag to output any data on the generated page. These data are automatically
converted to string and printed on the output stream.
Syntax:
JSP Expressions are: <%="Anything" %>
Scriplets:
In this tag we can insert any amount of valid java code and these codes are placed in the _jsp
Service method by the JSP engine.
Syntax:
<%//java codes%>
Variables available to the JSP Scriptlets are:
Request
Response
Session
Out
2
Directives:
A JSP “directive” starts with <%@ characters. In the directives, we can import packages, and
define error-handling pages or the session information of the JSP page.
Syntax:
<%@directive attribute="value"% >
page
include
taglib
Declarations:
This tag is used for defining the functions and variables to be used in the JSP.
Syntax:
<%!
//java codes
%>
Example:
<%@page import="java.util.*"%>
<HTML>
<BODY>
<%!
Date the Date=new Date(); Date getDate()
{
System.out.println("In getDate() method"); return theDate;
}
%>
Hello! The time is now<%=getDate()%>
</BODY>
</HTML>
3
Example of a JSP Web Page:
<HTML>
<HEAD>
<TITLE>A Web Page</TITLE>
</HEAD>
<BODY>
<%out.println("Hello there!");%>
</BODY>
</HTML>
Advantages of JSP:
Extension to Servlet: JSP Extension of Servlet. We can use all the features of Servlet
in JSP. we can use Implicit objects, Predefined tags, Customized tags, and expression
language that JSP development easily.
Easy toMaintain: It is easily managed because we can easily Separate our Business
Logic, in Servlet Technology, We can mix our business logic with the Presentation
logic.
Fast Development: No need to recompile and redeploy. If the JSP page is Modified.
we are not required to recompile and redeploy the Project. The Servlet code needs to be
Recompiled and updated if we want to change the Look and feel of the Application.
Less Code than Servlet: In JSP we can use a lot of tags Such as action tags, just,
Custom tags, etc that reduce the code. More ever We can use EL and Implicit Objects.
Disadvantages of JSP:
JSP is difficult to debug or trace errors because JSP pages are first translated into servlets
before the compilation process.
As JSP pages are translated to Servlets and Compiled, it is difficult to trace error occurred
in JSP pages.
Database connectivity is not easy.
JSP Pages require more disk space to hold the JSP page.
4
The difference between Servlet and JSP is as follows:
Servlet JSP
Servlet is a java code. JSP is a HTML-based compilation code.
Writing code for servlet is harder than JSP as it
JSP is easy to code as it is java in HTML.
is HTML in java.
Servlet plays a controller role in the ,MVC JSP is the view in the MVC approach for
approach. showing output.
JSP is slower than Servlet because the first
Servlet is faster than JSP. step in the JSP lifecycle is the translation of
JSP to java code and then compile.
Servlet can accept all protocol requests. JSP only accepts HTTP requests.
In Servlet, we can override the service() In JSP, we cannot override its service()
method. method.
In Servlet by default session management is not In JSP session management is automatically
enabled, user have to enable it explicitly. enabled.
In Servlet we have to implement everything like In JSP business logic is separated from
business logic and presentation logic in just one presentation logic by using JavaBeansclient-
servlet file. side.
Modification in Servlet is a time-consuming
compiling task because it includes reloading, JSP modification is fast, just need to click the
recompiling, JavaBeans and restarting the refresh button.
server.
It does not have inbuilt implicit objects. In JSP there are inbuilt implicit objects.
5
Servlet JSP
There is no method for running JavaScript on While running the JavaScript at the client side
the client side in Servlet. in JSP, client-side validation is used.
Packages can be imported into the JSP
Packages are to be imported on the top of the
program (i.e, bottom ,middleclient-side, or top
program.
)
It cannot handle extensive data processing
It can handle extensive data processing.
very efficiently.
The facility of writing custom tags is not
The facility of writing custom tags is present.
present.
Before the execution, JSP is compiled in Java
Servlets are hosted and executed on Web
Servlets and then it has a similar lifecycle as
Servers.
Servlets.
Servlet:
Definition:
Servlet is a technology which is used to create a web application.
Servlet is an API that provides many interfaces and classes including documentation.
Servlet is an interface that must be implemented for creating any Servlet.
Servlet is a class that extends the capabilities of the servers and responds to the incoming
requests. It can respond to any requests.
Servlet is a web component that is deployed on the server to create a dynamic web page.
6
Servlet Life Cycle: