如何将日期标识从一个jsp传递到另一个jsp页面
问题描述:
我正在计算基于持续时间的工作日和周末折扣价格。所以,我给出了使用datepicker插件的持续时间和日期输入在页面上方。为此我得到了正确的结果。但是我必须创建两个不同的jsp页面(date.jsp和cal.jsp)。 在第1个jsp页面(date.jsp)中,我使用datepicker选择日期。并在第二个jsp页面(cal.jsp)如何将日期标识从一个jsp传递到另一个jsp页面
我写了一个method ->[caluculate(#dateid,#duratiionid)]
来计算输入的时间作为持续时间的价格。
这里我的问题是如何将我通过[#dateid]
从1 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("₹ " + totalPriceOnWeekday);
}else if(day == 0 || day == 7){
totalPriceOnWeekends = totalPriceOnWeekends - 0.15 * totalPriceOnWeekday;
postMessage("₹ " + 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>
答
如果要共享一个跨页面的值,可以使用cookie来存储该值。如果您使用的是HTML5,另一种选择是localStorage/sessionStorage。 因此,在第一页(date.jsp)中,当用户选择一个日期时,可以将该选择存储到cookie中,在第二个页面(cal.jsp)中,您可以从cookie读取该值,然后进行计算。
我想你可以把日期字符串发回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);
感谢#你能否提供更多的细节?通过给出示例代码。 – Kumar
此流程中是否包含任何服务器端代码?如果是这样,您可以将选定日期发布到服务器,然后在那里进行计算并显示在cal.jsp中。如果没有服务器端代码,则可以使用“document.cookie”将选定的日期保存在date.jsp中,然后使用相同的“document.cookie”在cal.jsp中检索它以进行计算。 – DoT
是的。我使用servlet和MySQL来存储日期。但我不知道如何计算服务器端的价格(工作日+周末)??? ...你能给我一个代码示例..谢谢 – Kumar