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 [Link].*;
import [Link].*;
import [Link].*;
// 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
[Link]("text/html");
// Actual logic goes here.
PrintWriter out = [Link]();
[Link]("<h1>" + message + "</h1>");
}
public void destroy()
// do nothing.
Conclusion:
Hence, we have successfully designed web page to demonstrate the use of Servlet.
.
Program
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/ShowBookInfo")
public class ShowBookInfo extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
[Link]("text/html");
PrintWriter out=[Link]();
[Link]("<center><h2>Book Information</h2></center>");
try
{
[Link]("[Link]");
Connection
con=[Link]("jdbc:mysql://localhost:3306/ebooks","root","root");
Statement st=[Link]();
ResultSet rs=[Link]("select * from bookinfo");
[Link]("<center><table border=\"1\">");
[Link]("<tr><th>BookId</th><th>BookName</th><th>Author</th><th>Prize</th><th
>Quantity</th></tr>");
while([Link]())
{
[Link]("<tr>");
[Link]("<td>"+[Link](1)+"</td>");
[Link]("<td>"+[Link](2)+"</td>");
[Link]("<td>"+[Link](3)+"</td>");
[Link]("<td>"+[Link](4)+"</td>");
[Link]("<td>"+[Link](5)+"</td>");
[Link]("</tr>");
}
[Link]("</table></center>");
}
catch(Exception e)
{
[Link]("<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?