0

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

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by komalikaa
0

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

Since you are using servlets your server needs to be running so that it can interpret your servlet and respond to it..
Now your first mistake is
You got to right click on your HTML file in netbeans and click "Run File" instead of "view".

The difference is that if you view your html file netbeans doesnt start the server rather just displays the HTML output in your browser.
If you run your html file then you can find in your netbeans in the bottom right area you can find your server starting first.

You can set your action as
<form action="getStudentInfo" method="post">

I suggest you use post method instead of get.
The reason is when you use your get method the parameters are visible to others through the browser URL.. ( All the parameters passed get appended to the URL)
If you use post all the parameters are sent as packets to the servlet...

Just try it out.. Happy coding.. Kindly inform me if this helps.. I'll check out your code and inform you soon..

0

I've checked out your code and is perfectly working except for the following changes to be made...

Now the first problem is, you got to place your html file in the web folder of your project. You can manually copy it from the
directory where it is currently residing (" for me it is in C:\Users\Administrator\Documents\NetBeansProjects\WebApplication1\src\java\Chapter39\ ") and paste it in (" C:\Users\Administrator\Documents\NetBeansProjects\WebApplication1\web\"). After copying it there you can find in your netbeans that the file appears alongside your WEB-INF folder and your index.jsp..

In place of "administrator" kindly use your "username" in which you have installed netbeans or u r currently logged on to..

Secondly change your action to action="getStudentInfo" and not action="/getStudentInfo"

Third in netbeans you got to right click on your HTML file in the changed location right click and select "Run File" instead of "view".

The difference is that if you view your html file netbeans doesnt start the server rather just displays the HTML output in your browser.
If you run your html file then you can find in your netbeans in the bottom right area you can find your server starting first.

You can set your action as
<form action="getStudentInfo" method="post">

I suggest you use post method instead of get.
The reason is when you use your get method the parameters are visible to others through the browser URL.. ( All the parameters passed get appended to the URL). You can see it yourself if u use GET method.
If you use post all the parameters are sent as packets to the servlet... You wont find any paramters appended to your URL. Check this also.. :)

Just try it out.. Happy coding.. Kindly inform me if this helps.. Check it out and tell soon.. Thanks if i could be of any help to you.. And if it is perfectly working you can mark this thread solved. Else post any other doubt.. I'll try to help you out..
Happy new year
RAKHI

0

Hi Rakhi, thank you very much for your help and support. Some stuff I would like to point out.

1) I found out the problem why it wasn't working. Instead of doing the action = "getStudent", it was supposed to be the whole path for some reason. As in http://localhost:8080/getStudent.

2) I changed my ide from netbeans to eclipse ee. I found eclipse is more easier to use and my future jsp class is going to be using it so I thought why not learn this. Also, just like netbeans, eclipse ee can integrate tomcat inside.

3) I was using the get method because I wasn't really showing anything confidential and wasn't updating to a database. I agree that post is the most useful at times.

4) Thank you again for your time and effort. I hope people learn from it.

5) Rakhi one more question. How important is it to know servlets, jsp, jsf, in the future for jobs? I'm not the biggest fan of web development but I still like it a lot. Also, I heard if you want to go to a big company learn the JSP or ASP but if you want freelancing, go for PHP. Is this true? Thanks.

0

Well it isnt necessary that we need to give the entire path if the servlet info is present in the web.xml file.. It is enough to specify the servlet name. So only i told that way.. And moreover since u were using netbeans and asked regarding that i posted info related to netbeans only.. Even i feel eclipse much much easier to use..

Well regarding using get and post methods.. I just gave some info regarding that so that it might be helpful in future.I was not sure whether you knew it or not. Sorry if u knew it earlier and i mentioned about it again..

And coming to the final part. I'm not much into web development and am also a student of final year.. Just felt i knew the error in your code and hence thought of helping you out.. But as far as i know. JSP is very scalable and it is used for minimizing the load on the servers by providing means for 3 tier architecture..

It allows the programmer to do the logic part and designers to implement the design part thereby segregating the work. Am sorry i dont know whether you could get something out of this. But I'll try to gather little more info regarding your question and post it..

Thank you.. regards.. RAKHI

0

No no Rakhi you helped out a lot thank you man. Don't even worry about that. I'm actually a 4th year student myself and want to know a little more info. At first I thought jsp and servlets were mostly all web stuff as in (xhtml, css) but after studying it alone for a couple of weeks, I found that it's mostly all java. I love java and have about 1.5 year experience on it. Our school's main core is java. I've been hearing it lately and lately that web is the way to go for the future. I will be learning unix shell scripting and c++ next quarter which is also very good to know. Do you know what you want to do after you graduate? THanks.

0

Ya i have offers from Infosys and Cognizant.. But am planning to do my Masters in Businees Administration as i'm more interested in that.. I'm just your junior as i've only 6 months experience in java. But still i love it.. Am happy to know it helped you.. Thank you very much.

0

How can labels be used with form action in jsp ? eg I have to ass label as "abc"
to <html:form action="/AddCart method="GET" label="AddCarttForm">
Any help would be appreciated

Edited by komalikaa

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.