因此,我尝试使用一种简单的形式从用户获取输入,从数据库中检索必要的信息,然后将其发布。但是,在HTML页面上,“提交”按钮仅会清除表单。以下是该操作中引用的html文件和java文件。我知道与数据库交互的文件丢失了,但我认为问题出在这两个问题之一。如果需要,我可以稍后发布。我的问题是,为什么提交按钮会清除表单而不是使您进入“行动”网站?
Class 1:
Class 2:
Class 3:
Class 4:
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("");
toClient.println("
MyEcho");toClient.println("
");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("
Your Schedule:
");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("
No query given; resubmit ");
else if(1==1) { //if the request did not return anything, i.e. the number given is not a class
} else{
toClient.println("
Running search for classes: " + class1 + ", " + class2 + ", " + class3 + ", " + class4 + ", " + "
" );
classes = AccessMySQL.Execute( query );
totalHours = AccessMySQL.Execute(query2);
toClient.println("
Received classes:
");for( i = 0; i < classes.size(); i++ ){ //prints out info
toClient.println( "
" + (String)classes.elementAt( i ) + "
" );}
for(i=0; i < totalHours.size(); i++){
sum += Integer.parseInt((totalHours.elementAt(i)).toString());
}
toClient.println("
The total number of class hours is: " + sum + "
" );}
toClient.println( "" );
toClient.println("");
toClient.close();
} //closes doPost
}
最佳答案
因为您有两个
标记。第一个(不执行任何操作)是浏览器用户使用的。删除它,它应该会按预期开始工作。