Web Technology Practical No. 5
Web Technology Practical 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).
// Initialization code...
// Finalization code...
}
1.3 Examples
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Do required initialization
response.setContentType("text/html");
}
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 {
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?