Practical 2 AJAVA
Practical 2 AJAVA
Roll No:5287
Class:SYCS-A
Practical 2
AIM : Reading HTML Form Data in Servlet
1] Create a simple application to add and subtract two numbers using servlet.
Pseudo Code:
Step 1 : Create new JAVA Web project in the Netbeans with name "5287satya"
Step 2 : In Index.html file specify the form tag with action as " AddSubtractServlet " inside which
add two two input fileds to take the input from the user with name as "num1" & "num2" (these
are name attribute of input tag)
STep 3 : Create New Servlet in the project with name " AddSubtractServlet "
Step 4 : In Servlet file Metion the doPost() to read the inputes from request by using
getParameter() and store in firstnumber & secondnumber( these are variables name) respectively
Step 5 : use arithematic operator + & - to perform the task on
firstnumber & secondnumber
Step 6 : Print the final result by using out object of PrintWritter in form of HTML using its tags
Step 7 : Run index.html file
Step 8 : Enter two numbers and click on "Add & Sub" button
Step 9 : This will show the addition and Substraction of numbers entred by the user
Code:
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Two Numbers</title>
</head>
<body>
<h2>Add Two Numbers</h2>
<form action="AddSubtractServlet " method="post">
<label for="num1">Enter first number:</label>
<input type="number" id="num1" name="num1" required><br><br>
</form>
</body>
</html>
AddSubtractServlet.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("/AddSubtractServlet")
public class AddSubtractServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Results</title></head><body>");
out.println("<h2>Addition Result:</h2>");
out.println("<p>" + addResult + "</p>");
out.println("<h2>Subtraction Result:</h2>");
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
2] Create a simple application to Multiply and divide two numbers using servlet.
Pseudo Code:
Step 1 : Create new JAVA Web project in the Netbeans with name "5287satya"
Step 2 : In Index.html file specify the form tag with action as " MulDivServlet " inside which add two
two input fileds to take the input from the user with name as "num1" & "num2" (these are name attribute
of input tag)
STep 3 : Create New Servlet in the project with name " MulDivServlet".
Step 4 : In Servlet file Metion the doPost() to read the inputes from request by using getParameter() and
store in firstnumber & secondnumber( these are variables name) respectively
Step 5 : use arithematic operator * & / to perform the task on
firstnumber & secondnumber
Step 6 : Print the final result by using out object of PrintWritter in form of HTML using its tags
Step 7 : Run multidivide.html file
Step 8 : Enter two numbers and click on "Mul & Div" button
Step 9 : This will show the Multiplication and Division of numbers entred by the user
Code:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Two Numbers</title>
</head>
<body>
<h2>Add Two Numbers</h2>
<form action="MulDivServlet " method="post">
<label for="num1">Enter first number:</label>
<input type="number" id="num1" name="num1" required><br><br>
</html>
MulDivServlet.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("/MulDivServlet")
public class MulDivServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Results</title></head><body>");
out.println("<h2>Multiplication Result:</h2>");
out.println("<p>" + mulResult + "</p>");
out.println("<h2>Division Result:</h2>");
out.println("<p>" + divResult + "</p>");
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
out.println("</body></html>");
}
}
Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
Pseudo Code:
1. Create a new Java Web Project in NetBeans with the name "5287satya".
2. In the index.html file, specify a <form> tag with action="CalculatorServlet", and add
two <input> fields with name attributes as "Number1" and "Number2".
3. Add four buttons to the form with name="operation" and value as: "Add" for addition
"Subtract" for subtraction "Multilpy" for multiplication "Divide" for division
4. Create a new Servlet in the project with the name "CalculatorServlet".
5. In the Servlet file, implement the doPost() method to:
• Read inputs from the request using getParameter() and store them in variables
firstnumber and secondnumber.
• Read the operation using getParameter("operation") and store it in a variable operation.
6. Use a conditional structure to perform the respective operation:
• If operation = "Add", perform addition.
• If operation = "Subtract", perform subtraction.
• If operation = "Multiply", perform multiplication.
• If operation = "Divide", perform division.
7. Print the result using the out object of PrintWriter in HTML format with proper tags.
8. Run the index.html file.
9. Enter two numbers and click one of the buttons (Add, Subtract, Multiply, or Divide).
10. View the result of the selected operation displayed on the browser
Code:
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body>
<h2>Calculator</h2>
<form action="CalculatorServlet" method="post">
<label for="num1">Number 1:</label>
<input type="number" id="num1" name="num1" required><br><br>
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
CalculatorServlet.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("/calculate")
public class CalculatorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
int result = 0;
String operator = "";
switch(operation) {
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
case "Add":
result = num1 + num2;
operator = "+";
break;
case "Subtract":
result = num1 - num2;
operator = "-";
break;
case "Multiply":
result = num1 * num2;
operator = "*";
break;
case "Divide":
if (num2 != 0) {
result = num1 / num2;
operator = "/";
} else {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Error</title></head><body>");
out.println("<h2>Error: Division by zero</h2>");
out.println("</body></html>");
return;
}
break;
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Result</title></head><body>");
out.println("<h2>Result:</h2>");
out.println("<p>" + num1 + " " + operator + " " + num2 + " = " + result + "</p>");
out.println("</body></html>");
}
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
4] Create a Java servlet that calculates the factorial of a number provided by the user
through an HTML form.
Pseudo Code:
1. Create a new Java Web Project in NetBeans with the name "rohit5364".
2. In the index.html file, specify a <form> tag with action="rohit_prac2D", and add one
<input> field with the name attribute as "Enter a number" to accept the input number.
3. Add a button to the form:
• <button type="submit">Calculate Factorial</button>
4. Create a new Servlet in the project with the name "rohit_prac2D".
5. In the Servlet file, implement the doPost() method to:
• Read the input from the request using getParameter() and store it in a variable number.
• Convert the input to an integer and calculate the factorial using a loop or recursion.
6. Print the result using the out object of PrintWriter in HTML format with proper tags.
7. Run the index.html file.
8. Enter a number and click the "Calculate Factorial" button.
9. View the calculated factorial displayed on the browser.
Code:
index.html
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<h2>Factorial Calculator</h2>
<form action="FactorialServlet" method="get">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<button type="submit">Calculate Factorial</button>
</form>
</body>
</html>
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
FactorialServlet.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("/factorial")
public class FactorialServlet extends HttpServlet {
int number;
try {
// Convert the input parameter to an integer
number = Integer.parseInt(numberStr);
} catch (NumberFormatException e) {
out.println("<h2>Please provide a valid number.</h2>");
return;
}
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
Output:
5] Create a Java servlet to find whether the entered number is positive ,negative or zero
provided by the user through an HTML form.
Pseudo Code:
1. Create a new Java Web Project in NetBeans with the name "rohit".
2. In the index.html file, specify a <form> tag with action="program5", and add one <input>
field with the name attribute as "number" to accept the input number.
3. Add a button to the form:
<button type="submit">Check Number</button>
4. Create a new Servlet in the project with the name "rohit_checkNumber".
5. In the Servlet file, implement the doPost() method to:
• Read the input from the request using getParameter() and store it in a variable number.
• Convert the input to an integer.
• Use conditional statements to determine if the number is positive, negative, or zero.
6. Print the result using the out object of PrintWriter in HTML format with proper tags.
7. Run the index.html file.
8. Enter a number and click the "Check Number" button.
9. View the result ("Positive", "Negative", or "Zero") displayed on the browser.
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
Code:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Number Checker</title>
</head>
<body>
<h1>Check if a Number is Positive, Negative, or Zero</h1>
<form action="CheckNumberServlet" method="post">
<label for="number">Enter a number:</label>
CheckNumberServlet.java
import java.io.IOException;
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 java.io.PrintWriter;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.close();
}
}
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
6] Create a Java servlet to find whether the entered number is positive ,negative or zero
provided by the user through an HTML form.
Pseudo Code:
1. Create a new Java Web Project in NetBeans with the name "rohit".
2. In the index.html file, specify a <form> tag with action="program6", and add one <input>
field with the name attribute as "Enter a number" to accept the input number.
3. Add a button to the form:
<button type="submit">Prime</button>
4. Create a new Servlet in the project with the name "program6".
5. In the Servlet file, implement the doPost() method to:
• Read the input from the request using getParameter() and store it in a variable number.
• Convert the input to an integer.
• Write a method or use logic in doPost() to check if the number is prime:
• A number is prime if it is greater than 1 and divisible only by 1 and itself.
• Return whether the number is prime or not.
6. Print the result using the out object of PrintWriter in HTML format with proper tags.
7. Run the index.html file.
8. Enter a number and click the "Check Prime" button.
9. View the result ("Prime" or "Not Prime") displayed on the browser.
Code:
index.html
<!DOCTYPE html>
<html>
<head>
<title>Prime Checker</title>
</head>
<body>
<h2>Prime Checker</h2>
<form action="prime-checker" method="get">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<button type="submit">Prime</button>
</form>
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
</body>
</html>
PrimeCheckerServlet.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("/prime-checker")
public class PrimeCheckerServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Prime Number Checker</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Prime Number Checker</h2>");
if (isPrime) {
out.println("<p>" + number + " is a prime number.</p>");
} else {
out.println("<p>" + number + " is not a prime number.</p>");
}
out.println("</body>");
out.println("</html>");
Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
Code:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
ArmstrongServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/checkArmstrong")
public class ArmstrongServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
try {
// Get the number from the request
int number = Integer.parseInt(request.getParameter("number"));
int originalNumber = number;
int sum = 0;
int n = String.valueOf(number).length(); // Get the number of digits
8] Create a Java servlet to find the entered number is even or odd provided by the user
through an HTML form.
Pseudo Code:
1. Create a new Java Web Project in NetBeans with the name "rohit".
2. In the index.html file, specify a <form> tag with action="program8", and add one
<input>field with the name attribute as "Enter a number" to accept the input number.
3. Add a button to the form:
• <button type="submit">Find</button>
4. Create a new Servlet in the project with the name "program8".
5. In the Servlet file, implement the doPost() method to:
• Read the input from the request using getParameter() and store it in a variable
number.
• Convert the input to an integer.
• Use a conditional statement to check if the number is divisible by 2:
• If divisible by 2, the number is even.
• Otherwise, the number is odd.
6. Print the result using the out object of PrintWriter in HTML format with proper tags.
7. Run the index.html file.
8. Enter a number and click the "Find" button.
9. View the result ("Even number" or "Odd number") displayed on the browser.
Code:
index.html
<!DOCTYPE html>
<html>
<head>
<title>Even odd Checker</title>
</head>
<body>
<h2>Even Odd</h2>
EvenOddCheckerServlet.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("/evenodd-checker")
public class EvenOddCheckerServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<head>");
out.println("<title>Even or Odd Number Checker</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Even or Odd Number Checker</h2>");
out.println("<p>The number " + number + " is " + result + ".</p>");
out.println("</body>");
out.println("</html>");
}
Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
9] Create a Java servlet to generate the fibonacci series as nth term provided by the user
through an HTML form.
Pseudo Code:
1. Create a new Java Web Project in NetBeans with the name "rohit".
2. In the index.html file, create a <form> tag with action="program9" and method="post".
3. Add an <input> field with the name="n" to accept the number of terms (n) for the Fibonacci
series.
4. Add a submit button: <button type="submit">Generate Fibonacci</button>.
5. Create a new Servlet in the project with the name "program9".
• In the Servlet doPost() method:
• Retrieve the input number n using request.getParameter("n").
• Convert the input to an integer.
• Initialize variables:
• first = 0
• second = 1
• nextTerm = 0
Use a loop to generate the Fibonacci series up to the nth term:
• Print the values of first and second for the first two terms.
• For terms from 3 to n, calculate the next term as nextTerm = first + second and update
first and second.
5. Print the result using the out object of PrintWriter:
6. Run the index.html file in the browser.
7. Enter the number of terms (n) and click the "Generate Fibonacci" button.
8. View the Fibonacci series displayed on the browser.
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
Code:
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Series</title>
</head>
<body>
<h2>Fibonacci Series</h2>
<form action="fibonacci" method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<button type="submit">Find</button>
</form>
</body>
</html>
FibonacciSeriesServlet1.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("/fibonacci")
public class FibonacciSeriesServlet1 extends HttpServlet {
out.println("</p>");
out.println("</body>");
out.println("</html>");
}
}
Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
10] Create a Java servlet that determines whether a person is eligible to cast a vote
based on their age.
Pseudo code:
1. Create a new Java Web Project in NetBeans with the name "rohit".
2. In the index.html file, create a <form> tag with action="program10" and
method="post".
3. Add an <input> field with the name="age" to accept the age of the person.
4. Add a submit button: <button type="submit">find</button>.
5. Create a new Servlet in the project with the name "program10".
• In the Servlet doPost() method:
• Retrieve the input age using request.getParameter("age").
• Convert the input to an integer.
Use a conditional statement to check if the age is 18 or greater:
• If age >= 18, the person is eligible to vote.
• Otherwise, the person is not eligible to vote.
6. Print the result using the out object of PrintWriter:
• Display "You are eligible to vote" if the age is 18 or greater.
• Display "You are not eligible to vote" if the age is less than 18.
7. Run the index.html file in the browser.
8. Enter the age and click the "find" button.
9. View the result ("You are eligible to vote" or "You are not eligible to vote")
displayed on the browser.
Code:
Index.html .
<!DOCTYPE html>
<html>
<head>
<title>Age Validation</title>
</head>
<body>
<h2>Vote Cast</h2>
<form action="VoteCheckerServlet" method="post">
<label for="number">Enter a Age:</label>
<input type="number" id="number" name="number" required>
<button type="submit">Find</button>
</form>
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
</body>
</html>
VoteCheckerServlet.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("/vote-checker")
public class VoteCheckerServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
// Convert the input parameter to an integer
age = Integer.parseInt(ageStr);
} catch (NumberFormatException e) {
out.println("<h2>Please provide a valid age.</h2>");
return;
}
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
out.println("<head>");
out.println("<title>Vote Eligibility Checker</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Vote Eligibility Checker</h2>");
out.println("<p>" + message + "</p>");
out.println("</body>");
out.println("</html>");
}
}
Name:Satyait Gorad
Roll No:5287
Class:SYCS-A
Output: