我正在制作一个小页面,我可以跟踪我的工作时间。我的表格看起来像这样:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Working Hours</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("#datepicker").datepicker();
});
$(document).ready(function() {
$("#datepicker2").datepicker();
});
</script>
</head>
<body>
<form action="../dataExchange" method="POST">
Day Hours: <input type="text" name="Day_hours"><br>
Day Minutes: <input type="text" name="Day_minutes"><br>
Allday Hours: <input type="text" name="Allday_hours"><br>
<input type="submit" value="submit"><br><br><br>
</form>
<form>
<input id="datepicker" />
</form>
<form>
<input id="datepicker2" />
</form>
</body>
这里我还有一个javascript,即日历,其中有“从”日期“到”日期。
我与数据库的连接通过一个servlet,如下所示:
package WorkPackage;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/dataExchange")
public class dataExchange extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
String connectionURL = "jdbc:mysql://localhost/NekiWork";
Connection connection=null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String Date = req.getParameter("Date");
String Name = req.getParameter("Name");
String Address = req.getParameter("Address");
String Allday_hours = req.getParameter("Allday_hours");
String Day_hours = req.getParameter("Day_hours");
String Day_minutes = req.getParameter("Day_minutes");
String Km_to_address = req.getParameter("Km_to_address");
String Time_to_address = req.getParameter("Time_to_address");
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
String sql = "INSERT INTO Workdata VALUES (?,?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, Date);
pst.setString(2, Name);
pst.setString(3, Address);
pst.setString(5, Day_hours);
pst.setString(6, Day_minutes);
pst.setString(4, Allday_hours);
pst.setString(7, Km_to_address);
pst.setString(8, Time_to_address);
pst.executeUpdate();
pst.close();
}
catch(ClassNotFoundException e){
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e){
out.println("SQLException caught: " + e.getMessage());
}
catch (Exception e){
out.println(e);
}
finally {
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
out.println(ignored);
}
}
}
}
所以我的问题是,如何获取我的javascript日历以从我的数据库中获取信息?我可以从javascript连接 - &gt; servlet - &gt; MySQL数据库,还是我必须完全做其他事情?
最诚挚的问候 MADS
答案 0 :(得分:0)
通常,您可以使用返回某些内容的servlet。它可以是任何东西:JSON,XML,文本等等。您可以使用AJAX调用servlet并在javascript中处理结果。或者,您可以使用常规HTML <form>
元素,如上例所示。
如果不确切知道自己想要什么,就很难更具体。
如果您的意思是上面的例子,那么您不会调用PrintWriter out
的任何方法(除非获得例外)。您可能还会考虑内容类型(现在您正在使用text/html
),也许JSON可能是更好的选择。