我在jsp文件中通过ajax从servlet获取数据时遇到问题。我的servlet已连接到mySQL Workbench数据库。我正在使用fullcalendar插件,似乎无法通过servlet从数据库中获取必要的数据。有人可以帮我吗?
这些是我的CalendarIO.jsp代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel='stylesheet' href='fullcalendar/fullcalendar.css' />
<script src='lib/jquery.min.js'></script>
<script src='lib/moment.min.js'></script>
<script src='fullcalendar/fullcalendar.js'></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
// fullcalendar functions
$(document).ready(function() {
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
selectable: true,
selectHelper: true,
select: function(start, end) {
var title = prompt('Event Title:');
var events;
var eventData;
if (title) {
eventData = {
title: title,
start: start,
end: end
};
$('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true
}
$('#calendar').fullCalendar('unselect');
},
editable: true,
eventLimit: true,
eventClick: function(event){
$(".closon").click(function() {
$('#calendar').fullCalendar('removeEvents',event._id);
});
},
events: { url: 'http://localhost:8080/ENWP_Calendar/CalBean', type: 'POST'},
eventRender: function(event, element) {
element.append( "<span class='closeon'>X</span>" );
element.find(".closeon").click(function() {
$('#calendar').fullCalendar('removeEvents',event._id);
});
}
});
});
</script>
<body>
<div id="calendar" style="width: 70%; height: 40%; display: block; margin: auto; padding-top: 20%; padding-bottom: 20%;"></div>
</body>
</html>
这些是我的servlet(CalBean.java)代码:
package servlet;
import java.beans.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.sun.istack.internal.logging.Logger;
import model.CalInfo;
import net.sf.json.JSONArray;
/**
* Servlet implementation class CalBean
*/
@WebServlet("/CalBean")
public class CalBean extends HttpServlet {
Calendar now = Calendar.getInstance();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList array = new ArrayList();
try {
Connection con = null;
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/?
user=root");
Statement stmt = null;
String query = "select title, start " +
"from calendar ";
stmt = (Statement) con.createStatement();
ResultSet rs = ((java.sql.Statement) stmt).executeQuery(query);
while (rs.next()) {
CalInfo c = new CalInfo();
c.setTitle(rs.getString("title"));
c.setStart(rs.getString("start"));
String t = rs.getString("TYPE_CODE");
array.add(c);
}
}
catch (SQLException ex) {}
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(new Gson().toJson(array));
}
}
非常感谢大家!自2周前以来,我一直在尝试解决此问题...