So I am trying to use a simple form to get input from a user, retrieve the necesary info from the database, and post it. However, on the HTML page, the submit button just clears the form. Below is the html file and the java file that is referenced in the action. I know the file that interacts with the database is missing but I think my issue lies in one of these two. I can post it later if needed. My question is, why is the submit button clearing the form instead of bringing you to the site in “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
}
Because you have two
<form>tags. The first one (with no action) is the one that the browser users. Remove it and it should start working as expected.