0% found this document useful (0 votes)
12 views34 pages

Practical 2 AJAVA

Uploaded by

ay9219532407
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views34 pages

Practical 2 AJAVA

Uploaded by

ay9219532407
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Name: Satyajit Gorad

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>

<label for="num2">Enter second number:</label>


<input type="number" id="num2" name="num2" required><br><br>

<input type="submit" value="Add & Sub">


Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

</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;

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));

int addResult = num1 + num2;


int subtractResult = num1 - num2;

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

out.println("<p>" + subtractResult + "</p>");


out.println("</body></html>");
}
}

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>

<label for="num2">Enter second number:</label>


<input type="number" id="num2" name="num2" required><br><br>

<input type="submit" value="Mul & Div">


</form>
</body>
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

</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;

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));

int mulResult = num1 * num2;


int divResult = num1 / num2;

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

3] Create a Java servlet-based calculator application that performs basic arithmetic


operations (addition, subtraction, multiplication, and division) on two input numbers
provided by the user. (The application should consist of an HTML form with four
buttons for each operation including error handling for division by zero).

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

<label for="num2">Number 2:</label>


<input type="number" id="num2" name="num2" required><br><br>
<input type="submit" name="operation" value="Add">
<input type="submit" name="operation" value="Subtract">
<input type="submit" name="operation" value="Multiply">
<input type="submit" name="operation" value="Divide">
</form>
</body>
</html>

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;

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));
String operation = request.getParameter("operation");

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 {

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

// Retrieve the input parameter 'number' from the request


String numberStr = request.getParameter("number");

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

// Calculate factorial of the number


int factorial = calculateFactorial(number);

// Generate HTML response


out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Factorial Calculator Result</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Factorial of " + number + " is: " + factorial + "</h2>");
out.println("</body>");
out.println("</html>");
}

// Method to calculate factorial recursively


private int calculateFactorial(int n) {
if (n == 0)
return 1;
else
return n * calculateFactorial(n - 1);
}
}
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>

<input type="number" id="number" name="number" required>


<input type="submit" value="Check">
</form>
</body>
</html>

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;

@WebServlet("/checkNumber") // Maps to the URL pattern /checkNumber


public class CheckNumberServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

response.setContentType("text/html");
PrintWriter out = response.getWriter();

// Get the number from the request


String numberStr = request.getParameter("number");
double number = Double.parseDouble(numberStr);

// Determine if the number is positive, negative, or zero


String result;
if (number > 0) {
result = "The number is positive.";
} else if (number < 0) {
result = "The number is negative.";
} else {
result = "The number is zero.";
}

// Generate HTML response


out.println("<html><body>");
out.println("<h1>Result</h1>");
out.println("<p>" + result + "</p>");
out.println("<a href='index.html'>Go back</a>");
out.println("</body></html>");

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();

// Retrieve the input parameter 'number' from the request


String numberStr = request.getParameter("number");
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;
}

// Check if the number is prime


boolean isPrime = isPrimeNumber(number);

// Generate HTML response


Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

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>");

// Method to check if a number is prime


private boolean isPrimeNumber(int n) {
if (n <= 1)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
}

Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

7] Create a Java servlet application that checks whether a given number is an


Armstrong number, based on user input from an HTML form.
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=" Armstrong", 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">Check </button>
4. Create a new Servlet in the project with the name " Armstrong".
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 loop to calculate the sum of the cubes of each digit in the number.
• Compare the sum with the original number to check if it's an Armstrong
number.
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" button.
9. View the result ("Armstrong number" or "Not an Armstrong number")
displayed on the browser

Code:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

<meta name="viewport" content="width=device-width, initial-scale=1.0">


<title>Armstrong Number Checker</title>
</head>
<body>
<h1>Armstrong Number Checker</h1>
<form action="ArmstrongServlet" method="post">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<input type="submit" value="Check">
</form>
</body>
</html>

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");

PrintWriter out = response.getWriter();

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

// Calculate the sum of the powers of its digits


Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

while (number > 0) {


int digit = number % 10;
sum += Math.pow(digit, n);
number /= 10;
}
// Check if the number is an Armstrong number
out.println("<h1>Result</h1>");
if (sum == originalNumber) {
out.println("<p>" + originalNumber + " is an Armstrong number.</p>");
} else {
out.println("<p>" + originalNumber + " is not an Armstrong number.</p>");
}
} catch (NumberFormatException e) {
out.println("<p>Please enter a valid integer.</p>");
} finally {
out.close();
}
}
}
Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

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>

<form action="EvenOddCheckerServlet" 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>
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

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();

// Retrieve the input parameter 'number' from the request


String numberStr = request.getParameter("number");
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;
}

// Check if the number is even or odd


String result = (number % 2 == 0) ? "even" : "odd";

// Generate HTML response


out.println("<!DOCTYPE html>");
out.println("<html>");
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

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 {

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

// Retrieve the input parameter 'n' from the request


String nStr = request.getParameter("n");
int n;
try {
// Convert the input parameter to an integer
n = Integer.parseInt(nStr);
} catch (NumberFormatException e) {
out.println("<h2>Please provide a valid number.</h2>");
return;
}

// Generate Fibonacci series


out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Fibonacci Series Generator</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Fibonacci Series for " + n + " terms:</h2>");
out.println("<p>");
int firstTerm = 0, secondTerm = 1;
out.print(firstTerm);
if (n > 1) {
out.print(", " + secondTerm);
for (int i = 2; i < n; i++) {
int nextTerm = firstTerm + secondTerm;
out.print(", " + nextTerm);
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

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();

// Retrieve the input parameter 'age' from the request


String ageStr = request.getParameter("number");
int age;

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

// Check if the age is eligible to vote


String message;
if (age >= 18) {
message = "You are eligible to cast your vote.";
} else {
message = "You are not eligible to vote yet. Please wait until you turn 18.";
}

// Generate HTML response


out.println("<!DOCTYPE html>");
out.println("<html>");

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:

You might also like