如何将日期ID从一个jsp传递到另一个jsp页面

时间:2022-02-27 08:09:56

I am calculating the price of a ticket with discount on weekdays and weekends based on the time duration.So, i gave inputs of duration and date using datepicker plugin which is in Above Page. For this i am getting proper result.But i have to create two different jsp pages(date.jsp and cal.jsp). In 1st jsp page(date.jsp) i am selecting date using datepicker. And in 2nd jsp page(cal.jsp)

我根据持续时间计算工作日和周末折扣票的价格。所以,我使用在页面上方的datepicker插件输入持续时间和日期。为此,我得到了正确的结果。但我必须创建两个不同的jsp页面(date.jsp和cal.jsp)。在第一个jsp页面(date.jsp)中,我使用datepicker选择日期。在第二个jsp页面(cal.jsp)

I have written a method ->[caluculate(#dateid,#duratiionid)] to calulate the price by taking inputs as time duration.

我写了一个方法 - > [caluculate(#dididid,#duratiionid)]来通过将输入作为持续时间来计算价格。

Here My Question is how shall i pass [#dateid] from 1st jsp page(date.jsp) to 2nd jsp page(cal.jsp) so that i can pass both the id's in this method->[caluculate(#dateid,#duratiionid)].

这里我的问题是如何将[#dateid]从第一个jsp页面(date.jsp)传递到第二个jsp页面(cal.jsp),以便我可以在这个方法中传递两个id - > [caluculate(#didid,# duratiionid)。

<div id="container">
    <div id="form">
        <form id="book_court">
            <div class="fieldset">
            <fieldset>
                <legend class="visuallyhidden">Booking Details</legend>
                <h2>Booking Details</h2>                 
                <p>
                    <label for="date">Date<br/><span id="dateNote">Firefox does not have a HTML5 datepicker <a href="https://support.mozilla.org/en-US/questions/986096">yet</a>.</span></label>
                    <input type="date" name="date" id="date" min="today" required />
                </p>
                <p>
                    <label for="tickets_duration"> Hours</label>
                    <input type="number" min="1" name="tickets_duration" id="tickets_duration" required />
                </p>

                <p>
                    <label>Total Price</label>
                    <span id="total_price">(enter data first)</span>
                </p>
                <div id="submit_wrapper">
                    <input type="submit" id="submit" value="Book Court" />
                </div>
            </fieldset>
            </div>
        </form>
        </div>
        </div>
<script id="worker" type="javascript/worker">
    self.onmessage = function msgWorkerHandler(event){
        var jsonString = event.data;

        var day = jsonString.day;
        var tickets_duration = jsonString.tickets_duration;

        // set price of each hours as Rs. 200 and 300
        var totalPriceOnWeekday = tickets_duration * 200;
        var totalPriceOnWeekends=tickets_duration * 300;

        // 10% discount if on weekday and 15% on weekends
        if(day > 0 && day < 6){
            totalPriceOnWeekday = totalPriceOnWeekday - 0.10 * totalPriceOnWeekday;
           postMessage("&#x20b9; " + totalPriceOnWeekday);
        }else if(day == 0 || day == 7){
            totalPriceOnWeekends = totalPriceOnWeekends - 0.15 * totalPriceOnWeekday;

        postMessage("&#x20b9; " + totalPriceOnWeekends);
      }
    }
</script>

<script>
$(document).ready(function(){

    // first check the movies already book

    // apply jQuery UI Redmond theme to 'Book Tickets' button
    $("#submit").button();

    // calculateTotalPrice on keyup or on change of movie/date/tickets
    $("#date, #tickets_duration").change(calculateTotalPrice);

    // on form submit
    $("#book_court").submit(function(event){

        // prevent on submit page refresh
        event.preventDefault();

        // check locally stored data     
        // clear the form                   
        $( '#book_court' ).each(function(){
            this.reset();
        });

        // reset (enter data first) message
        $("#total_price").html("(enter data first)");

        // update movies booked list
    });

    // set minimum date in datepicker as today
    var today = new Date().toISOString().split('T')[0];
    document.getElementsByName("date")[0].setAttribute('min', today);

});

function calculateTotalPrice(){
    if($("#tickets_duration").val() != "" && $("#date").val() != ""){
        if(window.Worker){
            // create web worker
            var blob = new Blob(
                [document.querySelector("#worker").textContent],
                {type: 'text/javascript'});
            var worker = new Worker(window.URL.createObjectURL(blob));

            worker.onmessage = function(event){
                $("#total_price").html(event.data);
            }
            worker.onerror = function(errorObject){
                $("#total_price").html("Error: " + errorObject.message);
            }               
              var date = new Date($('#date').val());

            // get day
            var day = date.getDay();

            // get number of booked shows

            // send JSON data to worker                     
            var jsonData = {'day': day, 'tickets_duration': Number($("#tickets_duration").val())};
            worker.postMessage(jsonData);
        }
    }
}
</script>

1 个解决方案

#1


0  

If you want to share a value cross-pages, you can use cookie to store the value. Another option is localStorage/sessionStorage if you are using HTML5. So, in the first page (date.jsp), when user select a date, you can store that selection to cookie, in the second page (cal.jsp) you can read that value from cookie and then do your calculation.

如果要跨页共享值,可以使用cookie来存储值。如果您使用的是HTML5,则另一个选项是localStorage / sessionStorage。因此,在第一页(date.jsp)中,当用户选择日期时,您可以将该选择存储到cookie,在第二页(cal.jsp)中,您可以从cookie中读取该值,然后进行计算。

I suppose that you are able to post the date string back to servlet. In the servlet, you are using that date string to check for ticket validity. You just don't know how to check if the date is weekday or weekends. If so you can you the java.util.Calendar for that purpose. EDITED

我想你可以将日期字符串发布回servlet。在servlet中,您使用该日期字符串来检查票证有效性。你只是不知道如何检查日期是工作日还是周末。如果是这样,你可以为此目的使用java.util.Calendar。 EDITED

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date yourDate = formatter.parse(dateInString);
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

#1


0  

If you want to share a value cross-pages, you can use cookie to store the value. Another option is localStorage/sessionStorage if you are using HTML5. So, in the first page (date.jsp), when user select a date, you can store that selection to cookie, in the second page (cal.jsp) you can read that value from cookie and then do your calculation.

如果要跨页共享值,可以使用cookie来存储值。如果您使用的是HTML5,则另一个选项是localStorage / sessionStorage。因此,在第一页(date.jsp)中,当用户选择日期时,您可以将该选择存储到cookie,在第二页(cal.jsp)中,您可以从cookie中读取该值,然后进行计算。

I suppose that you are able to post the date string back to servlet. In the servlet, you are using that date string to check for ticket validity. You just don't know how to check if the date is weekday or weekends. If so you can you the java.util.Calendar for that purpose. EDITED

我想你可以将日期字符串发布回servlet。在servlet中,您使用该日期字符串来检查票证有效性。你只是不知道如何检查日期是工作日还是周末。如果是这样,你可以为此目的使用java.util.Calendar。 EDITED

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date yourDate = formatter.parse(dateInString);
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);