Program-5.
1
AIM: Create a servlet that recognizes a visitor for the first time to a web
application and responds by saying “Welcome, you are visiting for the first time”.
When the page is visited for the second time, it should say “Welcome Back”.
CODE:
package src;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/VisitorServlet")
public class VisitorServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
[Link]("text/html");
[Link]("Servlet is running...");
Cookie[] cookies = [Link]();
boolean firstVisit = true;
if (cookies != null) {
for (Cookie cookie : cookies) {
if ([Link]().equals("visited")) {
firstVisit = false;
break;
}
}
}
if (firstVisit) {
Cookie newCookie = new Cookie("visited", "true");
[Link](60 * 60 * 24);
[Link](newCookie);
[Link]().println("Welcome, you are visiting for the first
time!");
} else {
[Link]().println("Welcome back!");
}
}
}
OUTPUT:
Program-5.2
AIM: Write a program to set cookie information using Servlet.
CODE:
package src;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/SetCookieServlet")
public class SetCookieServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
Cookie userCookie = new Cookie("username", "Jatin123");
[Link](60 * 60 * 24);
[Link](userCookie);
Cookie[] cookies = [Link]();
String username = null;
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("username".equals([Link]())) {
username = [Link](); // Retrieve the value of 'username'
cookie
break;
}
}
}
[Link]("<html><body>");
if (username != null) {
[Link]("<h2>Cookie 'username' set successfully! </h2> <br>
<h3>Value: " + username +"</h3>");
} else {
[Link]("<h2>No 'username' cookie found yet.</h2>");
}
[Link]("</body></html>");
}
}
OUTPUT:
Program-5.3
AIM: Write a Java program to show user validation using Servlet.
CODE:
package src;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
@WebServlet("/UserValidationServlet")
public class UserValidationServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String username = [Link]("username");
String password = [Link]("password");
[Link]("text/html");
PrintWriter out = [Link]();
if ("admin".equals(username) && "admin123".equals(password)) {
[Link]("<h1>Login Successful</h1>");
} else {
[Link]("<h1>Invalid Username or Password</h1>");
}
}
}
[Link]
<!DOCTYPE html>
<html>
<head>
<title>User Login</title>
</head>
<body>
<h2>Login</h2>
<form action="UserValidationServlet" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="text" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
OUTPUT:
Program-6.1
Program 6.1
AIM: Write a Java program to insert data into a table using JSP.
CODE:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="[Link]" %>
<%@ page import="[Link]" %>
<html>
<head>
<title>Insert Data</title>
<style>
table {
width: 50%;
border-collapse: collapse;
margin: 25px 0;
font-size: 18px;
text-align: left;
}
th, td {
padding: 10px;
border: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h2>Insert User Data</h2>
<form action="[Link]" method="post">
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
<input type="submit" value="Insert">
</form>
<%
if([Link]().equalsIgnoreCase("POST")) {
String name = [Link]("name");
String email = [Link]("email");
List<String[]> users = (List<String[]>) [Link]("users");
if (users == null) {
users = new ArrayList<>();
}
[Link](new String[]{name, email});
[Link]("users", users);
[Link]("<h3>Data inserted successfully!</h3>");
}
List<String[]> users = (List<String[]>) [Link]("users");
if (users != null && ![Link]()) {
[Link]("<h3>Data:</h3>");
[Link]("<table>");
[Link]("<tr><th>Name</th><th>Email</th></tr>");
for (String[] user : users) {
[Link]("<tr><td>" + user[0] + "</td><td>" + user[1] + "</td></tr>");
}
[Link]("</table>");
}
%>
</body>
</html>
OUTPUT:
Program-6.2
AIM: Write JSP program to implement form data validation.
CODE:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Form Data Validation</title>
</head>
<body>
<h2>Form Data Validation</h2>
<form name="myForm" action="[Link]" method="post" onsubmit="return
validateForm()">
Name: <input type="text" name="name" value=""><br><br>
Email: <input type="email" name="email" value=""><br><br>
<input type="submit" value="Submit">
</form>
<%
// Server-side validation
String name = [Link]("name");
String email = [Link]("email");
if ([Link]().equalsIgnoreCase("POST")) {
boolean valid = true;
StringBuilder errorMessage = new StringBuilder();
if (name == null || [Link]().isEmpty()) {
[Link]("Name is required. <br>");
valid = false;
}
if (email == null || [Link]().isEmpty()) {
[Link]("Email is required. <br>");
valid = false;
}
if (email != null && ![Link]().isEmpty()) {
String emailPattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$";
if () {
[Link]("Please enter a valid email address. <br>");
valid = false;
}
}
if (valid) {
[Link]("<h3>Form Submitted Successfully!</h3>");
[Link]("<p><b>Name:</b> " + name + "</p>");
[Link]("<p><b>Email:</b> " + email + "</p>");
} else {
[Link]("<h3>Error:</h3>");
[Link]("<p>" + [Link]() + "</p>");
}
}
%>
</body>
</html>
OUTPUT:
Program-7.1
AIM: Create a database in MySQL using JSP and perform insertion and retrieval operations.
CODE:
[Link]
<%@ page import="[Link].*" %>
<html>
<head>
<title>Insert User</title>
</head>
<body>
<h2>Insert New User</h2>
<form action="[Link]" method="post">
<label for="name">Name: </label>
<input type="text" name="name" id="name" required /><br><br>
<label for="email">Email: </label>
<input type="email" name="email" id="email" required /><br><br>
<label for="address">Address: </label>
<input type="text" name="address" id="address" /><br><br>
<label for="mobile">Mobile: </label>
<input type="text" name="mobile" id="mobile" /><br><br>
<input type="submit" value="Submit" />
</form>
</body>
</html>
[Link]
<%@ page import="[Link].*" %>
<%
String name = [Link]("name");
String email = [Link]("email");
String address = [Link]("address");
String mobile = [Link]("mobile");
Connection con = null;
PreparedStatement stmt = null;
try {
[Link]("[Link]");
con = [Link]("jdbc:mysql://localhost:3306/user_db", "root", "");
String query = "INSERT INTO users (name, email, address, mobile) VALUES (?, ?, ?,
?)";
stmt = [Link](query);
[Link](1, name);
[Link](2, email);
[Link](3, address);
[Link](4, mobile);
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]("User has been inserted successfully!");
}
} catch (Exception e) {
[Link]();
[Link]("Error: " + [Link]());
} finally {
try {
if (stmt != null) [Link]();
if (con != null) [Link]();
} catch (SQLException se) {
[Link]();
}
}
%>
[Link]
<%@ page import="[Link].*" %>
<html>
<head>
<title>View Users</title>
</head>
<body>
<h2>All Users</h2>
<table border="1">
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>Mobile</th>
</tr>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
[Link]("[Link]");
con = [Link]("jdbc:mysql://localhost:3306/user_db", "root", "");
String query = "SELECT * FROM users";
stmt = [Link]();
rs = [Link](query);
while ([Link]()) {
String name = [Link]("name");
String email = [Link]("email");
String address = [Link]("address");
String mobile = [Link]("mobile");
%>
<tr>
<td><%= name %></td>
<td><%= email %></td>
<td><%= address %></td>
<td><%= mobile %></td>
</tr>
<%
}
} catch (Exception e) {
[Link]();
[Link]("Error: " + [Link]());
} finally {
try {
if (rs != null) [Link]();
if (stmt != null) [Link]();
if (con != null) [Link]();
} catch (SQLException se) {
[Link]();
}
}
%>
</table>
</body>
</html>
OUTPUT:
Program-7.2
AIM: Create a Java JSP login and Sign Up form with Session using MySQL.
CODE:
[Link]
<html>
<head>
<title>Sign-Up</title>
</head>
<body>
<h2>Sign Up</h2>
<form action="[Link]" method="post">
<label for="name">Name: </label>
<input type="text" name="name" id="name" required /><br><br>
<label for="email">Email: </label>
<input type="email" name="email" id="email" required /><br><br>
<label for="address">Address: </label>
<input type="text" name="address" id="address" /><br><br>
<label for="mobile">Mobile: </label>
<input type="text" name="mobile" id="mobile" /><br><br>
<label for="password">Password: </label>
<input type="password" name="password" id="password" required /><br><br>
<input type="submit" value="Sign Up" />
</form>
</body>
</html>
[Link]
<%@ page import="[Link].*%>
<%
String name = [Link]("name");
String email = [Link]("email");
String password = [Link]("password");
String address = [Link]("address");
String mobile = [Link]("mobile");
String url = "jdbc:mysql://localhost:3306/user_db";
String user = "root";
String dbPassword = "";
Connection con = null;
PreparedStatement stmt = null;
try {
[Link]("[Link]");
con = [Link](url, user, dbPassword);
String query = "INSERT INTO users (name, email, password, address, mobile) VALUES
(?, ?, ?, ?, ?)";
stmt = [Link](query);
[Link](1, name);
[Link](2, email);
[Link](3, password);
[Link](4, address);
[Link](5, mobile);
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]("User registered successfully!");
[Link]("[Link]");
}
} catch (Exception e) {
[Link]();
[Link]("Error: " + [Link]());
} finally {
try {
if (stmt != null) [Link]();
if (con != null) [Link]();
} catch (SQLException se) {
[Link]();
}
}
%>
[Link]
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="[Link]" method="post">
<label for="email">Email: </label>
<input type="email" name="email" id="email" required /><br><br>
<label for="password">Password: </label>
<input type="password" name="password" id="password" required /><br><br>
<input type="submit" value="Login" />
</form>
</body>
</html>
[Link]
<%@ page import="[Link].*, [Link].*, [Link].*" %>
<%
String email = [Link]("email");
String password = [Link]("password");
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
[Link]("[Link]");
con = [Link]("jdbc:mysql://localhost:3306/user_db", "root", "");
String query = "SELECT * FROM users WHERE email = ? AND password = ?";
stmt = [Link](query);
[Link](1, email);
[Link](2, password);
rs = [Link]();
if ([Link]()) {
[Link]("email", email);
[Link]("[Link]");
} else {
[Link]("Invalid email or password.");
}
} catch (Exception e) {
[Link]();
[Link]("Error: " + [Link]());
} finally {
try {
if (rs != null) [Link]();
if (stmt != null) [Link]();
if (con != null) [Link]();
} catch (SQLException se) {
[Link]();
}
}
%>
[Link]
<%@ page import="[Link].*, [Link].*" %>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome, <%= [Link]("email") %>!</h2>
<a href="[Link]">Logout</a>
</body>
</html>
[Link]
<%@ page import="[Link].*, [Link].*" %>
<%
[Link]();
[Link]("[Link]");
%>
OUTPUT:
Program - 8.1
AIM: Create Employee Registration Form using a combination of JSP, Servlet, JDBC and
MySQL database.
CODE:
[Link]
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Register Employee</title></head>
<body>
<h2>Register New Employee</h2>
<form action="${[Link]}/week8/register" method="post">
Name: <input type="text" name="name"><br><br>
Email: <input type="email" name="email"><br><br>
Department: <input type="text" name="department"><br><br>
Salary: <input type="number" step="0.01" name="salary"><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
[Link]
package week8;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
@WebServlet("/week8/register")
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = [Link]("name");
String email = [Link]("email");
String department = [Link]("department");
double salary = [Link]([Link]("salary"));
[Link]("text/html");
PrintWriter out = [Link]();
try {
[Link]("[Link]");
Connection conn = [Link](
"jdbc:mysql://localhost:3306/employee_db", "root", "");
String sql = "INSERT INTO employees (name, email, department, salary) VALUES
(?, ?, ?, ?)";
PreparedStatement stmt = [Link](sql);
[Link](1, name);
[Link](2, email);
[Link](3, department);
[Link](4, salary);
int rows = [Link]();
if (rows > 0) {
[Link]("<h3>Employee registered successfully!</h3>");
} else {
[Link]("<h3>Failed to register employee.</h3>");
}
[Link]();
} catch (Exception e) {
[Link]("<p>Error: " + [Link]() + "</p>");
[Link](out);
}
}
}
OUTPUT:
Program-8.2
AIM: Develop a small web program using Servlets, JSPs with Database connectivity.
CODE:
Employee Registration System
[Link]
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Register Employee</title></head>
<body>
<h2>Register New Employee</h2>
<form action="${[Link]}/week8/register" method="post">
Name: <input type="text" name="name"><br><br>
Email: <input type="email" name="email"><br><br>
Department: <input type="text" name="department"><br><br>
Salary: <input type="number" step="0.01" name="salary"><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
[Link]
package week8;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
@WebServlet("/week8/register")
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = [Link]("name");
String email = [Link]("email");
String department = [Link]("department");
double salary = [Link]([Link]("salary"));
[Link]("text/html");
PrintWriter out = [Link]();
try {
[Link]("[Link]");
Connection conn = [Link](
"jdbc:mysql://localhost:3306/employee_db", "root", "");
String sql = "INSERT INTO employees (name, email, department, salary) VALUES
(?, ?, ?, ?)";
PreparedStatement stmt = [Link](sql);
[Link](1, name);
[Link](2, email);
[Link](3, department);
[Link](4, salary);
int rows = [Link]();
if (rows > 0) {
[Link]("<h3>Employee registered successfully!</h3>");
} else {
[Link]("<h3>Failed to register employee.</h3>");
}
[Link]();
} catch (Exception e) {
[Link]("<p>Error: " + [Link]() + "</p>");
[Link](out);
}
}
}
[Link]
<%@ page import="[Link].*" %>
<%
[Link]("[Link]");
Connection conn
= [Link]("jdbc:mysql://localhost:3306/employee_db", "root", "");
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM employees");
%>
<!DOCTYPE html>
<html>
<head>
<title>Employee List</title>
</head>
<body>
<h2>Employee Records</h2>
<table border="1">
<tr><th>ID</th><th>Name</th><th>Email</th><th>Department</th><th>Salary</th>
</tr>
<%
while ([Link]()) {
%>
<tr>
<td><%= [Link]("id") %></td>
<td><%= [Link]("name") %></td>
<td><%= [Link]("email") %></td>
<td><%= [Link]("department") %></td>
<td><%= [Link]("salary") %></td>
</tr>
<%
}
[Link]();
%>
</table>
</body>
</html>
OUTPUT:
Program 9.1
AIM: Create an RMI application where the server hosts a calculator service, and the client can
perform basic arithmetic operations remotely.
CODE:
[Link]
import [Link];
import [Link];
public interface Calculator extends Remote {
double add(double a, double b) throws RemoteException;
double subtract(double a, double b) throws RemoteException;
double multiply(double a, double b) throws RemoteException;
double divide(double a, double b) throws RemoteException;
}
[Link]
import [Link];
import [Link];
public class CalculatorImpl extends UnicastRemoteObject implements Calculator {
protected CalculatorImpl() throws RemoteException {
super();
}
public double add(double a, double b) throws RemoteException {
return a + b;
}
public double subtract(double a, double b) throws RemoteException {
return a - b;
}
public double multiply(double a, double b) throws RemoteException {
return a * b;
}
public double divide(double a, double b) throws RemoteException {
if (b == 0) throw new ArithmeticException("Cannot divide by zero");
return a / b;
}
}
[Link]
import [Link];
public class Client {
public static void main(String[] args) {
try {
Calculator calc = (Calculator)
[Link]("rmi://localhost:1099/CalculatorService");
[Link]("Addition: " + [Link](5, 3));
[Link]("Subtraction: " + [Link](10, 4));
[Link]("Multiplication: " + [Link](2, 6));
[Link]("Division: " + [Link](20, 5));
} catch (Exception e) {
[Link]();
}
}
}
[Link]
import [Link];
import [Link];
public class Server {
public static void main(String[] args) {
try {
[Link](1099);
Calculator calc = new CalculatorImpl();
[Link]("rmi://localhost:1099/CalculatorService", calc);
[Link]("Calculator RMI Server is running...");
} catch (Exception e) {
[Link]();
}
}
}
OUTPUT: