Simple Bill Splitter Application using Java Servlets


Servlets are small java modules that are used on server side of a web connection to enhance the functionality of web server. All the methods and classes for creating a servlet are available in ‘javax.servlet’ and ‘javax.servlet.http’ packages. Hence, it is important to import them into your program before working with the servlet.

In this article, we will develop a simple bill splitter application using Java Servlets. Before starting, make sure you have installed the NetBeans IDE and Apache Tomcat server.

Steps to build Simple Bill Splitter

To develop this application follow these steps −

Step 1

Open your Netbeans IDE and create a new Java Web Application through the following path: File -> New Project -> Java Web -> Java Web Application.

Step 2

Now go to index.html page and paste the below code −

index.html Code

<!DOCTYPE html>
<html>
   <head>
      <title> Tutorials Point </title>
      <meta charset = "UTF-8">
      <meta name = "viewport" content = "width=device-width, initial-scale = 1.0">
      <style>
         input {
            margin: 10px;
         }
         body
         {
            background-color: #2c74c7;
            text-align: center;
         }
      </style>
   </head>
   <body>
      <div> Welcome to Tutorials Point </div>
      <form action = "Tutotrialspoint">
         <label> Enter your total bill: </label>
         <input type = "text" name = "pay">
         <br>
         <label> Enter total person: </label>
         <input type = "text" name = "person">
         <br>
         <input type = "submit">
      </form>
   </body>
</html>

The above code will create the web UI where a user can enter the bill amount and number of persons. We have used the <form> tag which is used to accept input from keyboard. Inside <input> tag we have declared the type of input and name to uniquely identify the text field.

Step 3

Open web.xml file and paste the following code −

web.xml Code

<?xml version = "1.0" encoding = "UTF-8"?>
<web-app version = "3.1" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee 
   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
   <servlet>
      <servlet-name> Tutorialspoint </servlet-name> // Global name
      <servlet-class> Servlet1 </servlet-class> 
   </servlet>
   <servlet-mapping>
      <servlet-name> Tutorialspoint </servlet-name>
      <url-pattern> /Tutotrialspoint </url-pattern>
   </servlet-mapping>
   <session-config>
      <session-timeout>
         30
      </session-timeout>
   </session-config>
</web-app>

In above code, The <servlet-class> named ‘Servlet1’ will be executed when we run the code. <url-pattern> will call the ‘Servlet1’ so that it can get executed.

Step 4

Now locate Servlet1.java file inside the source package and then, paste the code mentioned below.

Servlet1.java Code

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class Servlet1 extends HttpServlet {
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
      // to get the user input of string type into integer type
      int tot = Integer.parseInt(request.getParameter("pay"));
      int per = Integer.parseInt(request.getParameter("person"));
      double avg = tot/per;
      System.out.println(avg);
      // to send result 
      PrintWriter out = response.getWriter();
      out.println("Per person needs to pay: " + avg);   
   }
}

In the above code, we have created a servlet class named ‘Servlet1’ that extends HttpServlet. Inside this class we defined two objects, the first is ‘request’ to accept the data from user and the second one is ‘response’ to send the result to user.

When we Run our code, the following interface will be visible on the screen. Here we need to enter the details.

Output

Conclusion

Just like the Java programs, Servlet is also platform independent means once a servlet application is created, we can use it on any operating system. In this article, we have understood the basic concept of servlets and created a servlet application that can split the bill amount according to specified inputs.

Updated on: 12-May-2023

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements