Home > Mobile >  Variable returns null
Variable returns null

Time:06-10

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 >
  
  <th>Fare</th>
  
</tr>
<tr>
  <td id="p">INR ${price }</td>
 
</tr>

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

seat.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<body>
    <div >
        <div >
            <div id="seat-map" >
                <div >Front</div>
                
            </div>
            <div >
                <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 >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="#" >[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:)

CodePudding user response:

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.

  • Related