Home > Java > javaTutorial > Simple bill splitting application written using Java Servlets

Simple bill splitting application written using Java Servlets

WBOY
Release: 2023-09-09 11:57:06
forward
1187 people have browsed it

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!

source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template