WT Unit V
WT Unit V
Features of JSP
Coding in JSP is easy.
Reduction in the length of Code.
Connection to Database is easier.
Make Interactive websites.
Portable, Powerful, flexible and easy to maintain.
No Redeployment and No Re-Compilation.
Extension to Servlet.
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.
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
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.
• 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
Difference between Servlet and JSP:
JDBC:
Java DataBase Connectivity, commonly known as JDBC, is an API for Java programming
language that defines how a client may access a database. JDBC API uses JDBC driver
written in Java. JDBC is platform independent.
JDBC stands for Java Database Connectivity, which is a standard Java API for database
independent connectivity between the Java programming language, and a wide range of
databases.
The JDBC library includes APIs for each of the tasks mentioned below that are commonly
associated with database usage.
• Making a connection to a database.
• Creating SQL or MySQL statements.
• Executing SQL or MySQL queries in the database.
• Viewing & Modifying the resulting records.
Fundamentally, JDBC is a specification that provides a complete set of interfaces that allows
for portable access to an underlying database. Java can be used to write different types of
executables, such as:
• Java Applications
• Java Applets
• Java Servlets
• Java ServerPages (JSPs)
• Enterprise JavaBeans (EJBs).
All of these different executables are able to use a JDBC driver to access a database, and
take advantage of the stored data. JDBC provides the same capabilities as ODBC, allowing
Java programs to contain database independent code.
Components of JDBC:
JDBC has four major components that are used for the interaction with the database.
1. JDBC API
2. JDBC Test Suite
3. JDBC Driver Manger
4. JDBC ODBC Bridge Driver
1) JDBC API: JDBC API provides various interfaces and methods to establish easy connection
with different databases.
1. javax.sql.*;
2. java.sql.*;
2) JDBC Test suite: JDBC Test suite facilitates the programmer to test the various operations
such as deletion, updation, insertion that are being executed by the JDBC Drivers.
3) JDBC Driver manager: JDBC Driver manager loads the database-specific driver into an
application in order to establish the connection with the database. The JDBC Driver manager
is also used to make the database-specific call to the database in order to do the processing
of a user request.
4) JDBC-ODBC Bridge Drivers: JDBC-ODBC Bridge Drivers are used to connect the database
drivers to the database. The bridge does the translation of the JDBC method calls into the
ODBC method call. It makes the usage of the sun.jdbc.odbc package that encompasses the
native library in order to access the ODBC (Open Database Connectivity) characteristics.
JDBC Drivers:
A JDBC driver is a software component enabling a java application to interact with
a database. JDBC requires drivers for each database. The JDBC driver gives out the
connection to the database and implements the protocol for transferring the query
and result between client and database. JDBC drivers are client side adaptors that
convert request from java program to a protocol that that the DBMS can understand.
There are four types of drivers:
1. JDBC-ODBC(Open Database Connectivity) Bridge Driver (Type 1)
2. Native API Driver (Type 2/Partially Java Driver)
3. Network Protocol Driver (Type 3/Fully Java Driver)
4. Thin Driver (Type 4/Fully Java Driver)
Advantage:
• Better performance as compared to Type 1 driver.
Disadvantages:
• Driver is platform dependent.
• The vendor client library needs to be installed.
• Native driver needs to be installed on each machine.
Advantage:
o No client side library is required because of application server that can perform many
tasks like auditing, load balancing, logging etc.
Disadvantages:
o Network support is required on client machine.
o Requires database-specific coding to be done in the middle tier.
o Maintenance of Network Protocol driver becomes costly because it requires
database-specific coding to be done in the middle tier.
4. Thin Driver (Type 4/Fully Java Driver):
• Converts JDBC calls directly into vendor specific database protocol.
• Fully written in Java.
• User needs different drivers for each database.
• Performance is good.
Advantage:
Performance is good as compared to other drivers.
No software is required for client or server side.
Disadvantage:
Different types of drivers required for different database.
The forName() method loads the specified class into the memory and thus it automatically
gets registered.
Example:
Model Layer
• This is the data layer which consists of the business logic of the system.
• It consists of all the data of the application
• It also represents the state of the application.
• It consists of classes which have the connection to the database.
• The controller connects with model and fetches the data and sends to the view layer.
• The model connects with the database as well and stores the data into a database
which is connected to it.
View Layer
• This is a presentation layer.
• It consists of HTML, JSP, etc. into it.
• It normally presents the UI of the application.
• It is used to display the data which is fetched from the controller which in turn
fetching data from model layer classes.
• This view layer shows the data on UI of the application.
Controller Layer
• It acts as an interface between View and Model.
• It intercepts all the requests which are coming from the view layer.
• It receives the requests from the view layer and processes the requests and does the
necessary validation for the request.
• This request is further sent to model layer for data processing, and once the request
is processed, it sends back to the controller with required information and displayed
accordingly by the view.
Advantages of MVC Architecture
• Easy to maintain
• Easy to extend
• Easy to test
• Navigation control is centralized
Output:
JSP Implicit Objects
JSP supports nine automatically defined variables, which are also
called implicit objects. These variables are:
1. <form action="welcome.jsp">
2. <input type="text" name="uname">
3. <input type="submit" value="go"><br/>
4. </form>
welcome.jsp
1. <%
2. response.sendRedirect("http://www.google.com");
3. %>
Output
1 out.print(dataType dt)
Print a data type value
out.println(dataType dt)
2
Print a data type value then terminate the line with new line character.
out.flush()
3
Flush the stream.
Example of out implicit object
In this example we are simply displaying date and time.
index.jsp
1. <html>
2. <body>
3. <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>
4. </body>
5. </html>
Output
Output
The application Object
The application object is direct wrapper around the ServletContext object for the generated Servlet
and in reality an instance of a javax.servlet.ServletContext object.
This object is a representation of the JSP page through its entire lifecycle. This object is created
when the JSP page is initialized and will be removed when the JSP page is removed by
the jspDestroy() method.
By adding an attribute to application, you can ensure that all JSP files that make up your web
application have access to it.
We will check the use of Application Object in JSP - Hits Counter chapter.
1. <form action="welcome">
2. <input type="text" name="uname">
3. <input type="submit" value="go"><br/>
4. </form>
web.xml file
1. <web-app>
2.
3. <servlet>
4. <servlet-name>sonoojaiswal</servlet-name>
5. <jsp-file>/welcome.jsp</jsp-file>
6. </servlet>
7.
8. <servlet-mapping>
9. <servlet-name>sonoojaiswal</servlet-name>
10. <url-pattern>/welcome</url-pattern>
11. </servlet-mapping>
12.
13. <context-param>
14. <param-name>dname</param-name>
15. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
16. </context-param>
17.
18. </web-app>
welcome.jsp
1. <%
2.
3. out.print("Welcome "+request.getParameter("uname"));
4.
5. String driver=application.getInitParameter("dname");
6. out.print("driver name is="+driver);
7.
8. %>
Output
The config Object
The config object is an instantiation of javax.servlet.ServletConfig and is a direct wrapper around
the ServletConfig object for the generated servlet.
This object allows the JSP programmer access to the Servlet or JSP engine initialization
parameters such as the paths or file locations etc.
The following config method is the only one you might ever use, and its usage is trivial −
config.getServletName();
This returns the servlet name, which is the string contained in the <servlet-name> element defined
in the WEB-INF\web.xml file.
1. <form action="welcome">
2. <input type="text" name="uname">
3. <input type="submit" value="go"><br/>
4. </form>
web.xml file
1. <web-app>
2.
3. <servlet>
4. <servlet-name>sonoojaiswal</servlet-name>
5. <jsp-file>/welcome.jsp</jsp-file>
6.
7. <init-param>
8. <param-name>dname</param-name>
9. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
10. </init-param>
11.
12. </servlet>
13.
14. <servlet-mapping>
15. <servlet-name>sonoojaiswal</servlet-name>
16. <url-pattern>/welcome</url-pattern>
17. </servlet-mapping>
18.
19. </web-app>
welcome.jsp
1. <%
2. out.print("Welcome "+request.getParameter("uname"));
3.
4. String driver=config.getInitParameter("dname");
5. out.print("driver name is="+driver);
6. %>
Output
JSP Comments
Since JSP is built on top of HTML, we can write comments in JSP file
like html comments as
<-- This is HTML Comment -->
These comments are sent to the client and we can look it with view
source option of browsers. We can put comments in JSP files as:
<%-- This is JSP comment --%>
This comment is suitable for developers to provide code level
comments because these are not sent in the client response.