0% found this document useful (0 votes)
36 views5 pages

Unit 5 - 2 Marks & 16 Marks Questions

The document provides an overview of Servlets and Database Connectivity, detailing the functions and lifecycle of Servlets, the concept of Cookies, and the workings of JDBC. It explains the differences between various methods and classes related to Servlets and JDBC, including HttpServlet, ServletContext, and JDBC drivers. Additionally, it outlines practical examples and exercises related to session tracking and database queries using JDBC.

Uploaded by

Sakkaravarthi S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views5 pages

Unit 5 - 2 Marks & 16 Marks Questions

The document provides an overview of Servlets and Database Connectivity, detailing the functions and lifecycle of Servlets, the concept of Cookies, and the workings of JDBC. It explains the differences between various methods and classes related to Servlets and JDBC, including HttpServlet, ServletContext, and JDBC drivers. Additionally, it outlines practical examples and exercises related to session tracking and database queries using JDBC.

Uploaded by

Sakkaravarthi S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

UNIT – V

Servlets and Database Connectivity


Part - A

1. What are Servlets?


• Java Servlets are server-side Java program modules that process and answer client requests
and implement the servlet interface. It helps in enhancing Web server functionality with
minimal overhead, maintenance and support.
• A servlet acts as an intermediary between the client and the server. As servlet modules run on
the server, they can receive and respond to requests made by the client. Request and response
objects of the servlet offer a convenient way to handle HTTP requests and send text data back
to the client.
• Since a servlet is integrated with the Java language, it also possesses all the Java features such
as high portability, platform independence, and security and Java database connectivity

2. Explain the life cycle methods of a Servlet.


The javax.servlet.Servlet interface defines the three methods known as life-cycle method.
• init()
• service()
• destroy()
o First the servlet is constructed, then initialized wih the init() method.
o Any request from client are handled initially by the service() method before delegating
to the doXxx() methods in the case of HttpServlet.
o The servlet is removed from service, destroyed with the destroy() method, then
garbaged collected and finalized.

3. What is a Cookie? Give its uses. (Nov/Dec 2015)


Cookies are small files which are stored on a user's computer. They are designed to hold a data
specific to a particular client and website, and can be accessed either by the web server or the client
computer. Each cookie is effectively a small lookup table containing pairs of (key, data) values - for
example (firstname, John) (lastname, Smith). Once the cookie has been read by the code on the server
or client computer, the data can be retrieved and used to customize the web page appropriate
Cookies used as a convenient way to carry information from one session on a website to another, or
between sessions on related websites, without having to burden a server machine with massive
amounts of data storage.

4. Illustrate the general steps to run a servlet?


1.Compile the servlet using an appropriate compiler version
2.Copy the resulting .class file to the appropriate directory for our java-servlet capable server.
3.Start the server
4.Navigate to the URL corresponding to the servlet.

5. What is meant by Stateless Connection?


When a web server receives a HTTP request from a web browser it evaluates the request and returns
the requested document, if it exists, and then breaks the HTTP connection. This document is preceded
by the response header, which has details about how to display the document that will be sent by the
server. Each time a request is made to the server, it is as if there was no prior connection and each
request can yield only a single document. This is known as Stateless Connection.
6. Explain ServletContext
ServletContext interface is a window for a servlet to view it's environment. A servlet can use this
interface to get information such as initialization parameters for the web applicationor servlet
container's version. Every web application has one and only one ServletContext and is accessible to
all active resource of that application.

7. What is preinitialization of a servlet?


A container doesnot initialize the servlets as soon as it starts up, it initializes a servlet when it receives
a request for that servlet first time. This is called lazy loading. The servlet specification defines the
<load-on-startup> element, which can be specified in the deployment descriptor to make the servlet
container load and initialize the servlet as soon as it starts up. The process of loading a servlet before
any request comes in is called preloading or preinitializing a servlet.

8. What is the difference between Difference between doGet() and doPost()?


A doGet() method is limited with 2k of data to be sent, and doPost() method doesn't have this
limitation. Arequest string for doGet() looks like the following:
http://www.allapplabs.com/svt1?p1=v1&p2=v2&...&pN=vN

doPost() method call doesn't need a long text tail after a servlet name in a request. All parameters are
stored in arequest itself, not in a request string, and it's impossible to guess the data transmitted to a
servlet only looking ata request string.
9. What is the difference between HttpServlet and GenericServlet?
A GenericServlet has a service() method aimed to handle requests. HttpServlet extends
GenericServlet and adds support for doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(),
doOptions(), doDelete(), doTrace() methods (HTTP 1.1).
Both these classes are abstract.
10. What is the difference between ServletContext and ServletConfig?
ServletContext: Defines a set of methods that a servlet uses to communicate with its servlet container,
for example, to get the MIME type of a file, dispatch requests, or write to a log
file.TheServletContext object is contained within the ServletConfig object, which the Web server
provides the servlet when the servlet is initialized ServletConfig: The object created after a servlet is
instantiated and its default constructor is read. It is created to pass.
11. Explain about Session tracking.
A session is basically a conversation between a browser and a server. The session is important, as
HTTP is a stateless protocol. This means that the connection between web server and a web browser
is not automatically maintained, and that the state of a web session is not saved.
State is a general term that includes "everything about your situation" and the specifics vary based on
the application. For a web application, the state would include any data that had been entered, the
results of any queries that is run, and the security access information (e.g. whether you have logged in
to the site).
12. Define JDBC
Java Database Connectivity (JDBC) is an application program interface (API)
specification for connecting programs written in Java to the data in databases.

13. How JDBC work?


• The Java application calls JDBC classes and interfaces to submit SQL statements and retrieve
results.
• The JDBC API is implemented through the JDBC driver. The JDBC Driver is a set of classes
that implement the JDBC interfaces to process JDBC calls and return result sets to a Java
application. The database (or data store) stores the data retrieved by the application using the
JDBC Driver.
14. What are the objects of JDBC main API?
The main objects of the JDBC API include:
• A DataSource object is used to establish connections. Although the Driver Manager can also
be used to establish a connection, connecting through a DataSource object is the preferred
method.
• Statement, PreparedStatement, and CallableStatement objects are used for executing
SQL statements. A PreparedStatement object is used when an application plans to reuse a
statement multiple times. The application prepares the SQL it plans to use. Once prepared, the
application can specify values for parameters in the prepared SQL statement. The statement
can be executed multiple times with different parameter values specified for each execution.
• A ResultSet object contains the results of a query. A ResultSet is returned to an application
when a SQL query is executed by a statement object. The ResultSet object provides methods
for iterating through the results of the query.
15. List out the components of JDBC architecture
• The JDBC API :The JDBC API provides programmatic access to relational data from the
Java programming language. Using the JDBC API, applications can execute SQL statements,
retrieve results, and propagate changes back to an underlying data source.
• JDBC Driver Manager :The JDBC DriverManager class defines objects which can connect
Java applications to a JDBC driver. DriverManager has traditionally been the backbone of the
JDBC architecture.
• JDBC Test Suite :The JDBC driver test suite helps you to determine that JDBC drivers will
run your program
• JDBC-ODBC Bridge -The Java Software bridge provides JDBC access via ODBC drivers.
16. What is JDBC Driver ?
JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database
server.
For example, using JDBC drivers enable you to open database connections and to interact with it by
sending SQL or database commands then receiving results with Java.
17. What are different types of JDBC Drivers?
JDBC driver implementations vary because of the wide variety of operating systems and hardware
platforms in which Java operates.
Type 1: JDBC-ODBC Bridge Driver
Type 2: JDBC-Native API
Type 3: JDBC-Net pure Java
Type 4: 100% pure Java

18. What is JDBC-ODBC Bridge Driver?


JDBC Bridge is used to access ODBC drivers installed on each client machine. Using ODBC requires
configuring on your system a Data Source Name (DSN) that represents the target database.
When Java first came out, this was a useful driver because most databases only supported
ODBC access but now this type of driver is recommended only for experimental use or when no other
alternative is available.
19. What is JDBC-Native API
JDBC API calls are converted into native C/C++ API calls which are unique to the database.
These drivers typically provided by the database vendors and used in the same manner as the JDBC-
ODBC Bridge, the vendor-specific driver must be installed on each client machine.
If we change the Database we have to change the native API as it is specific to a database and
they are mostly obsolete now but you may realize some speed increase with a Type 2 driver, because
it eliminates ODBC's overhead.

20. What is JDBC-Net pure Java?


A Type 3 driver, a three-tier approach is used to accessing databases. The JDBC clients use
standard network sockets to communicate with an middleware application server. The socket
information is then translated by the middleware application server into the call format required by
the DBMS, and forwarded to the database server.
This kind of driver is extremely flexible, since it requires no code installed on the client and a

single driver can actually provide access to multiple databases.

21. Write the code segment to store current server time in session using java servlet API
(May/June 2016)
// Create a session object if it is already not created.
HttpSession session = request.getSession(true);
// Get session creation time.
Date createTime = new Date(session.getCreationTime());
// Get last access time of this web page.
Date lastAccessTime = new Date(session.getLastAccessedTime());
22. Explain the servlet interface and its methods (Nov/Dec 2016)
Method Description
public void init(ServletConfig config) initializes the servlet. It is the life cycle method
of servlet and invoked by the web container
only once.
public void service(ServletRequest provides response for the incoming request. It is
request,ServletResponse response) invoked at each request by the web container.

public void destroy() is invoked only once and indicates that servlet
is being destroyed.
public ServletConfig getServletConfig() returns the object of ServletConfig.
public String getServletInfo() returns information about servlet such as writer,
copyright, version etc.

Part - B
1. Explain the concept of Servlets with an example program
2. What are attributes are available for servlet and explain each of them
3. List down the methods of HttpServlet. Explain one of them with an example
4. List down the methods of SevletConfig. Explain one of them with an example
5. List down the methods of Servletcontext. Explain one of them with an example
6. Explain the architechture of a Servlet?
7. Explain life cycle of a Servlet?
8. What is Servlet Chaining? Explain with an example
9. Write short notes on JDBC
10. Explain Database connectivity.

Part – C
1. Write a program in servlet using Session tracking to display date and time.
2. Demonstration with the assumption that a database has a table Dept with two columns Deptid and
Name. Assume that the administrator user id and password to access the database table are, scott
and tiger. Write a JDBC program that can query and print all the entries in the table Dept. Make
the database connection using a type3driver database.driver and connection string jdbc:db:oci.

3. Use session tracking to make a servlet that says ” Welcome CSE ” to first-time visitors (within a
browsing session) and ” Welcome Back ” to return visitors. Do this job using Session Tracking
and Cookies
4. Write a program using servlet and JDBC for developing an online application for the
shopping of computer science books. (Hint: use concept of session tracking) we have to create
a database for book title, author(s) of book, publisher, year of publication, price. Make
necessary assumptions for book shopping.

You might also like