Servlet

Inventory Management Servlet Jsp with Ajax

This tutorial will teach you how to make a Inventory Management System in Servlet Jsp with Ajax step by step. This system will helpful you to learn Inventory Management System.this system helpful for you learn sales handling process of Servlet Jsp mvc with ajax.

In order to create the project download the following Jar Files

gson-2.2.2.jar
json-simple-1.1.jar
mysql-connector-java.jar

Sales.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Inventory Management System in Jsp Servlet Ajax</title>
       
        <link rel="stylesheet" href="//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
        
        
</head>
<body>
   
</br>
<div class="container-fluid bg-2 text-center">
<div class="row">
    <div class="col-sm-8">
        <form class="form-horizontal" id="frmInvoice">

            <table class="table table-bordered">
                <caption> Add Products  </caption>
                <tr>
                    <th>Product Code</th>
                    <th>Product Name</th>
                    <th>Price</th>
                    <th>Qty</th>
                   
                    <th>Amount</th>
                    <th>Option</th>
                </tr>
                <tr>
                    
                
                    
                    
                    <td>
                        
                       
                        
                        <input type="text" class="form-control" placeholder="barcode" id="barcode" name="barcode"  size="25px"  required>
                    </td>
                    <td>
                      
                        <label id="pname" name="pname"></label>
                        
                    </td>
                    <td>
                          <label id="pro_price" name="pro_price"></label>
                        
                    </td>
                   <td>
                    <input type="number" class="form-control pro_price" id="qty" name="qty"
                           placeholder="qty" min="1" value="1"  required>
                </td>
                <td>
                    <input type="text" class="form-control" placeholder="total_cost" id="total_cost" name="total_cost" disabled>
                </td>
                    <td>
                           <div class="focusguard" id="focusguard-2" tabindex="8"></div>
                            
                        <button class="btn btn-success" type="button" >

make inside the product folder

get_id.jsp

<%@page import="java.sql.*" %>
<%@page import="org.json.simple.JSONArray"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="org.json.simple.parser.JSONParser"%>
<%@page import="org.json.simple.parser.ParseException"%>

<%
     JSONArray list = new JSONArray();
     Connection con;
            PreparedStatement pst;
            ResultSet rs;
            Class.forName("com.mysql.jdbc.Driver");
            String a1 = request.getParameter("a");
           con = DriverManager.getConnection("jdbc:mysql://localhost/jsppos","root","");
            pst = con.prepareStatement("select * from product where barcode = ?");
            pst.setString(1, a1);
            rs = pst.executeQuery();

            if(rs.next() == true)
            {
                 String prodname   = rs.getString("prodname");
                 String price     = rs.getString("retailprice");
                 JSONObject obj = new JSONObject();
                 obj.put("prodname", prodname);
                 obj.put("retailprice", price); 
                 list.add(obj);
            }
            
           out.print(list.toJSONString());
           out.flush(); 

%>

salesadd.java

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

@WebServlet("/salesadd")
public class salesadd extends HttpServlet 
{
       @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        
           response.setContentType("text/html;charset=UTF-8");
           PrintWriter out = response.getWriter();
           Connection con;
           PreparedStatement pst; 
           PreparedStatement pst1; 
           PreparedStatement pst2; 
           
           
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime now = LocalDateTime.now();
            String date = dtf.format(now);
            

           
           
            String jsonData = request.getParameter("data1");
            String uname = request.getParameter("uname");
            
            
            String subtotal = request.getParameter("subtotal");
            String pay = request.getParameter("pay");
            String balance = request.getParameter("balance");
            int lastinsetid  = 0;
           
           try
           {
           JSONArray list = new JSONArray();
           JSONObject obj = new JSONObject();
           Class.forName("com.mysql.jdbc.Driver");
           con = DriverManager.getConnection("jdbc:mysql://localhost/jsppos", "root", ""); 
           String query = "insert into sales(casier,date,subtotal,pay,bal)values(?,?,?,?,?)";          
           pst1 = con.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);        
           pst1.setString(1, uname);
           pst1.setString(2, date);
           pst1.setString(3, subtotal);
           pst1.setString(4, pay);
           pst1.setString(5, balance);
           pst1.executeUpdate();
           ResultSet genteratedKeyResult = pst1.getGeneratedKeys();
           
           if(genteratedKeyResult.next())
            {
                lastinsetid =genteratedKeyResult.getInt(1);
            }
           
           String newval = String.valueOf(lastinsetid);
           obj.put("newval",newval);
           
           list.add(obj);
           out.print(list.toJSONString());
           out.flush(); 
           Gson gson = new Gson();
           List<Product> objectList = getObjectList(jsonData, Product.class);
           
           
         for (Product product : objectList) 
         {

           try
           { 
             
            Class.forName("com.mysql.jdbc.Driver");
            int barcode = product.get_barcode();
            int qty     =  product.getQty();
            pst2 = con.prepareStatement("update product set qty =qty-? where id =?  ");
            pst2.setInt(1, qty);
            pst2.setInt(2, barcode);
            pst2.executeUpdate();    

           pst = con.prepareStatement("insert into sales_product(sales_id,prod_id,price,qty,total)values(?,?,?,?,?) ");
           pst.setInt(1, lastinsetid);
           pst.setInt(2, product.get_barcode());
           pst.setInt(3, product.getPrice());
           pst.setInt(4, product.getQty());
           pst.setInt(5, product.getTotal());
           pst.executeUpdate(); 
           
           } 
           
           catch (SQLException ex) 
           {
           ex.printStackTrace(); 
           }      
           catch (ClassNotFoundException ex) 
           {
                ex.printStackTrace(); 
           }
        
      }
           } 
           catch (SQLException ex)
           {
            ex.printStackTrace(); 
           } catch (ClassNotFoundException ex) {
               ex.printStackTrace(); 
           }
    }
    public static <T> List<T> getObjectList(String jsonString,Class<T> cls){
    List<T> list = new ArrayList<>();
    try {
        Gson gson = new Gson();
        JsonArray arry = new JsonParser().parse(jsonString).getAsJsonArray();
        for (JsonElement jsonElement : arry) 
        {
            list.add(gson.fromJson(jsonElement, cls));
        }
    } catch (Exception e) 
    {
        e.printStackTrace();
    }
    return list;
}
   
}
    
    
    
    

Print.jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="org.json.simple.JSONArray"%>
<%@page import="java.sql.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>


<%
Connection con;
PreparedStatement pst;
ResultSet rs;

String lastid = request.getParameter("lastinsetid");
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/jsppos","root","");
pst = con.prepareStatement("select sales.id,sales.subtotal,sales.pay,sales.bal, salesp.price,salesp.qty,salesp.total, prod.barcode,prod.prodname from sales_product salesp, sales sales, product prod where sales.id = salesp.sales_id and salesp.prod_id = prod.barcode and salesp.sales_id =?");
pst.setString(1, lastid);
rs = pst.executeQuery();


 if(rs.next()== true)                                   
{
    String id = rs.getString(1);
    String subtotal = rs.getString(2);
    String pay = rs.getString(3);
    String bal = rs.getString(4);
    String price = rs.getString(5);
    String qty = rs.getString(6);
    String total = rs.getString(7);
    String pid = rs.getString(8);
    String pname = rs.getString(9);
    
 %>

    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">


        <style>
            @media print
            {
                .button
                {
                    display: none;
                }
            }

            @media print
            {
                @page {
                    margin-top: 0;
                    margin-bottom: 0;
                }
                body  {
                    padding-top: 72px;
                    padding-bottom: 72px ;
                }
            }

        </style>

        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


    </head>
    <body style='background: #f9f9f9'>


    <div class="container">
        <div class="row">
            <div class="col-xs-12">
                <div class='print' style="border: 0px solid #a1a1a1; font-family:monospace; font-size: 14px; width: 88mm; background: white; padding: 10px; margin: 0 auto; text-align: center; ">
                    <div class="invoice-title" align="center">
                        <h3>               
                            <img src="image/tit1.jpg" width="200px" height="100px" alt=""/></h3>
                    </div>

                    <div class="invoice-title" align="left">
                        Order #  &nbsp; &nbsp;<b> <%=id %></b>
                    </div>
                    <div class="invoice-title" align="left">
                        Date #  &nbsp; &nbsp;  <b>  
                        <%java.text.DateFormat df = new java.text.SimpleDateFormat("dd/MM/yyyy"); %>
                       <%= df.format(new java.util.Date()) %> 
                        </b>
                    </div>

                    
                   
                    </br>

                    <div>
                        <div>
                            <table class="table table-condensed">
                            <thead>
                                <tr>
                                    <td class="text-center"><strong>No</strong></td>
                                    <td class="text-center"><strong>Pname</strong></td>
                                    <td class="text-center"><strong>Qty</strong></td>
                                    <td class="text-center"><strong>Price</strong></td>
                                    <td class="text-right"><strong>Total</strong></td>
                                </tr>
                               
                                
                                
                                
                                <%       
                                    Class.forName("com.mysql.jdbc.Driver");
                                    con = DriverManager.getConnection("jdbc:mysql://localhost/jsppos","root","");

                                    pst = con.prepareStatement("select sales.id,sales.subtotal,sales.pay,sales.bal, salesp.price,salesp.qty,salesp.total, prod.barcode,prod.prodname from sales_product salesp, sales sales, product prod where sales.id = salesp.sales_id and salesp.prod_id = prod.barcode and salesp.sales_id =?");
                                    pst.setString(1, lastid);
                                    rs = pst.executeQuery();
                                     int x = 1;
                                     while(rs.next())
                                     {
                                 %>
                
                                    <tr>
                                        <td class="text-center">
                                             <%= x %>
                                        </td >
                                        
                                        <td class="text-center"> 
                                           <%=rs.getString("prod.prodname") %>
                                        </td >
                                        <td class="text-center">
                                             <%=rs.getString("salesp.qty") %>
                                            
                                        </td >
                                        
                                        <td class="text-center">
                                           <%=rs.getString("salesp.price") %>
                                        </td>
                                     
                                        <td class="text-center">
                                            <%=rs.getString("salesp.total") %>
                                        </td>
                                    </tr>

                                <% x= x + 1; %>      
                                <% } %>      
                                    
                            </table>

                        </div>
                    </div>
                    <div  align="right">
                        Sub Total &nbsp;&nbsp;<b> <%=subtotal  %></b>
                    </div>
                    <div align="right">
                        Pay  &nbsp;&nbsp; <b> <%=pay  %></b>
                    </div>
                    <div align="right">
                        Due &nbsp;&nbsp;   <b> <%=bal  %></b>
                    </div>

                    <div align="center">
                        <i>60 b bank road India</i>
                    </div>
                </div>
           </div>
            <div>
                <div>
                </div>

                <center>

   
     <script src="component/jquery/jquery.js" type="text/javascript"></script>
        <script src="component/jquery/jquery.min.js" type="text/javascript"></script>
        <script src="component/jquery.validate.min.js" type="text/javascript"></script>

    <script>

       myFunction();
        window.onafterprint = function(e)
        {
         closePrintView();
        };
        function myFunction()
        {
           window.print();
        }
        function closePrintView()
        {
           window.location.href = 'sales.jsp';
        }

    </script>
    
 </body>
    </html>


<% } %>

Download the full Source code

Download

i have attached the video link below. which will do this tutorials step by step

 

admin

Recent Posts

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

4 days ago

Java Payroll System Calculate Employee Overtime

Act as a Java developer to create a program that calculates the gross wage for…

6 days ago

Employee Working Hours Calculation System using Java

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

6 days ago

Laravel 11 School Management System

In this tutorial, we will teach you how to create a simple school management system…

2 weeks ago

How to Make Admin Panel Using React MUI

I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…

3 weeks ago

Install Laravel Breeze for Authentication Using Laravel 11

In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…

4 weeks ago