Web Programming Lab Manual-1
Web Programming Lab Manual-1
<label>Title:</label>
<input type="text" name="title" required><br><br>
<label>Author:</label>
<input type="text" name="author" required><br><br>
<label>Edition:</label>
<input type="text" name="edition" required><br><br>
<label>Publication:</label>
<input type="text" name="publication" required><br><br>
save_book.php
<?php
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "library";
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
MySQL Database Table:
USE library;
Output
9.php
<?php
$servername="localhost";
$username="root";
$password=" ";
$dbname="library";
if($conn->connect_error){
die("connection failed:".$conn->connect_error);
}
$title=$_GET['title'];
$sql="SELECT accession_number,title,author,edition,publication
FROM books where title LIKE %title%";
$result=$conn->query($sql);
if($result->num_rows>0){
echo "<h2>Search Results</h2>";
echo "<table border='1'><tr><th>Accession
Number</th><th>Title</th><th>Author</th><th>Edition</th><th>Public
ation</th></tr>";
while($row=$result->fetch_assoc()){
echo
"<tr><td>".$row["accession_number"]."</td><td>".$row["title"]."</td><t
d>".$row["author"]."</td><td>".$row["edition"]."</td><td>".$row["publi
cation"]."</td></tr>";
}
echo "</table>";
}
else{
echo "No books found with title '$title'";
}
$conn->close();
?>
Output
(on success)
(on fail)
10. a) Program to accept username and display a greeting message.
index.html
<html>
<body>
<h2>Enter Your Username</h2>
<form action="GreetingServlet" method="GET">
Username: <input type="text" name="username">
<input type="submit" value="Submit">
</form>
</body>
</html>
GreetingServlet.java
import java.io.IOException;
import java.io.PrintWriter;
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("/GreetingServlet")
public class GreetingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Hello, " + username + "! Welcome to our
website.</h1>");
out.println("</body></html>");
}
}
Output:
10b) Program to change the background colour of the page based on the
colour selected by the user.
color.html
<html>
<head>
<title>Select Color</title>
</head>
<body>
<h2>Select a Background Color</h2>
<form action="ColorServlet" method="GET">
<label for="color">Choose a color:</label>
<select id="color" name="color">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="yellow">Yellow</option>
</select>
<input type="submit" value="Apply">
</form>
</body>
</html>
ColorServlet.java
package com.example;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body style='background-color:" + color +
"'>");
out.println("<h1>The background color is " + color + "</h1>");
out.println("</body></html>");
}
}
Output:
11. Program to display a greeting based on the access time of the server.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Greeting Page</title>
</head>
<body>
<h1>Welcome to the Greeting Page</h1>
<form action="GreetingTimeServlet" method="get">
<button type="submit">Get Greeting</button>
</form>
</body>
</html>
TimeBasedGreetingServlet.java
package com.example;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalTime;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
if (now.isBefore(LocalTime.NOON)) {
greeting = "Good Morning!";
} else if (now.isBefore(LocalTime.of(18, 0))) {
greeting = "Good Afternoon!";
} else {
greeting = "Good Evening!";
}
out.println("<html><body>");
out.println("<h1>" + greeting + "</h1>");
out.println("</body></html>");
}
}
Output:
12. Program to create and display a cookie.
cookie.html
<html>
<head>
<title>Cookie Example</title>
</head>
<body>
<h2>Enter Your Favorite Language</h2>
<form action="CookieServlet" method="POST">
<label for="lang">Favorite Programming Language:</label>
<input type="text" id="lang" name="lang">
<input type="submit" value="Submit">
</form>
</body>
</html>
CookieServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/CookieServlet")
public class CookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Output:
13. Program to create a session and display session information viz, session
ID, creation time and last accessed.
Sessioninfo.html
<html>
<head>
<title>Session Information</title>
</head>
<body>
<h2>Check Session Information</h2>
<form action="SessionInfoServlet" method="get">
<input type="submit" value="Get Session Info">
</form>
</body>
</html>
SessionInfoServlet.java
package com.example;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;
out.println("<html><body>");
out.println("<h2>Session Information</h2>");
out.println("<p>Session ID: " + sessionId + "</p>");
out.println("<p>Creation Time: " + creationTime + "</p>");
out.println("<p>Last Accessed Time: " + lastAccessTime + "</p>");
out.println("</body></html>");
}
}
Output:
14. Program to request server information viz. Request Method, URI,
Protocol and Remote address
serverinfo.html
<html>
<head>
<title>Server Information</title>
</head>
<body>
<h2>Check Server Information</h2>
<form action="ServerInfoServlet" method="get">
<input type="submit" value="Get Server Info">
</form>
</body>
</html>
ServerInfoServlet.java
package com.example;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
userinfo.html
<html>
<head>
<title>Submit User Info</title>
</head>
<body>
<h2>Enter your details</h2>
<form action="UserInfoServlet" method="post">
<label for="username">Username:</label>
<input type="text" id="username"
name="username"><br><br>
<label for="address">Address:</label>
<input type="text" id="address" name="address"><br><br>
UserInfoServlet.java
package com.example;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use CGI qw(:standard);
# Clean up
$sth->finish();
$dbh->disconnect();
Output: