Table of Contents
Steps to build a simple bill splitter
step 1
Step 4
index.htmlcode
web.xml code
Servlet1.java code
Output
in conclusion
Home Java javaTutorial Simple bill splitting application written using Java Servlets

Simple bill splitting application written using Java Servlets

Sep 09, 2023 am 11:57 AM

Servlet is a small Java module used on the server side of a Web connection to enhance the functionality of the Web server. All methods and classes used to create servlets can be found in the "javax.servlet" and "javax.servlet.http" packages. Therefore, it is important to import servlets into your program before using them.

In this article, we will develop a simple bill splitting application using Java Servlet. Before you begin, make sure you have NetBeans IDE and Apache Tomcat server installed.

Steps to build a simple bill splitter

To develop this application, please follow the steps below -

step 1

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

使用Java Servlets编写的简单账单分割应用程序

Step 2

Now go to the index.html page and paste the following code -

index.htmlcode

<!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>
Copy after login

The above code will create a web UI where the user can enter the bill amount and number of people. We used the

tag to accept input from the keyboard. Within the tag, we declare the input type and name to uniquely identify the text field.

Step 3

Open the web.xml file and paste the following code -

使用Java Servlets编写的简单账单分割应用程序

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>
Copy after login

In the above code, when we run the code, the named "Servlet1" will be executed. Will call "Servlet1" so that it can be executed.

Step 4

Now find the Servlet1.java file in the source package and paste the code mentioned below.

使用Java Servlets编写的简单账单分割应用程序

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);   
   }
}
Copy after login

In the above code, we created a servlet class named "Servlet1" which extends HttpServlet. In this class, we define two objects, the first is "request", which is used to accept the user's data, and the second is "response", which is used to send the results to the user.

When we run the code, the following interface will be displayed on the screen. Here we need to enter the details.

Output

使用Java Servlets编写的简单账单分割应用程序

使用Java Servlets编写的简单账单分割应用程序

in conclusion

Just like Java programs, Servlets are also platform independent, which means that once a servlet application is created, we can use it on any operating system. In this article, we learned about the basic concepts of Servlets and created a Servlet application that can split the bill amount based on specified inputs.

The above is the detailed content of Simple bill splitting application written using Java Servlets. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles