我有一个应用程序,每周显示一个时间表。所有约会都存储为数据库条目。我使用类(ScheduleCells
)来管理这些条目并将它们存储在bean(ScheduleCellbean
)中并返回一个二维ArrayList。在我的index.jsp
中,我有以下代码来检索此ArrayList并填写计划。在index.jsp
中,我向servlet发送请求。
的index.jsp:
<script type="text/javascript">
fillSchedule = function (w) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "schedule?w=" + w);
xhr.send();
<%
int i;
int j;
int w = 0;
if (session.getAttribute("w") != null) {
w = (Integer) session.getAttribute("w");
}
ScheduleCells sc = new ScheduleCells();
for (i = 0; i < 9; i++) {
for (j = 0; j < 5; j++) {
ScheduleCellBean cell = sc.getCellByHDW(i + 1, j + 1, w);
if (cell.getExists()) {
%>
createNewAppointment(<%= j%>, <%= i%>,
"<%= cell.getSubject_scheduled_subjectabbr()%>",
"<%= cell.getUser_scheduled_userabbr()%>",
"<%= cell.getScheduled_groupname()%>");
<% }}}%>
};
fillSchedule(7);
</script>
ScheduleCellbean:
public class ScheduleCellBean extends ScheduleCells {
private boolean exists = false;
private String scheduled_id;
private String scheduled_date;
private String scheduled_hour;
private String user_scheduled_firstname;
private String user_scheduled_lastname;
private String user_scheduled_username;
private String user_scheduled_password;
private String user_scheduled_userabbr;
private String user_scheduled_subjectname;
private String user_scheduled_subjectabbr;
private String scheduled_groupname;
private String subject_scheduled_subjectname;
private String subject_scheduled_subjectabbr;
private String scheduled_datetimerequest;
private String user_request_firstname;
private String user_request_lastname;
private String user_request_username;
private String user_request_password;
private String user_request_userabbr;
private String user_request_subjectname;
private String user_request_subjectabbr;
//Getters And Setters.
}
ScheduleCellServlet.jsp:
@WebServlet(name = "ScheduleCell", urlPatterns = {"/schedule"})
public class ScheduleCellServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
HttpSession session = request.getSession();
session.setAttribute("w", Integer.parseInt(request.getParameter("w")));
}
//Default HTTP Servlet functions.
}
一切正常,但因为我在那里使用servlet会有延迟,这会导致计划似乎无法加载,但在刷新时,会话属性会通过并且正确计划已加载。因此,我所做的任何更改(例如fillSchedule(8)
)仅在我重新加载页面时发生。但我喜欢互动的时间表,这意味着用户可以选择一周,并立即弹出该周的正确时间表。我看到的时间表在我选择的内容方面缺乏落后,当我重新阅读页面时,我所选择的时间表是可见的,但同时第7周的时间表再次被加载,但只有当我再次加载时才能看到页。
我的问题是:我怎样才能以不必(手动)刷新页面的方式实现这一目标?
提前感谢您的帮助!
答案 0 :(得分:0)
自动刷新您的jsp页面以获得新的响应。
代码段:
<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Auto Refresh Header Example</title>
</head>
<body>
<center>
<h2>Auto Refresh Header Example</h2>
<%
// Set refresh, autoload time as 5 seconds
response.setIntHeader("Refresh", 5);
// Get current time
Calendar calendar = new GregorianCalendar();
String am_pm;
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
if(calendar.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";
String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
out.println("Crrent Time: " + CT + "\n");
%>
</center>
</body>
</html>