WT-Ex 6
WT-Ex 6
AIM:
The aim of this program is to develop a digital library application using Java Servlets to
manage user interactions and session data. It demonstrates how to invoke servlets from HTML
forms, allowing users to interact with the library. Additionally, it uses hidden form fields for
session tracking and counts user visits during a session.
PROCEDURE:
1. Create a new Java Web Project in your IDE (e.g., Eclipse)
2. Ensure the project has support for Servlets and configure a servlet container like Apache
Tomcat.
3. Create an HTML form (login.html) to allow users to interact with the digital library
application.
4. Use form fields to capture user details, such as username, and submit the form to a servlet.\
5. Create a servlet (LibraryServlet.java) to handle the form submission.
6. Retrieve the username from the request and display a welcome message.
7. Implement session tracking by:
a. Using a hidden form field to maintain the username across requests.
b. Counting the number of times the user has accessed the servlet within the session.
8. Deploy the application on the servlet container (e.g., Apache Tomcat).
9. Open index.html in a browser to start interacting with the application.
10. Enter a username and submit the form to see the personalized welcome message and visit
count.
11. Click "Go back to Library" to see the visit count increase.
PROGRAM:
Project Structure
DigiLib/
├── src/
│ └── library/
│ ├── LoginServlet.java # Handles login and initializes user session
│ ├── AddBookServlet.java # Allows adding a book and uses hidden fields to track session
│ ├── LibraryServlet.java # Displays library and counts visits
├── Webapp/
│ ├── login.html # Login form page
│ ├── addBook.html # Form for adding a new book
│ └── WEB-INF/
│ └── web.xml # Web deployment descriptor
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Digital Library Login</title>
</head>
<body>
<h1>Digital Library Login</h1>
<form action="LoginServlet" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
LoginServlet.java
package library;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String username = request.getParameter("username");
// Redirect to the LibraryServlet to display the library page and track visits
response.sendRedirect("LibraryServlet");
}
}
LibraryServlet.java
package library;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String username = request.getParameter("username");
// Redirect to the LibraryServlet to display the library page and track visits
response.sendRedirect("LibraryServlet");
}
}
addBook.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Book</title>
</head>
<body>
<h1>Add a New Book</h1>
<form action="AddBookServlet" method="POST">
<label for="title">Title:</label>
<input type="text" id="title" name="title" required><br><br>
<label for="author">Author:</label>
<input type="text" id="author" name="author" required><br><br>
<input type="submit" value="Add Book">
</form>
</body>
</html>
AddBookServlet.java
package library;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
/**
* Servlet implementation class AddBookServlet
*/
public class AddBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddBookServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Retrieve the book details and user session
String title = request.getParameter("title");
String author = request.getParameter("author");
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>DigiLib</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>library.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>AddBookServlet</display-name>
<servlet-name>AddBookServlet</servlet-name>
<servlet-class>library.AddBookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddBookServlet</servlet-name>
<url-pattern>/AddBookServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LibraryServlet</display-name>
<servlet-name>LibraryServlet</servlet-name>
<servlet-class>library.LibraryServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LibraryServlet</servlet-name>
<url-pattern>/LibraryServlet</url-pattern>
</servlet-mapping>
</web-app>
OUTPUT:
RESULT:
Thus, the program to develop digital library application using Java Servlets to invoke
servlets from HTML forms, Session tracking using hidden form fields and counts user visits
during a session has been executed successfully.