Simple Bill Splitter Application using Java Servlets
Last Updated :
02 Dec, 2020
Pre-requisite: Java Servlets
Servlets is a Java Technology for server-side programming generally used to create web applications. It is a module that runs inside a Java-enabled web server.
Here, you will see the implementation by developing a Bill Splitter Application. In this Java Servlets app, there are two input columns one for the total bill amount and the second column is for the number of people in which the bill will be split. Let's have a look.
Input :
Input your bill(Integer value) : GUI_User_Input
Enter no. of people(Integer value) : GUI_User_Input
Output :
Result will display once user will give input and will click on submit button (Double data type value)
=
Input your bill /Enter no. of people

Here, we will build a simple web application that will split the bill amount among the specified number of people.
Understanding how a Servlet works :
- The client sends a request to the server and the request goes to the Web Container.
- The Web Container uses a file named web.xml to detect which servlet to call and it calls the required servlet.
- The servlet will process the information and will send a response to the client machine.
Setting up the Eclipse IDE:
- Download the Eclipse IDE for Java EE Development from http://www.eclipse.org/downloads/
- Download the Tomcat server from https://tomcat.apache.org
- Go to File>New>Dynamic Web Project.
- Add the link to the folder containing Tomcat to the Servlets tab at the bottom of the window and you’re all set to create your first servlet!
Creating the HTML file for Web layout :
Create a simple HTML file that includes a form to input details about the bill amount and the number of people.
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Bill Splitter</title>
<!-- CSS properties. -->
<style type="text/css">
body
{
background-color:skyblue;
text-align:center;
}
</style>
</head>
<body>
<h1>Bill Splitter</h1>
<!-- Application GUI form HTML code -->
<form action="splitter">
Input your bill: <input type="text" name="bill">
<br>
Enter no. of people: <input type="text" name="people">
<br>
<input type="submit">
</form>
</body>
</html>
Creating the Java file:
Create a new class and extend the HttpServlet class to make your java class a servlet. In the case of Servlet file, response, and request object includes in servlet class. The req object is used to fetch data from the client while the res object is used to send a response to the client. These two functions parseInt and getParameter will help to take input from HTML form and will take integer value from that and then the calculation will be performed and you will see PrintWriter object to print the output value.
parseInt is used to convert string to integer.
getParameter is used to get the user input in string form.
PrintWriter is used to fetch output to the output window and not the console.
Java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class split extends HttpServlet{
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
{
int i = Integer.parseInt(req.getParameter("bill"));
int j = Integer.parseInt(req.getParameter("people"));
double r = i/j;
System.out.println(r);
PrintWriter out=res.getWriter();
out.println("Bill per person is Rs" +r);
}
}
Configuring the web.xml file:
Whenever you create a new dynamic web project, you get a file named web.xml in your Project Folder which is the most important file for mapping together the servlet and the HTML file. The web.xml file uses two tags <servlet> & <servlet-mapping> to map the Servlet and HTML file together.
<servlet-name> is used to assign a universal name to the servlet.
<servlet-class> is used to assign the java class to be executed when servlet is called.
<url-pattern> is used to define the url pattern which when found will call the java class
defined in <servlet-class>
XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>servlet</display-name>
<servlet>
<servlet-name>serv1</servlet-name>
<servlet-class>servlet.split</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>serv1</servlet-name>
<url-pattern>/splitter</url-pattern>
</servlet-mapping>
</web-app>
Running the Application:
Start the server and RUN!
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read