Unit 3 Chapter 1
Unit 3 Chapter 1
Chapter 1
JSP
JavaServer Pages (JSP) is a Web page development technology that supports dynamic content. This allows programmers to use
specific JSP tags to insert Java code into HTML pages. A part of JavaServer Pages is a type of Java servlet designed to perform
the function of a Java web application user interface
JSP technology is used to create web application just like Servlet technology. It can be thought of as an extension to Servlet
because it provides more functionality than servlet such as expression language, JSTL, etc.
A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than Servlet because we can separate
designing and development. It provides some additional features such as Expression Language, Custom Tags, etc.
Advantages of JSP over Servlet
There are many advantages of JSP over the Servlet. They are as follows:
1) Extension to Servlet
JSP technology is the extension to Servlet technology. We can use all the features of the Servlet in JSP. In addition to, we can use
implicit objects, predefined tags, expression language and Custom tags in JSP, that makes JSP development easy.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business logic with presentation logic. In Servlet technology, we
mix our business logic with the presentation logic.
If JSP page is modified, we don't need to recompile and redeploy the project. The Servlet code needs to be updated and
recompiled if we have to change the look and feel of the application.
In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduces the code. Moreover, we can use EL,
implicit objects, etc.
Disadvantages of JSP
● It is hard to trace JSP pages error because JSP pages are translated to servlet.
● As JSP output is HTML, it is not rich in features.
● It is very hard to debug or trace errors because JSP pages are first translated into servlets before the compilation
process.
● Database connectivity is not easy.
Why use Servlet?
● In Java server pages JSP, the execution is much faster compared to other dynamic languages.
● It is much better than Common Gateway Interface (CGI).
● Java server pages (JSP)are always compiled before its processed by the server as it reduces the effort of
the server to create process.
● Java server pages are built over Servlets API. Hence, it has access to all Java APIs, JNDI, JDBC EJB,
and other components of java.
● JSP is an important part of Java EE (Enterprise Edition), which is a platform for enterprise-level
applications.
The Lifecycle of a JSP Page
To create the first JSP page, write some HTML code as given below, and save it by .jsp extension. We have saved this file as
index.jsp. Put it in a folder and paste the folder in the web-apps directory in apache tomcat to run the JSP page.
Index.jsp
1. <html>
2. <body>
3. <% out.print(2*5); %>
4. </body>
5. </html>
How to run a simple JSP Page?
The directory structure of JSP page is same as Servlet. We contain the JSP page outside the WEB-INF folder or in any directory.
The JSP API
1. javax.servlet.jsp
2. javax.servlet.jsp.tagext
javax.servlet.jsp package The javax.servlet.jsp package has two interfaces and classes.The two interfaces are as follows:
1. JspPage
2. HttpJspPage
● JspWriter
● PageContext
● JspFactory
● JspEngineInfo
● JspException
● JspError
The JspPage interface
According to the JSP specification, all the generated servlet classes must implement the JspPage interface. It extends the
Servlet interface. It provides two life cycle methods.
1. public void jspInit(): It is invoked only once during the life cycle of the JSP when JSP page is requested firstly. It is used to
perform initialization. It is same as the init() method of Servlet interface.
2. public void jspDestroy(): It is invoked only once during the life cycle of the JSP before the JSP page is destroyed. It can
be used to perform some clean up operation.