Hey guys i have a problem and I'm starting to fall short of solutions. Here is the scenario. I created a student registration HTML file and a servlet to answer this file. The problem is the submit button doesn't work on the html form. I have tried dozens and dozens of ways to put the right "action = path" but it doesn't seem to comply. This is what I did step-by-step.
In NETBEANS 6.9.1
1) Create new web application project.
2) Under source packages, I created a new Chapter39 package.
3) After that, I right-clicked the chapter39 package and created a HTML form and a java servlet.
4) After creating both, I right clicked the HTML form and clicked "VIEW" and it opened up in my firefox.
5) I entered the information into the html form and when I clicked submit, it said "FILE NOT FOUND". When I went to the location of the file, it was there. So I don't understand.... what's going on.
HERE IS THE HTML CODE.
1. <html>
2. <head><title> Student Registration Form </title></head>
3. <body bgcolor ="black" text ="white">
4.
5. <h2> Student Registration Form </h2>
6.
7. <form action = "/getStudentInfo" method = "GET">
8.
9. <p>
10. <label> Last Name </label>
11. <input type ="text" name ="lastName" size ="25" />
12. <label> First Name </label>
13. <input type ="text" name="firstName" size="25"/>
14. <label> MiddleInitial </label>
15. <input type="text" name="middleInitial"size="1" />
16. </p>
17.
18. <p>
19. <label> Gender </label>
20. <input type ="radio" name ="gender" value ="M"/> Male
21. <input type ="radio" name ="gender" value ="F"/> Female
22. </p>
23.
24. <p>
25. <label> Major </label>
26. <select name ="Major" size="1">
27. <option value ="CS">Computer Science</option>
28. <option value ="Math">Mathematics</option>
29. <option>Physics</option>
30. <option>Chemistry</option>
31. </select>
32.
33.
34. <label> Minor </label>
35. <select name ="Minor" size="2">
36. <option>Computer Science</option>
37. <option>Mathematics</option>
38. <option>Physics</option>
39. <option>Chemistry</option>
40. </select>
41. </p>
42.
43. <p>
44. <label> Hobby </label>
45. <input type ="checkbox" value="tennis" /> Tennis
46. <input type ="checkbox" value="golf" /> Golf
47. <input type ="checkbox" value="pingpong" />Ping Pong
48. </p>
49.
50. <p> <label> Remarks </label> </p>
51. <p> <textarea name ="remarks" rows="4" cols="50"></textarea> </p>
52.
53. <p>
54. <input type ="submit" value ="Submit"/>
55. <input type="reset" value ="Reset"/>
56. </p>
57.
58.
59. </form>
60.
61.
62.
63. </body>
64. </html>
HERE IS THE SERVLET CODE
1. package Chapter39;
2.
3. import java.io.IOException;
4. import java.io.PrintWriter;
5. import javax.servlet.ServletException;
6. import javax.servlet.http.HttpServlet;
7. import javax.servlet.http.HttpServletRequest;
8. import javax.servlet.http.HttpServletResponse;
9.
10. public class getStudentInfo extends HttpServlet {
11.
12.
13. protected void doGet(HttpServletRequest request, HttpServletResponse response)
14. throws ServletException, IOException {
15.
16.
17.
18. response.setContentType("text/html");
19. PrintWriter writer = response.getWriter();
20.
21.
22. String lastName = request.getParameter("lastName");
23. String firstName = request.getParameter("firstName");
24. String gender = request.getParameter("gender");
25. String major = request.getParameter("Major");
26. String minors[] = request.getParameterValues("Minor");
27. String tennis = request.getParameter("tennis");
28. String golf = request.getParameter("golf");
29. String pingPong = request.getParameter("pingpong");
30. String remarks = request.getParameter("remarks");
31.
32. writer.println("Last Name: <b>" + lastName + "</b>");
33. writer.println("First Name: <b>" + firstName + "</b><br />");
34. writer.println("Gender: <b>" + gender + "<b><br />");
35. writer.println("Major: <b>" + major + "</b>");
36. writer.println("Minor: <b>");
37.
38. if(minors != null)
39. for(int i = 0; i<minors.length; i++)
40. writer.println(minors[i] + " ");
41.
42. writer.println("</b><br />");
43. writer.println("Tennis: <b>" + tennis + "</b>");
44. writer.println("Golf <b>" + golf + "</b>");
45. writer.println("PingPong: <b>" + pingPong + "</b><br />");
46. writer.println("Remarks: <b>" + remarks + "<br />");
47. writer.close();
48. }
49.
50.
51. }
HERE IS THE WEB.XML FILE
1. <?xml version="1.0" encoding="UTF-8"?>
2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
3. <servlet>
4. <servlet-name>getStudentInfo</servlet-name>
5. <servlet-class>Chapter39.getStudentInfo</servlet-class>
6. </servlet>
7. <servlet-mapping>
8. <servlet-name>getStudentInfo</servlet-name>
9. <url-pattern>/getStudentInfo</url-pattern>
10. </servlet-mapping>
11. <session-config>
12. <session-timeout>
13. 30
14. </session-timeout>
15. </session-config>
16. <welcome-file-list>
17. <welcome-file>index.jsp</welcome-file>
18. </welcome-file-list>
19. </web-app>
Here is the URL for the HTML File
file:///K:/Web%20Programming/Servlets/IntroToServlets/src/java/Chapter39/StudentRegistration.html
Here is the URL for the servlet
http://localhost:8084/getStudentInfo
Both the HTML file and the servlet file are in the same folder.
Please just tell me what I must put for the "ACTION = " in the html form.
I have tried .....
action = "getStudentInfo"
action = "/getStudentInfo"
action = "/servlet/getStudentInfo