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

时间:2016-05-14 15:28:01

标签: javascript jquery jsp

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

我写了一个method ->[caluculate(#dateid,#duratiionid)]来计算价格,将输入视为持续时间。

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

<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 个答案:

答案 0 :(得分:0)

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

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

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);