1.
DISPLAY A WELCOME MESSAGE USING
SERVLET
Program :
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/welcome")
public class WelcomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html”);
PrintWriter out = [Link]();
[Link]("<html><body>");
[Link]("<h1>Welcome to the Servlet World!</h1>");
[Link]("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}}
1
OUTPUT:
2
[Link] A PURCHASE ORDER FORM USING
HTML FORM AND SERVLET
Program :
(html code)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Purchase Order Form</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
form {
max-width: 400px;
margin: auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
input[type="text"], input[type="number"], input[type="date"] {
3
width: 100%;
padding: 8px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h2>Purchase Order Form</h2>
<form action="processOrder" method="post">
<label for="customerName">Customer Name:</label>
<input type="text" id="customerName" name="customerName" required>
<label for="productName">Product Name:</label>
<input type="text" id="productName" name="productName" required>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1" required>
4
<label for="price">Price per Unit:</label>
<input type="number" id="price" name="price" min="0" step="0.01" required>
<label for="orderDate">Order Date:</label>
<input type="date" id="orderDate" name="orderDate" required>
<input type="submit" value="Submit Order">
</form>
</body>
</html>
(servlet code)
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/processOrder")
public class ProcessOrderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
String customerName = [Link]("customerName");
String productName = [Link]("productName");
int quantity = [Link]([Link]("quantity"));
5
double price = [Link]([Link]("price"));
String orderDate = [Link]("orderDate");
double totalCost = quantity * price;
PrintWriter out = [Link]();
[Link]("<html><body>");
[Link]("<h2>Order Details</h2>");
[Link]("<p><strong>Customer Name:</strong>" + customerName + "</p>");
[Link]("<p><strong>Product Name:</strong>" + productName + "</p>");
[Link]("<p><strong>Quantity:</strong>" + quantity + "</p>");
[Link]("<p><strong>Price per Unit:</strong> $" + price + "</p>");
[Link]("<p><strong>Order Date:</strong>" + orderDate + "</p>");
[Link]("<p><strong>Total Cost:</strong> $" + totalCost + "</p>");
[Link]("</body></html>");
}
}
6
OUTPUT:
7
[Link] A PROGRAM FOR CALCULATINGTHE PERCENTAGE
OF MARKS OF ASTUDENT USING JSP
Program :
([Link])
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8"%>
<!DOCTYPE html>
<html>
<head>
<title>Calculate Percentage</title>
</head>
<body>
<h2>Enter Student Marks</h2>
<form action="[Link]" method="post">
Name :<input type=”text” name=”name” required><br><br>
Roll No :<input type=”number” name=”roll no” required><br><br>
Subject 1: <input type="number" name="subject1" required><br><br>
Subject 2: <input type="number" name="subject2" required><br><br>
Subject 3: <input type="number" name="subject3" required><br><br>
Subject 4: <input type="number" name="subject4" required><br><br>
Subject 5: <input type="number" name="subject5" required><br><br>
<input type="submit" value="Calculate Percentage">
</form>
</body>
</html>
([Link])
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8"%>
<!DOCTYPE html>
8
<html>
<head>
<title>Percentage Result</title>
</head>
<body>
<h2>Percentage Calculation Result</h2>
<%
int subject1 = [Link]([Link]("subject1"));
int subject2 = [Link]([Link]("subject2"));
int subject3 = [Link]([Link]("subject3"));
int subject4 = [Link]([Link]("subject4"));
int subject5 = [Link]([Link]("subject5"));
int totalMarks = subject1 + subject2 + subject3 + subject4 + subject5;
double percentage = (totalMarks / 500.0) * 100;
[Link]("Name: " + name+ "<br>");
[Link]("Roll No: " +roll no + "<br>");
[Link]("Subject 1: " + subject1 + "<br>");
[Link]("Subject 2: " + subject2 + "<br>");
[Link]("Subject 3: " + subject3 + "<br>");
[Link]("Subject 4: " + subject4 + "<br>");
[Link]("Subject 5: " + subject5 + "<br>");
[Link]("Total Marks: " + totalMarks + "<br>");
[Link]("Percentage: " + percentage + "%<br>");
%>
<a href="[Link]">Calculate Again</a>
</body>
</html>
9
OUTPUT:
10
4. DESIGN A PURCHASE ORDER FORM USING
HTML FORM AND JSP
Program :
([Link])
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Purchase Order Form</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
form {
max-width: 400px;
margin: auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
input[type="text"], input[type="number"], input[type="date"] {
11
width: 100%;
padding: 8px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h2>Purchase Order Form</h2>
<form action="[Link]" method="post">
<label for="customerName">Customer Name:</label>
<input type="text" id="customerName" name="customerName" required>
<label for="productName">Product Name:</label>
<input type="text" id="productName" name="productName" required>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1" required
<label for="price">Price per Unit:</label>
<input type="number" id="price" name="price" min="0" step="0.01" required>
<label for="orderDate">Order Date:</label>
12
<input type="date" id="orderDate" name="orderDate" required>
<input type="submit" value="Submit Order">
</form>
</body>
</html>
([Link])
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Order Details</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.order-details {
max-width: 400px;
margin: auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
h2 {
color: #4CAF50;
}
13
</style>
</head>
<body>
<div class="order-details">
<h2>Order Details</h2>
<%
String customerName = [Link]("customerName");
String productName = [Link]("productName");
int quantity = [Link]([Link]("quantity"));
double price = [Link]([Link]("price"));
String orderDate = [Link]("orderDate");
double totalCost = quantity * price;
%>
<p><strong>Customer Name:</strong><%= customerName %></p>
<p><strong>Product Name:</strong><%= productName %></p>
<p><strong>Quantity:</strong><%= quantity %></p>
<p><strong>Price per Unit:</strong> $<%= [Link]("%.2f", price) %></p>
<p><strong>Order Date:</strong><%= orderDate %></p>
<p><strong>Total Cost:</strong> $<%= [Link]("%.2f", totalCost) %></p>
</div>
</body>
</html>
14
OUTPUT :
15
5. PREPARE A EMPLOYEE PAY SLIP USING
JSP
Program :
([Link])
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Employee Pay Slip Form</title>
</head>
<body>
<h2>Employee Pay Slip Generator</h2>
<form action="[Link]" method="post">
Employee ID: <input type="text" name="empId"><br><br>
Employee Name: <input type="text" name="empName"><br><br>
Basic Salary: <input type="number" name="basicSalary"><br><br>
<input type="submit" value="Generate Pay Slip">
</form>
</body>
</html>
([Link])
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Employee Pay Slip</title>
<style>
table { border-collapse: collapse; width: 50%; }
th, td { border: 1px solid black; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
16
</style>
</head>
<body>
<h2>Employee Pay Slip</h2>
<%
String empId = [Link]("empId");
String empName = [Link]("empName");
double basicSalary = [Link]([Link]("basicSalary"));
double hra = basicSalary * 0.40; // 40% of basic
double da = basicSalary * 0.25; // 25% of basic
double grossSalary = basicSalary + hra + da;
double deductions = pf + tax;
double netSalary = grossSalary - deductions;
%>
<table>
<tr>
<th>Employee ID:</th>
<td><%= empId %></td>
</tr>
<tr>
<th>Employee Name:</th>
<td><%= empName %></td>
</tr>
<tr>
<th>Basic Salary:</th>
<td><%= [Link]("₹%.2f", basicSalary) %></td>
</tr>
<tr>
<th>HRA (40%):</th>
<td><%= [Link]("₹%.2f", hra) %></td>
17
</tr>
<tr>
<th>DA (25%):</th>
<td><%= [Link]("₹%.2f", da) %></td>
</tr>
<tr>
<th>Gross Salary:</th>
<td><%= [Link]("₹%.2f", grossSalary) %></td>
</tr>
<tr>
<th>Total Deductions:</th>
<td><%= [Link]("₹%.2f", deductions) %></td>
</tr>
<tr>
<th>Net Salary:</th>
<td><%= [Link]("₹%.2f", netSalary) %></td>
</tr>
</table>
</body>
</html>
18
OUTPUT:
19
6. WRITE A PROGRAM USING JDBC FOR CREATING A
TABLE,INSERTING, DELETING RECORDS AND LIST OUT THERE
CORDS
Program :
import [Link].*;
import [Link];
public class JdbcProgram {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydb";
String username = "your_username";
String password = "your_password";
try (Connection conn = [Link](url, username, password)) {
Statement stmt = [Link]();
[Link]("CREATE TABLE IF NOT EXISTS employees (" +
"id INT AUTO_INCREMENT PRIMARY KEY, " +
"name VARCHAR(255), " +
"age INT)");
Scanner scanner = new Scanner([Link]);
while (true) {
[Link]("1. Insert Record");
[Link]("2. Update Record");
[Link]("3. Delete Record");
[Link]("4. View All Records");
[Link]("5. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter name: ");
String name = [Link]();
20
[Link]("Enter age: ");
int age = [Link]();
[Link]("INSERT INTO employees (name, age) VALUES ('" + name + "', "
+ age + ")");
break;
case 2:
[Link]("Enter ID to update: ");
int id = [Link]();
[Link]("Enter new name: ");
name = [Link]();
[Link]("Enter new age: ");
age = [Link]();
[Link]("UPDATE employees SET name = '" + name + "', age = " + age + "
WHERE id = " + id);
[Link]("Record updated!");
break;
case 3:
[Link]("Enter ID to delete: ");
id = [Link]();
[Link]("DELETE FROM employees WHERE id = " + id);
[Link]("Record deleted!");
break;
case 4:
ResultSet rs = [Link]("SELECT * FROM employees");
while ([Link]()) {
[Link]("ID: " + [Link]("id"));
[Link]("Name: " + [Link]("name"));
[Link]("Age: " + [Link]("age"));
[Link]();
}
break;
case 5:
21
[Link](0);
break;
default:
[Link]("Invalid choice!");
}
}
} catch (SQLException e) {
[Link]("Error: " + [Link]());
}
}
}
22
OUTPUT:
23
24
7. WRITE A PROGRAM USING JAVA SERVELT
TO HANDLE FORM DATA
Program :
([Link])
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Registration Form</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
form {
max-width: 400px;
margin: auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
input[type="text"], input[type="email"], input[type="number"] {
25
width: 100%;
padding: 8px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h2>User Registration Form</h2>
<form action="handleForm" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="1" required>
<input type="submit" value="Submit">
</form>
</body> </html>
26
([Link])
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/handleForm")
public class FormHandlerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
String name = [Link]("name");
String email = [Link]("email");
int age = [Link]([Link]("age"));
PrintWriter out = [Link]();
[Link]("<html><body>");
[Link]("<h2>Form Submission Result</h2>");
[Link]("<p><strong>Name:</strong>" + name + "</p>");
[Link]("<p><strong>Email:</strong>" + email + "</p>");
[Link]("<p><strong>Age:</strong>" + age + "</p>");
[Link]("</body></html>");
}
}
27
OUTPUT:
28
[Link] A SIMPLE SERVELT PROGRAM TO CREATE A TABLE
OF ALL THE HEADERS IT RECEIVES ALONG WITH THEIR
ASSOCIATED VALUES
Program :
([Link])
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/headers")
public class HeaderTableServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html><head><title>Request Headers</title>");
[Link]("<style>");
[Link]("table { width: 50%; border-collapse: collapse; margin: 20px auto; }");
[Link]("th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }");
[Link]("th { background-color: #f2f2f2; }");
[Link]("</style>");
29
[Link]("</head><body>");
[Link]("<h2 style='text-align: center;'>Request Headers</h2>");
[Link]("<table>");
[Link]("<tr><th>Header Name</th><th>Header Value</th></tr>");
Enumeration<String> headerNames = [Link]();
while ([Link]()) {
String headerName = [Link]();
String headerValue = [Link](headerName);
[Link]("<tr><td>" + headerName + "</td><td>" + headerValue + "</td></tr>");
}
[Link]("</table>");
[Link]("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
30
OUTPUT:
31
9. WRITE A PROGRAM IN JSP USING SESSION
OBJECT
Program :
([Link])
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
<style>
h1{
font-size: 300%;
}
body{
text-align: center;
background-color:#ccffcc;
}
</style>
</head>
<body>
<h1>Student Login</h1>
<%
String userName = [Link]("userName");
String password = [Link]("password");
session = [Link](true);
if ("periyaiya".equals(userName) && "23mce3912".equals(password)) {
[Link]("userName", userName);
[Link]("[Link]");
} else if (userName != null || password != null) {
[Link]("<p>Invalid username or password. Try again.</p>");
32
}
if ([Link]("userName") == null) {
%>
<form action="[Link]" method="POST">
Username: <input type="text" name="userName" placeholder="Enter UserName"
required=""><br>
Password: <input type="password" name="password" placeholder="Enter Password"
required=""><br>
<input type="submit" value="Login">
</form>
<%
}
%>
</body>
</html>
([Link])
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
session = [Link](false); // Don't create if it doesn't exist
if (session == null || [Link]("userName") == null) {
// Not logged in, redirect to [Link]
[Link]("[Link]");
} else {
String userName = (String) [Link]("userName");
%>
<html>
<head>
<title>Welcome</title>
<style>
body{
background-color:whitesmoke;
text-align: center;
33
}
table {
width: 80%;
border-collapse: collapse;
margin:20px auto;
}
th,td{
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th{
background-color:aqua;
}
</style>
</head>
<body>
<h2>Welcome, <%= userName %>!</h2>
<p>*Login Successfully*</p>
<section>
<h2>UG Education Details:</h2>
<table>
<tbody><tr>
<th>Degree</th>
<th>University</th>
<th>Year</th>
<th>Percentage</th>
</tr>
<tr>
<td>Bca</td>
<td>Alagappa University</td>
<td>2020-2023</td>
34
<td>80%</td>
</tr>
</tbody></table>
<h1>School Education Details:</h1>
<table>
<tbody><tr>
<th>School Name</th>
<th>Govt\Pvt</th>
<th>Year</th>
<th>Percentage</th>
</tr>
<tr>
<td>Alagappa Model</td>
<td>Govt</td>
<td>2018-2020</td>
<td>90%</td>
</tr>
</tbody></table>
</section>
</body>
</html>
<%
}
%>
35
OUTPUT :
36
[Link] A PROGRAM TO BUILD A SIMPLE CLIENT SERVER
APPLICATION USING RMI
Program :
([Link])
import [Link];
import [Link];
public interface InterestCalculator extends Remote {
double calculateInterest(double principal, double rate, double time) throws RemoteException;
}
([Link])
import [Link];
import [Link];
import [Link];
import [Link];
public class InterestCalculatorImpl extends UnicastRemoteObject implements
InterestCalculator {
protected InterestCalculatorImpl() throws RemoteException {
super();
}
@Override
public double calculateInterest(double principal, double rate, double time) throws
RemoteException {
return (principal * rate * time) / 100;
}
public static void main(String[] args) {
try {
InterestCalculatorImpl obj = new InterestCalculatorImpl();
Registry registry = [Link](1099);
[Link]("InterestCalculator", obj);
37
[Link]("Interest Calculator Server is ready!");
} catch (Exception e) {
[Link]("Server exception: " + [Link]());
[Link]();
}
}
}
([Link])
import [Link];
import [Link];
import [Link];
public class InterestCalculatorClient {
public static void main(String[] args) {
try {
Registry registry = [Link]("localhost", 1099);
InterestCalculator stub = (InterestCalculator) [Link]("InterestCalculator");
Scanner scanner = new Scanner([Link]);
[Link]("Enter principal amount: ");
double principal = [Link]();
[Link]("Enter rate of interest: ");
double rate = [Link]();
[Link]("Enter time (in years): ");
double time = [Link]();
double interest = [Link](principal, rate, time);
[Link]("Simple Interest: " + interest);
} catch (Exception e) {
[Link]("Client exception: " + [Link]());
[Link]();
}}}
38
OUTPUT:
39
[Link] AN APPLET FOR A CALCULATOR
APPLICATION
Program :
(SimpleCalculatorApplet .java)
import [Link];
import [Link].*;
import [Link].*;
public class SimpleCalculatorApplet extends Applet {
TextField display;
String expression = "";
public void init() {
// Set the layout for the Applet
setLayout(new BorderLayout());
// Create the display TextField (like the HTML input box)
display = new TextField();
[Link](false); // The display is not editable directly
add(display, [Link]);
// Create a Panel for the calculator buttons
Panel panel = new Panel();
[Link](new GridLayout(4, 4, 5, 5)); // 4x4 grid for buttons
// Create and add the buttons
String[] buttons = {
"1", "2", "3", "+",
"4", "5", "6", "-",
"7", "8", "9", "*",
"C", "0", "=", "/"
};
for (String button : buttons) {
Button b = new Button(button);
[Link](new ButtonClickListener());
40
[Link](b);
}
add(panel, [Link]);
}
// ActionListener to handle button clicks
class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String cmd = [Link]();
if ([Link]("=")) {
calculate();
} else if ([Link]("C")) {
expression = "";
[Link](expression);
} else {
expression += cmd;
[Link](expression);
}
}
}
// Calculate the result
private void calculate() {
try {
// Using Java's scripting engine to evaluate the expression
[Link] mgr = new [Link]();
[Link] engine = [Link]("JavaScript");
Object result = [Link](expression);
[Link]([Link]());
expression = [Link]();
} catch (Exception ex) {
[Link]("Error");
} }}
41
OUTPUT :
42
12. PROGRAM TO SEND A TEXT MESSAGE TO ANOTHER
SYSTEM AND RECEIVE THE TEXT MESSAGE FROM THE
SYSTEM (USE SOCKET PROGRAMMING)
Program :
([Link])
import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12345);
[Link]("Server is waiting for client request...");
Socket socket = [Link]();
[Link]("Client connected.");
BufferedReader input = new BufferedReader
(new InputStreamReader([Link]()));
PrintWriter output = new PrintWriter([Link](), true);
String clientMessage = [Link]();
[Link]("Received from client: " + clientMessage);
String serverResponse = "Hello Client, I received your message: " + clientMessage;
[Link](serverResponse);
[Link]();
[Link]();
[Link]();
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
43
([Link])
import [Link].*;
import [Link].*;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 12345);
BufferedReader input = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter output = new PrintWriter([Link](), true);
BufferedReader userInput = new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter a message for the server: ");
String message = [Link]();
[Link](message);
String serverResponse = [Link]();
[Link]("Response from server: " + serverResponse);
[Link]();
[Link]();
[Link]();
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
44
OUTPUT :
45