reset password
Author Message
Vanquish39
Posts: 134
Posted 20:39 Mar 31, 2011 |

I'm reading ahead a little and I have a little problem.  If anyone can answer it, I would be grateful.


I have a new project called Chapter4.

Under the WebContent folder, I have a html file and inside the file there is a <form action = "?????">

I have a servlet under /src/Form1/HtmlServlet.java.  The Form1 is a package. 

My question is, what do I write in the question mark area of  the <form action = "??????">  so that the HTML can call the servlet HtmlServlet.java?


I've tried

<form action="/src/Form1/HtmlServlet.java>

<form action="/src/Form1/HtmlServlet>

<form action="/Form1/HtmlServlet>

None worked.  Anyone? Thanks

cysun
Posts: 2935
Posted 20:43 Mar 31, 2011 |

Your servlet should be mapped to a URL in @WebServlet. The source code from the textbook is probably for an older version of Tocmat so you may need to add the @WebSevlet yourself. Then the form action is simply the URL of the servlet.

Vanquish39
Posts: 134
Posted 20:49 Mar 31, 2011 |

Do you mean I have to write something here?

@WebServlet("/ThreeParamsServlet")


Or do I have to write something in web.xml?  I don't understand what you mean by "Mapped"

 

Are you referring to something of this nature.

<web-app>
  <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>servletexample.createHelloWorldExample
      </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorldExample/*</url-pattern>
  </servlet-mapping>
</web-app>
Last edited by Vanquish39 at 20:50 Mar 31, 2011.
cysun
Posts: 2935
Posted 20:58 Mar 31, 2011 |

Either way should work if you are using Tomcat 7 and Servlet 3.0 (see the "version" attribute of <web-app> in your web.xml).

If you are using Tomcat 6 / Servlet 2.5, you have to do it in web.xml.

Vanquish39
Posts: 134
Posted 21:21 Mar 31, 2011 |

Still doesn't seem to be working professor.

Location of servlet:  K:\CS320\Chapter4\src\Form1\ThreeParamsServlet.java

Location of html: K:\CS320\Chapter4\WebContent\ThreeParamsForm.html


This is inside the servlet

@WebServlet(name="ThreeParamsServlet", urlPatterns={"/ThreeParamsServlet", "/ThreeP"})


None of these work.

<form action = "/Form1/ThreeParamsServlet" method = "GET" >

<form action ="/ThreeParamsServlet" method = "GET">

<form action = "/ThreeP" method = "GET">

<form action = "ThreeParamsServlet" method = "GET">

 

For testing purposes, I right click the html file and I click "open with web browser".  Then I click submit

I'm also using tomcat 7.

 

What am I doing wrong?

Last edited by Vanquish39 at 21:27 Mar 31, 2011.
cysun
Posts: 2935
Posted 21:34 Mar 31, 2011 |
Vanquish39 wrote:

Still doesn't seem to be working professor.

Location of servlet:  K:\CS320\Chapter4\src\Form1\ThreeParamsServlet.java

Location of html: K:\CS320\Chapter4\WebContent\ThreeParamsForm.html


This is inside the servlet

@WebServlet(name="ThreeParamsServlet", urlPatterns={"/ThreeParamsServlet", "/ThreeP"})


None of these work.

<form action = "/Form1/ThreeParamsServlet" method = "GET" >

<form action ="/ThreeParamsServlet" method = "GET">

<form action = "/ThreeP" method = "GET">

<form action = "ThreeParamsServlet" method = "GET">

 

For testing purposes, I right click the html file and I click "open with web browser".  Then I click submit.

 

What am I doing wrong?

The "ThreeParamsServlet" one should work. Most likely it's because you are using Servlet 2.5, and if that's the case, you need to do the servlet mapping in web.xml.

Vanquish39
Posts: 134
Posted 21:47 Mar 31, 2011 |

I think it's 3.0.  Is it not?

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Chapter4</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

cysun
Posts: 2935
Posted 22:10 Mar 31, 2011 |

Did you run the project? Go to the URL http://localhost:8080/Chapter4/ThreeParamsServlet and see if the servlet is running.

Last edited by cysun at 22:11 Mar 31, 2011.
Vanquish39
Posts: 134
Posted 00:40 Apr 01, 2011 |

Professor, it worked, but something is odd.  If I run it on the browser:  http://localhost:8080/Chapter4/ThreeParamsForm.html... it works fine but If I run it inside eclipse, even with the servlet running, it doesn't work.  For example:  If I rightclick the html file, click "Open with web browser", then execute it from there, it doesn't work.  The location inside eclipse is:  file://K:/CS320/Chapter4/WebContent/ThreeParamsServlet

Last edited by Vanquish39 at 00:40 Apr 01, 2011.
cysun
Posts: 2935
Posted 08:00 Apr 01, 2011 |
Vanquish39 wrote:

Professor, it worked, but something is odd.  If I run it on the browser:  http://localhost:8080/Chapter4/ThreeParamsForm.html... it works fine but If I run it inside eclipse, even with the servlet running, it doesn't work.  For example:  If I rightclick the html file, click "Open with web browser", then execute it from there, it doesn't work.  The location inside eclipse is:  file://K:/CS320/Chapter4/WebContent/ThreeParamsServlet

I'll let you figure out why it doesn't work this way. HINT: it has something to do with URL, absolute path, and relative path in a hyperlink (or in this case, the value of "action").

Vanquish39
Posts: 134
Posted 13:57 Apr 01, 2011 |

Professor I figured it out.  May I post it so other students know?

cysun
Posts: 2935
Posted 14:14 Apr 01, 2011 |
Vanquish39 wrote:

Professor I figured it out.  May I post it so other students know?

Sure.

Vanquish39
Posts: 134
Posted 15:31 Apr 01, 2011 |

To anyone that has a problem running an html file connecting to a servlet via Eclipse, type this inside the form tag.

<form action = "http://locallhost:8080/Projectname/ServletMapping"

The servlet mapping is whatever is inside the urlpatterns

@WebServlet(name="ThreeParamsServlet", urlPatterns={"/ThreeParamsServlet", "/ThreeP"}) 

This worked for me.