Practical File
Practical File
Practical File
<script>
document.addEventListener('DOMContentLoaded', function() {
console.log('Document loaded');
});
</script>
</body>
</html>
Experiment 2: write an html program to design an entry
form of student details and send it to database server like
SQL, oracle or Ms access
HTML form (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Details Entry Form</title>
</head>
<body>
<h2>Student Details Entry Form</h2>
<form action="submit.php" method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br><br>
<label for="dob">Date of Birth:</label><br>
<input type="date" id="dob" name="dob"><br><br>
<label for="gender">Gender:</label><br>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Preparation:
1. Download Software:
o Downloading Tomcat from: https://tomcat.apache.org/
o Downloading Apache from: https://httpd.apache.org/download.html
2. Locate Static Web Pages:
o Then Finally accessed the book website from here.
A. Installing Tomcat:
1. Extract Tomcat:
o Extract the downloaded Tomcat archive to a desired location
2. Start Tomcat:
o Navigate to the Tomcat bin directory:
cd /opt/tomcat/bin
startup.bat
After placing files, restart Tomcat using the shutdown and startup scripts:
shutdown.bat
startup.bat
3. Accessing in Browser:
o Opened web browser and went to http://localhost:8080/books_website.
Finally saw our books website!
Experiment 8: Assume four users user1, user2, user3,
user4 having the passwords pwd1,pwd2,pwd3,pwd4
respectively . write a servlet for doing the following .
create a cookie and add these four user id's and
passwords to this cookie . and read the user id and
passwords entered in the login form and authenticate with
the values available in the cookies.
Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String userId = request.getParameter("userId");
String password = request.getParameter("password");
Cookie cookie = new Cookie("UserCredentials", userId + ":" + password);
cookie.setMaxAge(3600); // Cookie expires in 1 hour
response.addCookie(cookie);
out.println("<html><head><title>Login Result</title></head><body>");
boolean authenticated = false;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie c : cookies) {
if (c.getName().equals("UserCredentials")) {
String[] values = c.getValue().split(":");
if (userId.equals(values[0]) && password.equals(values[1])) {
authenticated = true;
break;
}
}
}
}
if (authenticated) {
out.println("<h2>Welcome, " + userId + "!</h2>");
out.println("<p>You are successfully logged in.</p>");
} else {
out.println("<h2>Authentication failed!</h2>");
out.println("<p>Invalid user ID or password.</p>");
}
out.println("</body></html>");
}
}
@Override
public void init() throws ServletException {
products.add(new Product(1, "Product 1", 10.00, 0));
products.add(new Product(2, "Product 2", 20.00, 0));
products.add(new Product(3, "Product 3", 30.00, 0));
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
HttpSession session = request.getSession(true); // Create session if not existing
Cart cart = (Cart) session.getAttribute("cart");
if (cart == null) {
cart = new Cart();
session.setAttribute("cart", cart);
}
request.setAttribute("products", products);
request.getRequestDispatcher("/products.jsp").forward(request, response);
}
}
CartServlet
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/cart")
public class CartServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
int productId = Integer.parseInt(request.getParameter("productId"));
String action = request.getParameter("action");
int quantity = 1; // Default quantity
if (request.getParameter("quantity") != null) {
quantity = Integer.parseInt(request.getParameter("quantity"));
}
HttpSession session = request.getSession();
Cart cart = (Cart) session.getAttribute("cart");
if (cart == null) {
cart = new Cart();
session.setAttribute("cart", cart);
}
switch (action) {
case "add":
cart.addItem(getProductById(productId), quantity);
break;
case "remove":
cart.removeItem(getProductById(productId));
break;
case " "update":
cart.updateQuantity(getProductById(productId), quantity);
break;
default:
break; } }
private Product getProductById(int productId) {
return new Product(productId, "Sample Product", 10.00, 0); } }