You are on page 1of 9

Java assignment 1,2,3

1. What is Servlet?

Servlets are the Java programs that run on the Java-enabled web server or application
server. They are used to handle the request obtained from the webserver, process the
request, produce the response, then send a response back to the webserver.

1) Servlet is a technology which is used to create a web application.


2) Servlet is an API that provides many interfaces and classes including
documentation.
3) Servlet is an interface that must be implemented for creating any Servlet.
4) Servlet is a class that extends the capabilities of the servers and responds to the
incoming requests. It can respond to any requests.
5) Servlet is a web component that is deployed on the server to create a dynamic web
page.

Execution of Servlets basically involves six basic steps:


1.The clients send the request to the webserver.
2. The web server receives the request.
3. The web server passes the request to the corresponding servlet.
4. The servlet processes the request and generates the response in the form of
output.
5. The servlet sends the response back to the webserver.
5. The web server sends the response back to the client and the client browser
displays it on the screen.

2. Explain Servlet Life Cycle?


Stages of the Servlet Life Cycle: The Servlet life cycle mainly goes through four
stages,
Loading a Servlet.
Initializing the Servlet.
Request handling.
Destroying the Servlet.
Let’s look at each of these stages in details:
Loading a Servlet: The first stage of the Servlet lifecycle involves loading and
initializing the Servlet by the Servlet container. The Web container or Servlet
Container can load the Servlet at either of the following two stages :
Initializing the context, on configuring the Servlet with a zero or positive
integer value.
If the Servlet is not preceding stage, it may delay the loading process until the
Web container determines that this Servlet is needed to service a request.
Loading : Loads the Servlet class.
Instantiation : Creates an instance of the Servlet. To create a new instance
of the Servlet, the container uses the no-argument constructor.
Initializing a Servlet: After the Servlet is instantiated successfully, the Servlet
container initializes the instantiated Servlet object. The container initializes the
Servlet object by invoking the Servlet.init(ServletConfig) method which accepts
ServletConfig object reference as parameter.
The Servlet container invokes the Servlet.init(ServletConfig) method only once,
immediately after the Servlet.init(ServletConfig) object is instantiated
successfully. This method is used to initialize the resources, such as JDBC
datasource.
Now, if the Servlet fails to initialize, then it informs the Servlet container by
throwing the ServletException or UnavailableException.
Handling request: After initialization, the Servlet instance is ready to serve the
client requests. The Servlet container performs the following operations when
the Servlet instance is located to service a request :
It creates the ServletRequest and ServletResponse objects. In this case, if this is
a HTTP request, then the Web container
creates HttpServletRequest and HttpServletResponse objects which are
subtypes of the ServletRequest and ServletResponse objects respectively.
After creating the request and response objects it invokes the
Servlet.service(ServletRequest, ServletResponse) method by passing the
request and response objects.
The service() method while processing the request may throw
the ServletException or UnavailableException or IOException.
Destroying a Servlet: When a Servlet container decides to destroy the Servlet,
it performs the following operations,
It allows all the threads currently running in the service method of the Servlet
instance to complete their jobs and get released.
After currently running threads have completed their jobs, the Servlet
container calls the destroy() method on the Servlet instance.
After the destroy() method is executed, the Servlet container releases all the
references of this Servlet instance so that it becomes eligible for garbage
collection.

3.What IS the TRACKING SESSION ?

Session Tracking is a way to maintain state (data) of an user. It is also known


as session management in servlet.

Http protocol is a stateless so we need to maintain state using session tracking


techniques. Each time user requests to the server, server treats the request as the new
request. So we need to maintain the state of an user to recognize to particular user.

The conversion of a user over a period of time is referred to as a session. In


general, it refers to a certain period of time.
The recording of the object in session is known as tracking.
Session tracking is the process of remembering and documenting customer
conversions over time. Session management is another name for it.
The term “stateful web application” refers to a web application that is capable
of remembering and recording client conversions over time.
Why is Session Tracking Required
Because the HTTP protocol is stateless, we require Session Tracking to make
the client-server relationship stateful.
Session tracking is important for tracking conversions in online shopping,
mailing applications, and E-Commerce applications.
The HTTP protocol is stateless, which implies that each request is treated as a
new one. As you can see in the image below.

4.Explain advanced features of Servlets.


1. Portable:
As I mentioned above that Servlet uses Java as a programming language,
Since java is platform independent, the same holds true for servlets. For
example, you can create a servlet on Windows operating system that users
GlassFish as web server and later run it on any other operating system like
Unix, Linux with Apache tomcat web server, this feature makes servlet
portable and this is the main advantage servlet has over CGI.

2. Efficient and scalable:


Once a servlet is deployed and loaded on a web server, it can instantly start
fulfilling request of clients. The web server invokes servlet using a lightweight
thread so multiple client requests can be fulling by servlet at the same time
using the multithreading feature of Java. Compared to CGI where the server
has to initiate a new process for every client request, the servlet is truly
efficient and scalable.

3. Robust:
By inheriting the top features of Java (such as Garbage collection, Exception
handling, Java Security Manager etc.) the servlet is less prone to memory
management issues and memory leaks. This makes development of web
application in servlets secure and less error prone.

Integration Servlets are tightly integrated with the server. Servlet can
use the server to translate the file paths, check authorization, perform
logging and MIME type mapping etc.
Persistent: Servlets remain in memory until explicitly destroyed. This
helps in serving several incoming requests. Servlets establishes
connection only once with the database and can handle several
requests on the same database.
Server Independent: Servlets are compatible with any web server
available today.
Protocol Independent: Servlets can be created to support any
protocols like FTP commands, Telnet sessions, NNTP newsgroups, etc.
It also provides extended support for the functionality of HTTP protocol.
Fast: Since servlets are compiled into bytecodes, they can execute more
quickly as compared to other scripting languages. The bytecode
compilation feature helps servlets to give much better performance. In
addition, it also provides advantage of strong error and type checking.

5.What are Difference between browser and server sessions?

6.What is JDBC?
JDBC or Java Database Connectivity is a Java API to connect and execute the
query with the database. It is a specification from Sun microsystems that
provides a standard abstraction(API or Protocol) for java applications to
communicate with various databases. It provides the language with java
database connectivity standards. It is used to write programs required to
access databases. JDBC, along with the database driver, can access databases
and spreadsheets. The enterprise data stored in a relational database(RDB) can
be accessed with the help of JDBC APIs.
Definition of JDBC(Java Database Connectivity)
JDBC is an API(Application programming interface) used in java programming
to interact with databases. The classes and interfaces of
JDBC allow the application to send requests made by users to the specified
database.
Purpose of JDBC
Enterprise applications created using the JAVA EE technology need to interact
with databases to store application-specific information. So, interacting with a
database requires efficient database connectivity, which can be achieved by
using the ODBC(Open database connectivity) driver. This driver is used with
JDBC to interact or communicate with various kinds of databases such as
Oracle, MS Access, Mysql, and SQL server database.

7.What is JDBC-ODBC bridge?


Therefore, JDBC-ODBC bridge driver was developed which
translates the JDBC API to the ODBC API and vice versa. This
bridge acts as interface that enables all DBMS which support ODBC
(Open Data Base Connectivity) to interact with Java Applications.
JDBC-ODBC bridge is implemented as a class file and a native
library. The name of the class file is JdbcOdbc.class. On windows
platform, native library called JDBCODBC.DLL is used.
Figure shows the JDBC application architecture in which a front-end
application uses JDBCAPI for interacting with JDBC Driver
Manager. Here, JDBC Driver Manager is the backbone of JDBC
architecture. It acts as interface that connects a Java application to
the driver specified in the Java program. Next, is the JDBC-ODBC
bridge which helps the JDBC to access ODBC data sources.
The JDBC-ODBC Bridge allows applications written in the Java
programming language to use the JDBC API with many existing
ODBC drivers. The Bridge is itself a driver based on JDBC
technology ("JDBC driver") that is defined in the
class sun.jdbc.odbc.JdbcOdbcDriver. The Bridge defines the
JDBC sub-protocol odbc

8.What are the uses of Connection, Statement and Result set?

A Connection is a session between a Java application and a database. It


helps to establish a connection with the database.

The Connection interface is a factory of Statement, PreparedStatement,


and DatabaseMetaData, i.e., an object of Connection can be used to get
the object of Statement and DatabaseMetaData. The Connection interface
provide many methods for transaction management like commit(),
rollback(), setAutoCommit(), setTransactionIsolation(), etc.

The Statement interface provides methods to execute queries with the


database. The statement interface is a factory of ResultSet i.e. it provides
factory method to get the object of ResultSet.
The statement interface is used to create SQL basic statements in Java it
provides methods to execute queries with the database. There are different
types of statements that are used in JDBC as follows:
* Create Statement
* Prepared Statement
* Callable Statement

A ResultSet object maintains a cursor that points to the current row in the
result set. The term "result set" refers to the row and column data contained in
a ResultSet object.

9.What is JSP? Explain predefined JSP variables

JSP stands for Java Server Pages, and it’s a server side technology. It’s used for
creating web applications and dynamic web content. The main property of JSP
is that we can insert our java code inside our HTML page using JSP tag. JSP
provides you platform-independent pages.
request:

This variable specifies the data included in a http request. This variable takes
value from the clients' browser to pass it over to the server.reponse:
This variable specifies the data included in the http response. It is used with
cookies and also in http headers.
out:

This variable specifies the output stream otherwise known as printwriter in a


page context.
session:

This variable specifies the data associated with httpsession object with a
specific session of a user. The main purpose of this object is to use the session
information to maintain multiple page requests.
application:

This variable is used to share data with all application pages.


Config:

This variable refers the java servlet configuration value.


pagecontext:

This variable is used to store the environment for the page like page attributes,
access to the request, response and session objects.
page:

This variable is used to store the instance of the generated java servlet.

1. Write JSP code Structure with the page directive.

You might also like