Create a Salary Calculator using JSP Web forms and Servlet class:-
Salary Calculator program using JSP and Servlet
Code of JSP File:-
<%@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 bgcolor="gray">
<center>
<h1>Salary Calculator</h1>
<form action="CalcSer" method="post">
<input type="text" placeholder="Enter Basic" name="txtbasic" required="" />
<br><br>
<input type="text" placeholder="Enter TA" name="txtta" required="" />
<br><br>
<input type="text" placeholder="Enter DA" name="txtda" required=""/>
<br><br>
<input type="text" placeholder="Enter COMM" name="txtcomm" required="" />
<br><br>
<input type="text" placeholder="Enter PF" name="txtpf" required="" />
<br><br>
<input type="text" placeholder="Number of leave" name="txtlv" required="" />
<br><br>
<input type="submit" name="btnsubmit" value="Calculate" />
</form>
</center>
</body>
</html>
Code of Servlet:-
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Hp
*/
public class CalcSer extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
float basic = Float.parseFloat(request.getParameter("txtbasic"));
float ta = Float.parseFloat(request.getParameter("txtta"));
float da = Float.parseFloat(request.getParameter("txtda"));
float comm = Float.parseFloat(request.getParameter("txtcomm"));
float pf = Float.parseFloat(request.getParameter("txtpf"));
float l = Float.parseFloat(request.getParameter("txtlv"));
float tsal = basic+ta+da+comm;
float nsal = tsal-(pf)-(tsal/30)*l;
out.write("Salary is "+nsal);
}
catch(Exception ex)
{
out.print(ex.getMessage());
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
<%@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 bgcolor="gray">
<center>
<h1>Salary Calculator</h1>
<form action="CalcSer" method="post">
<input type="text" placeholder="Enter Basic" name="txtbasic" required="" />
<br><br>
<input type="text" placeholder="Enter TA" name="txtta" required="" />
<br><br>
<input type="text" placeholder="Enter DA" name="txtda" required=""/>
<br><br>
<input type="text" placeholder="Enter COMM" name="txtcomm" required="" />
<br><br>
<input type="text" placeholder="Enter PF" name="txtpf" required="" />
<br><br>
<input type="text" placeholder="Number of leave" name="txtlv" required="" />
<br><br>
<input type="submit" name="btnsubmit" value="Calculate" />
</form>
</center>
</body>
</html>
Code of Servlet:-
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Hp
*/
public class CalcSer extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
float basic = Float.parseFloat(request.getParameter("txtbasic"));
float ta = Float.parseFloat(request.getParameter("txtta"));
float da = Float.parseFloat(request.getParameter("txtda"));
float comm = Float.parseFloat(request.getParameter("txtcomm"));
float pf = Float.parseFloat(request.getParameter("txtpf"));
float l = Float.parseFloat(request.getParameter("txtlv"));
float tsal = basic+ta+da+comm;
float nsal = tsal-(pf)-(tsal/30)*l;
out.write("Salary is "+nsal);
}
catch(Exception ex)
{
out.print(ex.getMessage());
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Post a Comment
POST Answer of Questions and ASK to Doubt