0% found this document useful (0 votes)
2 views8 pages

Web Technology Practical No. 5

The document outlines an assignment focused on implementing a sample program using Java Servlets, detailing the objectives, software and hardware requirements, and the servlet life cycle. It explains the advantages of servlets over CGI, including better performance and platform independence, and provides example code for a simple servlet that displays book information from a database. Additionally, it includes FAQs related to servlets and their functionality.

Uploaded by

nilimapawase14
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)
2 views8 pages

Web Technology Practical No. 5

The document outlines an assignment focused on implementing a sample program using Java Servlets, detailing the objectives, software and hardware requirements, and the servlet life cycle. It explains the advantages of servlets over CGI, including better performance and platform independence, and provides example code for a simple servlet that displays book information from a database. Additionally, it includes FAQs related to servlets and their functionality.

Uploaded by

nilimapawase14
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/ 8

Assignment No: 5

Problem Statement: Implement the sample program demonstrating the use of Servlet.
Objectives: To use client side and server side web technologies.
Outcome: Apply the client side and server side technologies for web application
development
Software & Hardware Requirments:
Operating System: Ubuntu
Software: Eclipse
Programming Language: Java(Servlet/JSP)
Server: Tomcat
Browser: Mozilla/Google Chrome
Hardware: i3 Processor, 4GB RAM, 500GB HDD

Theory:
1. Servlet
Java Servlets are programs that run on a Web or Application server and act as a middle
layer between a request coming from a Web browser or other HTTP client and databases or
applications on the HTTP server. Using Servlets, you can collect input from users through web
page forms, present records from a database or another source, and create web pages
dynamically. Java Servlets often serve the same purpose as programs implemented using the
Common Gateway Interface (CGI).

1.1 Advantages in comparison with the CGI.


● Performance is significantly better.
● Servlets execute within the address space of a Web server. It is not necessary to
create a separate process to handle each client request.
● Servlets are platform-independent because they are written in Java.
● Java security manager on the server enforces a set of restrictions to protect the
resources on a server machine. So servlets are trusted.
● The full functionality of the Java class libraries is available to a servlet. It can
communicate with applets, databases, or other software via the sockets and RMI
mechanisms that you have seen already.

1.2 . Servlets - Life Cycle


The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the
servlet:
1. Servlet class is loaded.
2. Servlet instance is created.
3. init method is invoked.
4. service method is invoked.
5. destroy method is invoked.
The init() method :
The init method is designed to be called only once. It is called when the servlet is first
created, and not called again for each user request. So, it is used for one-time initializations, just
as with the init method of applets.
When a user invokes a servlet, a single instance of each servlet gets created, with each
user request resulting in a new thread that is handed off to doGet or doPost as appropriate. The
init() method simply creates or loads some data that will be used throughout the life of the
servlet.
The init method definition looks like this:
public void init() throws ServletException

// Initialization code...

The service() method :


The service() method is the main method to perform the actual task. The servlet container
(i.e. web server) calls the service() method to handle requests coming from the client( browsers)
and to write the formatted response back to the client.
Each time the server receives a request for a servlet, the server spawns a new thread and
calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE,
etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
public void service(ServletRequest request, ServletResponse response)throws
ServletException, IOException

The destroy() method :


The destroy() method is called only once at the end of the life cycle of a servlet. This
method gives your servlet a chance to close database connections, halt background threads, write
cookie lists or hit counts to disk, and perform other such cleanup activities.
After the destroy() method is called, the servlet object is marked for garbage collection.
public void destroy() \

// Finalization code...

}
1.3 Examples
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.

Conclusion:
Hence, we have successfully designed web page to demonstrate the use of Servlet.
.
Program
ShowBookInfo.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ShowBookInfo")
public class ShowBookInfo extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.print("<center><h2>Book Information</h2></center>");
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ebooks","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from bookinfo");
out.print("<center><table border=\"1\">");

out.print("<tr><th>BookId</th><th>BookName</th><th>Author</th><th>Prize</th><th
>Quantity</th></tr>");
while(rs.next())
{
out.print("<tr>");
out.print("<td>"+rs.getString(1)+"</td>");
out.print("<td>"+rs.getString(2)+"</td>");
out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getInt(4)+"</td>");
out.print("<td>"+rs.getInt(5)+"</td>");
out.print("</tr>");
}
out.print("</table></center>");
}
catch(Exception e)
{
out.print("<center><h2>Unable to connect to database</h2></center>");
}

}
Database

Output:
FAQs:
1. What is Servlet?
2. Explain life cycle of servlet?
3. What is session management?
4. Explain advantages of Servlet over CGI?

You might also like