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.
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?
<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")
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
@WebServlet(name="ThreeParamsServlet", urlPatterns={"/ThreeParamsServlet", "/ThreeP"})
<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 |
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"?> |
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 |
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 |
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. |