1
因此,我嘗試使用簡單的表單從用戶獲取輸入,從數據庫中檢索必要的信息併發布。但是,在HTML頁面上,提交按鈕只是清除表單。以下是操作中引用的html文件和java文件。我知道與數據庫交互的文件丟失了,但我認爲我的問題在於其中之一。如果需要,我可以稍後發佈。我的問題是,爲什麼提交按鈕會清除表單而不是將您帶入網站的「操作」中?提交按鈕無需發帖即可清除表格
<form>
<FORM METHOD=POST ACTION = "http://uml.cs.uga.edu:8080/michael_crosby_courses/Schedule">
Class 1: <input type=text name="Class1"><br>
Class 2: <input type=text name="Class2"><br>
Class 3: <input type=text name="Class3"><br>
Class 4: <input type=text name="Class4"><br>
<input type=submit>
</form>
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Schedule extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter toClient = res.getWriter();
String field;
String val;
Vector classes = null;
Vector totalHours = null;
String query = null, query2 = null;
String class1 = null, class2 = null, class3 = null, class4 = null;
int i, sum=0;
res.setContentType("text/html");
toClient.println("<html>");
toClient.println("<title>MyEcho</title>");
toClient.println("<body>");
Enumeration values = req.getParameterNames();
if(values.hasMoreElements()) {
class1 = req.getParameter("Class 1");
class2 = req.getParameter("Class 2");
class3 = req.getParameter("Class 3");
class4 = req.getParameter("Class 4");
toClient.println("<P><P><B>Your Schedule:</B></P></P>");
query = "select * from csci_section where number = " + class1 + " or number = " + class2 + " or number = " + class3 + " or number = " + class4 + ";";
}
query2 = "select credit from csci_section where number = " + class1 + " or number = " + class2 + " or number = " + class3 + " or number = " + class4 + ";";
if(query == null)
toClient.println("<P><P><B>No query given; resubmit </B>");
else if(1==1) { //if the request did not return anything, i.e. the number given is not a class
} else{
toClient.println("<P><B>Running search for classes: </B>" + class1 + ", " + class2 + ", " + class3 + ", " + class4 + ", " + "<P><P>");
classes = AccessMySQL.Execute(query);
totalHours = AccessMySQL.Execute(query2);
toClient.println("<P><P><B>Received classes: </B></P></P>");
for(i = 0; i < classes.size(); i++){ //prints out info
toClient.println("<p><tt>" + (String)classes.elementAt(i) + "</tt></p>");
}
for(i=0; i < totalHours.size(); i++){
sum += Integer.parseInt((totalHours.elementAt(i)).toString());
}
toClient.println("<p><tt>The total number of class hours is: " + sum + "</tt></p>");
}
toClient.println("</body>");
toClient.println("</html>");
toClient.close();
} //closes doPost
}
男人試一下,問一個具體問題 – Ali
你的HTML無效。驗證它,然後嘗試發佈它,然後調試您的服務器端代碼。 –