0% found this document useful (0 votes)
9 views16 pages

Java Servlet Programs for Students and Employees

The document outlines multiple Java servlet exercises, including programs for calculating student grades, temperature conversion, employee salary, cookie implementation, and session management. Each exercise includes an HTML form for user input and corresponding Java servlet code to process the data and display results. The document serves as a practical guide for implementing basic web applications using Java servlets.
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)
9 views16 pages

Java Servlet Programs for Students and Employees

The document outlines multiple Java servlet exercises, including programs for calculating student grades, temperature conversion, employee salary, cookie implementation, and session management. Each exercise includes an HTML form for user input and corresponding Java servlet code to process the data and display results. The document serves as a practical guide for implementing basic web applications using Java servlets.
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

REGISTER NUMBER:23301840

/*******************************************************************************
Ex No:01 Date: 02-12-2025

AIM: Write a program to calculate total and average of a student using Java servlet. Use
HTML page to take user inputs. Minimum 35 is required to pass. Result is calculated as
follows:
● If average>80; result=Distinction
● If average>60; result=First Class
● If average>50; result=Second Class
● Else; result=Pass
*******************************************************************************/
[Link]

<html>
<head>
<title>Student Information</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" name="student">
Name:<input type="text" name="nm" required ><br><br>
Reg no.:<input type="number" name="reg" required ><br><br>
course:<input type="text" name="cor" required ><br><br>
subject 1:<input type="text" name="s1" required ><br><br>
subject 2:<input type="text" name="s2" required ><br><br>
subject 3:<input type="text" name="s3" required ><br><br>
<input type="submit" value="Result">
</form>
</body>
</html>

Servelet – [Link]

@WebServlet(urlPatterns = {"/student"})
public class student extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
/* TODO output your page here. You may use following sample code. */
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet servelet1</title>");
[Link]("</head>");
REGISTER NUMBER:23301840

[Link]("<body>");
String regno=[Link]("txtregno");
String name=[Link]("txtname");
int m1=[Link]([Link]("txtm1"));
int m2=[Link]([Link]("txtm2"));
int m3=[Link]([Link]("txtm3"));
int total=m1+m2+m3;
float avg=(float)total/3;
String result;
if(m1<35||m2<35||m3<35)
result="Fail";
else if(avg>80)
result="Distinction";
else if(avg>50)
result="Second Class";
else
result="Pass";
[Link]("<table border=2>");
[Link]("<tr><th colspan=2>Students Marks</th></tr>");
[Link]("<tr><td>Register Number</td><td>"+regno+"</td></tr>");
[Link]("<tr><td>Name</td><td>"+name+"</td></tr>");
[Link]("<tr><td>Total</td><td>"+regno+"</td></tr>");
[Link]("<tr><td>Average</td><td>"+avg+"</td></tr>");
[Link]("<tr><td>Result</td><td>"+result+"</td></tr>");
[Link]("</table>");
[Link]("</body>");
[Link]("</html>");
}
}
}

OUTPUT:
REGISTER NUMBER:23301840

/*******************************************************************************
Ex No: 02 Date: 04-12-2025

AIM: Write a program to convert the temperature from Celsius to Fahrenheit and vice versa
using Java servlet. Use HTML page to take user input
*******************************************************************************/
[Link]

<html>
<head>
<title>Temperature Conversion</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Temperature Conversion</h1>
<form action="temp" method="POST">
Enter Temperature:<input type="text" name="txttemp" value=""/>
<br><br>
Convert To:
<br><br>
<input type="radio" name="operation" value="1" checked/>
Celsius(from Fahrenheit)
<br><br>
<input type="radio" name="operation" value="2"/>
Fahrenheit(from Celsius)
<br><br>
<input type="submit" value="Convert" />
</form>
</body>
</html>

[Link]

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
/* TODO output your page here. You may use following sample code. */
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet temp</title>");
[Link]("</head>");
[Link]("<body>");
float t=[Link]([Link]("txttemp"));
REGISTER NUMBER:23301840

int op=[Link]([Link]("operation"));
[Link]("<h3><u>Temperature Conversion</u></h3>");
[Link]("The given temperature is:"+t);
switch(op)
{
case 1:float c=(float)((t-32.0)/1.8);
[Link]("<br>The temperature in celsius is:"+c);
break;
case 2:float f=(float)((t*1.8)+32.0);
[Link]("<br>The temperature in fahrenheit is:"+f);
break;

}
[Link]("</body>");
[Link]("</html>");
}
}

OUTPUT:
REGISTER NUMBER:23301840

/*******************************************************************************
Ex No: 03 Date: 09-12-2025

AIM: Write a program to calculate the salary of an employee using Java servlet. Calculate
DA, HRA, PF, Gross Pay, Tax and Net Pay by taking Basic Pay as user input from the HTML
page. Display the pay slip in tabular form.
Additions are calculated as follows:
● If Basic Pay<8000 then DA=20% of Basic Pay
● If Basic Pay<12,000 then DA=30% of Basic Pay
● Else DA=35% of Basic Pay
● If Basic Pay<10,000 then HRA=12.5% of Basic Pay
● If Basic Pay<15,000 then HRA=18% of Basic Pay
● Else HRA=20% of Basic Pay
Deductions are calculated as follows:
● PF=13.5% of Basic Pay, subject to minimum of 1500
● If Gross Pay >=35,000 then Tax =20% of Gross Pay
● If Gross Pay >=25,000 then Tax =18.5% of Gross Pay
● If Gross Pay >=20,000 then Tax =15% of Gross Pay
● Else Tax=0
*******************************************************************************/
[Link]

<!DOCTYPE html>
<html>
<head>
<title>Employee Details</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Emp" method="post">
Employee ID:<input type="text" name="id"><br><br>
Employee name:<input type="text" name="name"><br><br>
Basic Pay:<input type="text" name="bp"><br><br>
<input type="submit" value="submit">
</form>
</body>
</html>

[Link]

[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Emp</title>");
[Link]("</head>");
REGISTER NUMBER:23301840

[Link]("<body>");
[Link]("<h1>Servlet Emp at"+[Link]()+"</h1>");
String eid=[Link]("id");
String ename=[Link]("name");
float bp=[Link]([Link]("bp"));
float da,hra,pf,gp,tax,np;
if(bp<8000)
da=(float)0.2*bp;
else
da=(float)0.35*bp;
if(bp<10000)
hra=(float)0.125*bp;
else if(bp<15000)
hra=(float)0.18*bp;
else
hra =(float)0.2*bp;
pf=(float)0.135*bp;
if(pf<1500)
pf=1500;
gp=(bp+da+hra);
if(gp>=35000)
tax=(float)0.20*gp;
else if(gp>30000)
tax=(float)0.185*gp;
else if(gp>=20000)
tax=(float)0.15*gp;
else
tax=0;
np=(gp-pf-tax);
[Link]("<h2>Pay Slip</h2>");
[Link]("<table border='1'>");
[Link]("<tr><th>Employee ID</th><td>"+eid+"</td></tr>");
[Link]("<tr><th>Employee Name</th><td>"+ename+"</td></tr>");
[Link]("<tr><th>Basic Pay</th><td>"+bp+"</td></tr>");
[Link]("<tr><th>Dearness Allowance(DA)</th><td>"+da+"</td></tr>");
[Link]("<tr><th>House Rent Allowance</th><td>"+hra+"</td></tr>");
[Link]("<tr><th>Provident Fund</th><td>"+pf+"</td></tr>");
[Link]("<tr><th>Gross Pay</th><td>"+gp+"</td></tr>");
[Link]("<tr><th>Tax</th><td>"+tax+"</td></tr>");
[Link]("<tr><th>Net Pay</th><td>"+np+"</td></tr>");
[Link]("</table>");
[Link]("</body>");
[Link]("</html>");
REGISTER NUMBER:23301840

OUTPUT:
REGISTER NUMBER:23301840

/*******************************************************************************
Ex No: 04 Date:09-12-2025

AIM:Write a program to implement cookies using java servlets. Accept user input from html
page. Use cookie methods to store and display the data. Set the expiry of cookies to 30 days.
******************************************************************************/
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Cookies </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="first" method="post">
<h3><u>COOKIES PROGRAM</u></h3>
Enter your name:<input type="text" name="name"><br><br>
Enter your email:<input type="text" name="email"><br><br>
Password:<input type="password" name="pwd"><br><br>
<input type="submit" value="Add Cookie">
</form>
</body>
</html>

[Link]

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet(urlPatterns = {"/first"})
public class first extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
/* TODO output your page here. You may use following sample code. */
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet first</title>");
REGISTER NUMBER:23301840

[Link]("</head>");
[Link]("<body>");
String name=[Link]("name");
String email=[Link]("email");
String pwd=[Link]("pwd");
Cookie n = new Cookie("Name",name);
[Link](60*60*24*30);
[Link](n);
Cookie e=new Cookie("Email",email);
[Link](60*60*24*30);
[Link](e);
Cookie p=new Cookie("Password",pwd);
[Link](60*60*24*30);
[Link](p);
[Link]("<h1>Cookie has been successfully added successfully !!</h1>");
[Link]("<form action='second' method='post'>");
[Link]("<input type='submit' value='continue'>");
[Link]("</body>");
[Link]("</html>");
}
}
}

[Link]

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
/* TODO output your page here. You may use following sample code. */
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet second</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h1>Values Retrieved from Cookies</h1>");
Cookie[] mycookie=[Link]();
if(mycookie==null)
[Link]("No Cookies");
else
{
for(int i=0;i<[Link];i++)
{
[Link](mycookie[i].getName()+":"+mycookie[i].getValue());
[Link]("<br><br>");
REGISTER NUMBER:23301840

}
}
[Link]("</body>");
[Link]("</html>");
}
}
}

OUTPUT:
REGISTER NUMBER:23301840

/*******************************************************************************
Ex No:05 Date:16-12-2025

AIM: Write a program to implement sessions using java servlets. Accept user input from html
page. Use session methods to store and display the data
*******************************************************************************/
[Link]

<!DOCTYPE html>
<html>
<head>
<title>Student Registration</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Student Registration</h1>
<form action="processRequest" method="post">
<label>Registration No:
<input type="text" name="regNo" required/>
</label>
<br><br>
<label>Name:
<input type="text" name="name" required>
</label>
<br><br>
<label>Class:
<input type="text" name="class">
</label>
<br><br>
<label>Date of Birth:
<input type="date" name="dob">
</label>
<br><br>
<label>Gender:
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female
<input type="radio" name="gender" value="other">other
</label>
<br><br>
<label>Blood Group:
<select name="bloodGroup">
<option value="">--Select--</option>
<option value="A+">A+</option>
<option value="A-">A-</option>
<option value="B+">B+</option>
<option value="B-">B-</option>
<option value="O+">O+</option>
<option value="O-">O-</option>
<option value="AB+">AB</option>
REGISTER NUMBER:23301840

<option value="AB-">AB-</option>
</select>
</label><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>

[Link]

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
@WebServlet(urlPatterns = {"/processRequest"})
public class processRequest extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
/* TODO output your page here. You may use following sample code. */
String regNo=[Link]("regNo");
String name=[Link]("name");
String studentClass=[Link]("class");
String email=[Link]("email");
String dob=[Link]("dob");
String gender=[Link]("gender");
String bloodGroup=[Link]("bloodGroup");

HttpSession session=[Link]();
[Link]("regNo",regNo);
[Link]("name", name);
[Link]("studentClass",studentClass);
[Link]("email",email);
[Link]("dob",dob);
[Link]("gender",gender);
[Link]("bloodGroup",bloodGroup);
[Link]("displaySession");
}
}
}

[Link]
REGISTER NUMBER:23301840

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
@WebServlet(urlPatterns = {"/displaySession"})
public class displaySession extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
/* TODO output your page here. You may use following sample code. */

HttpSession session=[Link]();
String regNo=(String)[Link]("regNo");
String name=(String)[Link]("name");
String studentClass=(String)[Link]("studentClass");
String email=(String)[Link]("email");
String dob=(String)[Link]("dob");
String gender=(String)[Link]("gender");
String bloodGroup=(String)[Link]("bloodGroup");

[Link]("<html>");
[Link]("<body>");
[Link]("<h2>User Information</h2>");
[Link]("<table border='1'>");
[Link]("<tr><th>Field</th><th>Value</th></tr>");
[Link]("<tr><td>Registration Number</td><td>"+regNo+"</td></tr>");
[Link]("<tr><td>Name</td><td>"+name+"</td></tr>");
[Link]("<tr><td>Class</td><td>"+studentClass+"</td></tr>");
[Link]("<tr><td>Email ID</td><td>"+email+"</td></tr>");
[Link]("<tr><td>Date of Birth</td><td>"+dob+"</td></tr>");
[Link]("<tr><td>Gender</td><td>"+gender+"</td></tr>");
[Link]("<tr><td>Blood Group</td><td>"+bloodGroup+"</td></tr>");
[Link]("</table>");
[Link]("</body>");
[Link]("</html>");
}
}
}
REGISTER NUMBER:23301840

OUTPUT:
REGISTER NUMBER:23301840

/*******************************************************************************
Ex No:06 Date:19-12-2025

AIM: Write a program read all the form parameters in a JSP page using enumeration. Use
html form to input book details.
*******************************************************************************/
[Link]

<html>
<head>
<title>Book Details</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>Book Details</h2>
<form action="[Link]" method="post">
Book ID:<input type="text" name="bid"><br><br>
Title:<input type="text" name="bname"><br><br>
Author:<input type="text" name="author"><br><br>
Price:<input type="text" name="price"><br><br>
Publisher:<input type="text" name="pub"><br><br>
Genre:
<input type="radio" name="Comedy">Comedy
<input type="radio" name="Thriller">Thriller
<input type="radio" name="author">Horror
<br><br>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>

[Link]

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1><%="BOOK DETAILS"%></h1><%--EXPRESSION TAG--%>
<br><br>
<%
String bid=[Link]("bid");
REGISTER NUMBER:23301840

String bname=[Link]("bname");
String author=[Link]("author");
String genre=[Link]("genre");
String publisher=[Link]("pub");
String price=[Link]("price");
%>
<%--SCRIPLET TAG--%>
<table border="1">
<tr><td>BOOK ID</td><td><%=bid%></td></tr>
<tr><td>BOOK NAME</td><td><%=bname%></td></tr>
<tr><td>AUTHOR</td><td><%=author%></td></tr>
<tr><td>GENRE</td><td><%=genre%></td></tr>
<tr><td>PUBLISHERS</td><td><%=publisher%></td></tr>
<tr><td>PRICE</td><td><%=price%></td></tr>
</table>
</body>
</html>

OUTPUT:

You might also like