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 your bill(Integer value) : GUI_User_Input
Enter no. of people(Integer value) : GUI_User_Input
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.
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.
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>
Running the Application:
Start the server and RUN!
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.