0

In my project ,I need to pass the "price" value which is retrieved from the database in "Availability.java" to "display.jsp".I have achieved it, but the problem is.... I also need to pass the "price" value from "display.jsp" to "seat.jsp". When I tried, it returns null value for the price variable.

Availability.java

    try {
         response.setContentType("text/html;charset=UTF-8");
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bus","root","happy"); 
        Statement stmt=conn.createStatement();
        ResultSet rs=stmt.executeQuery("select * from availability");  
        String fp=request.getParameter("from");
        String tp=request.getParameter("to");
        String date = request.getParameter("date");
        Boolean flag=false;
            while(rs.next()){
                                             
                String from=rs.getString("fromplace").toString();
                String to=rs.getString("toplace").toString(); 
                String datedb=rs.getString("date").toString();
                String price=rs.getString("price").toString();
               

                if(from.equals(fp) && to.equals(tp) && datedb.equals(date) )         
                {
                    //PrintWriter out=response.getWriter();
                    //out.print("Available"); 
                    request.setAttribute("from",from);
                    request.setAttribute("to",to);
                    request.setAttribute("date",date);
                    request.setAttribute("price",price);  
                    request.getRequestDispatcher("display.jsp").forward(request, response); 
                    flag=true;
                    break;
                    
                }
                
               
            }
            if(!flag) {
                request.setAttribute("date",date);
                request.getRequestDispatcher("Unavailable.jsp").forward(request, response); 
            }
         

display.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
   <table >
   <tr class="icon">
  
  <th>Fare</th>
  
</tr>
<tr>
  <td id="p">INR ${price }</td>
 
</tr>

</table>
 <center><a href="seat.jsp" class="button"> <span>View Seats </span></a></center>

seat.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<body>
    <div class="wrapper">
        <div class="container">
            <div id="seat-map" class="seat-panel">
                <div class="front-indicator">Front</div>
                
            </div>
            <div class="booking-details">
                <h2>Booking Details</h2>
                
                <h3> Selected Seats (<span id="counter">0</span>):</h3>
                <ul id="selected-seats"> </ul>
                Total: <b>Rs.<span id="total">0</span></b>
                
                <button class="checkout-button">Checkout &raquo;</button>
                
                <div id="legend"></div>
            </div>
        </div>
    </div>
    
    
    <script>
        var firstSeatLabel = 1;
        $(document).ready(function() {
            var $cart = $('#selected-seats'),
                $counter = $('#counter'),
                $total = $('#total'),
                sc = $('#seat-map').seatCharts({
                map: [
                    'ff_ff',
                    'ff_ff',
                    'ee_ee',
                    'ee_ee',
                    'ee___',
                    'ee_ee',
                    'ee_ee',
                    'ee_ee',
                    'eeeee',
                ],
                seats: {
                    f: {
                        price :document.getElementById("p"),
                    },
                    e: {
                        price   : document.getElementById("p"),
                    },
                                        
                
                },
                naming : {
                    top : false,
                    getLabel : function (character, row, column) {
                        return firstSeatLabel++;
                    },
                },
                legend : {
                    node : $('#legend'),
                    items : [
                        [ 'f', 'available',   'Available' ],
                        [ 'f', 'unavailable', 'Already Booked']
                    ]                   
                },
                click: function () {
                    if (this.status() == 'available') {
                    
                        $('<li> Seat - '+this.settings.label+': <b>$'+this.data().price+'</b> <a href="#" class="cancel-cart-item">[cancel]</a></li>')
                            .attr('id', 'cart-item-'+this.settings.id)
                            .data('seatId', this.settings.id)
                            .appendTo($cart);
                        
                        
                        $counter.text(sc.find('selected').length+1);
                        $total.text(recalculateTotal(sc)+this.data().price);
                        
                        return 'selected';
                    } else if (this.status() == 'selected') {
                        $counter.text(sc.find('selected').length-1);
                        $total.text(recalculateTotal(sc)-this.data().price);
                    
                        $('#cart-item-'+this.settings.id).remove();
                    
                        return 'available';
                    } else if (this.status() == 'unavailable') {
                        return 'unavailable';
                    } else {
                        return this.style();
                    }
                }
            });
            $('#selected-seats').on('click', '.cancel-cart-item', function () {
                sc.get($(this).parents('li:first').data('seatId')).click();
            });


    });

    function recalculateTotal(sc) {
        var total = 0;
        sc.find('selected').each(function () {
            total += this.data().price;
        });
        
        return total;
    }
        
    </script>
    

How can I achieve this?

Kindly answer my question! Immediate response is appreciatable.

Thank you in advance:)

Belle
  • 39
  • 5
  • Does this answer your question? [Passing parameters to another JSP file using tag](https://stackoverflow.com/questions/19150683/passing-parameters-to-another-jsp-file-using-jspinclude-tag) – mohammedkhan Jun 09 '22 at 10:14
  • No,I need to pass the data from my jsp file to servlet without using form – Belle Jun 09 '22 at 10:45
  • The question and answer on there has nothing to do with using a form. – mohammedkhan Jun 10 '22 at 08:32

1 Answers1

0

In display.jsp, in the href, you can have a servlet instead of another jsp page and that servlet can pass as a requestParameter the value you need, and you can perform a forward to your seat.jsp. You can, then, read the parameter that you need in your seat.jsp.

I hope that helps.

Dan Chirita
  • 119
  • 9
  • I used servlet code in display.jsp - "<% String id=request.getAttribute("price").toString(); request.setAttribute("price", id); request.getRequestDispatcher("seat.jsp").forward(request, response); %>" . Now the price is passed to the seat.jsp, but it skips the display.jsp.The contents of display.jsp is not shown in browser.Do you have any idea? – Belle Jun 07 '22 at 13:53
  • I was referring to creating a standalone servlet, e.g. PriceServlet which should be used in the href instead of seat.jsp. `` – Dan Chirita Jun 07 '22 at 14:27
  • I tried , but it shows HTTP 404 ERROR ,"The origin server did not find a current representation for the target resource or is not willing to disclose that one exists." – Belle Jun 08 '22 at 12:33