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
How to Implement a Simple Chat Application Using Sockets in Java?
In this article, we will create a simple chat application using Java socket programming. Before we are going to discuss our topic, we must know Socket in Java. Java Socket connects two different JREs (Java Runtime Environment). Java sockets can be connection-oriented or connection-less. In Java, we
4 min read
Simple Calculator using Java Socket Programming
Prerequisite: Socket Programming in Java First, we understand the basics of java socket programming. Java Socket is used to communicate between two different JREs. Java socket can be connection-oriented or connection-less. In java, we have a package called "java.net". In this package, we have two cl
3 min read
How to Create a simple TCP Client-Server Connection in Java?
TCP can be defined as Transmission Control Protocol. This is the standard protocol for transmitting data over a network. It provides reliable, structured, and error-checked data delivery between applications running on hosts connected via an IP (Internet Protocol) network. In Java, we can create TCP
3 min read
Split a Vector into Multiple Smaller Vectors in Java
In the realm of Java programming, the manipulation of vectors is a common task. One particularly useful operation is splitting a vector into multiple smaller vectors. This article aims to provide a detailed guide on achieving this in Java, catering to both beginners and experienced developers. Prere
3 min read
How to Set Up a Basic HTTP Server in Java?
In Java, setting up a basic HTTP server involves creating an application that listens for incoming HTTP requests and responses. In this article, we will discuss how to set up a basic HTTP server in Java. Implementation Steps to Set Up a Basic HTTP Server Step 1: Create an HttpServer instance.Step 2:
2 min read
Pizza Shop Billing System using Java Swing
Java is a fascinating programming language that provides its users with a plethora of features like OOP, platform independence, simplicity, GUI based programming, etc. One such feature is creating robust applications using the Swing toolkit provided by Java Foundation Classes. This can be used to cr
5 min read
How to Split a String in Java with Delimiter?
In Java, the split() method of the String class is used to split a string into an array of substrings based on a specified delimiter. The best example of this is CSV (Comma Separated Values) files.Program to Split a String with Delimiter in JavaHere is a simple program to split a string with a comma
2 min read
How to Implement Peer-to-Peer Communication in Java?
Peer-to-peer communication is a decentralized form of communication where two or more devices communicate directly with each other without the need for a central server. In peer-to-peer communication, each peer can act as both a client and a server, enabling them to both send and receive data. Peer-
2 min read
Establishing the two-way Communication between Server and Client in Java
It is possible to send data from the server and receive a response from the client. Similarly, the client can also send and receive data to-and-from. Below are the various steps to do so: We need additional streams both at server and client. For example, to receive data into the server, it is a bett
3 min read
How to Split an ArrayList in Multiple Small ArrayLists?
In Java, ArrayList is a pre-defined class of the Java collection framework, and it can be used to add the elements dynamically. One more special feature is that it can shrink the size dynamically to add or remove the elements from the ArrayList. In this article, we will discuss how to split an Array
2 min read