所以我试图使用一个简单的表单来获取用户的输入,从数据库中检索必要的信息,然后发布它。但是,在HTML页面上,提交按钮只是清除表单。下面是html文件和操作中引用的java文件。我知道缺少与数据库交互的文件,但我认为我的问题在于这两个中的一个。如果需要,我可以稍后发布。我的问题是,为什么提交按钮会清除表单而不是将您带到#34; action"?
<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
}
答案 0 :(得分:2)
因为您有两个<form>
标签。第一个(没有动作)是浏览器用户使用的那个。删除它,它应该按预期开始工作。
<form> <-- Remove this one
<FORM METHOD=POST ACTION = "http://uml.cs.uga.edu:8080/michael_crosby_courses/Schedule">