You are on page 1of 7

Assignment No.

: 05

o Title:
Implement the sample program demonstrating the use of Servlet.
e.g., Create a database table ebookshop (book_id, book_title, book_author, book_price,
quantity) using database like Oracle/MySQL etc. and display (use SQL select query) the
table content using servlet.
o Date of performance:
o Date of submission:
o Sign:
Assignment No.05

o Aim:
Implement the sample program demonstrating the use of Servlet.
e.g., Create a database table ebookshop (book_id, book_title, book_author,
book_price, quantity) using database like Oracle/MySQL etc. and display
(use SQL select query) the table content using servlet.
o Hardware/Software Requirements:
Software:
1. Any Operating System
2. JDK 7 or later
3. Editors : Eclipse
4. Web browser
5. Tomcat 7 or later

o Theory:

❖ Servlet :

➢ 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.

❖ Servlet Life Cycle Methods:


There are three life cycle methods of a Servlet :
• init()
• service()
• destroy()

1. init() method:
The Servlet.init() method is called by the Servlet container to indicate
that this Servlet instance is instantiated successfully and is about to put
into service.

Syntax:
public class MyServlet implements Servlet{
public void init(ServletConfig config) throws ServletException {
//initialization code
}
//rest of code
}

2. service() method:
The service() method of the Servlet is invoked to inform the Servlet
about the client requests.
This method uses ServletRequest object to collect the data requested
by the client. This method uses ServletResponse object to generate
the output content.

Syntax:

public class MyServlet implements Servlet{


public void service(ServletRequest res, ServletResponse res)
throws ServletException, IOException {
// request handling code
}
// rest of code
}

3. destroy() method:
The destroy() method runs only once during the lifetime of a
Servlet and signals the end of the Servlet instance.

Syntax:
public void destroy()

As soon as the destroy() method is activated, the Servlet container


releases the Servlet instance.
❖ “Hello World” Servlet Program:

// Import required java libraries


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class


public class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException {


// Do required initialization
message = "Hello World";
}

public void doGet(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {

// Set response content type


response.setContentType("text/html");

// Actual logic goes here.


PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}

public void destroy() {


// do nothing.
}
}
❖ Java Database Connectivity
1. Register the driver class
The forName() method of Class class is used to register the driver class.
This method is used to dynamically load the driver class.

Example,

Class.forName("oracle.jdbc.driver.OracleDriver");

2. Create the connection object


The getConnection() method of DriverManager class is used to establish
connection with the database.

Example,

Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");

3. Create the Statement object


The createStatement() method of Connection interface is used to create
statement. The object of statement is responsible to execute queries with the
database.

Example,
Statement stmt=con.createStatement();

4. Execute the query


The executeQuery() method of Statement interface is used to execute queries
to the database. This method returns the object of ResultSet that can be used
to get all the records of a table.

Example,

ResultSet rs=stmt.executeQuery("select * from emp");


while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

5. Close the connection object


By closing connection object statement and ResultSet will be closed
automatically. The close() method of Connection interface is used to close
the connection.

Example,
con.close();

o Conclusion:
Thus, we have implemented the sample program demonstrating the use of
Servlet.

You might also like