Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# servlet beginner confused

Patricia Miguel
Greenhorn

Joined: Feb 26, 2012
Posts: 22
I need to call a servlet from an HTML file, but I can't get my servlet to work outside the Eclipse IDE. Something must be wrong with my paths, but I don't know what.
I'm using Tomcat 7.0.

This is my servlet class:

I also have this web.xml file:

And my HTML file has a simple form like this:

Then, in my C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps folder I created a "test" folder where I placed my index.html file and a WEB-INF folder with the web.xml file and a sub-folder called "classes", where I placed HelloServlet.java and HelloServlet.class.
I also set environment variables in my windows 7 system: CATALINA_HOME with value C:\Program Files\Apache Software Foundation\Tomcat 7.0 and CLASSPATH with value %CLASSPATH%;%CATALINA_HOME%\common\lib\servlet-api.jar;.

With all of this, I start tomcat, open index.html and click on the submit button. All I get is a "Firefox can't find the file at /C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/test/HelloServlet" error message.
What kind of newbie mistake am I making?

Thanks!!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670

18

You do open the index.html file using http://localhost:8080/test/index.html right? Because if you just double click on the index.html file, Firefox (and any other browser) will not open it through Tomcat but from the file system itself.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

It looks like you opened the HTML file directly from within Tomcat's webapps directory - This is not the way to run a web page. Instead, you should use URL, for example: http://localhost/test/index.html

PM Me If You Want to get Urgent Help on Java Programming
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997

65

The form action should use a server-relative URL for the servlet -- that is, beginning with the context path. The page-relative URL you are currently using is either guaranteed to fail now, or at some point in the future.

Also, using all uppercase for HTML went out in 1998. Use lowercase.

Patricia Miguel
Greenhorn

Joined: Feb 26, 2012
Posts: 22
Thanks a lot for your help
I was trying to open the index.html file directly with a double-click, which showed me the form with the button that should call the servlet. Pressing it throws the error message I mentioned in my previous message.
But when I try http://localhost:8080/test/index.html it doesn't even show me the page. It gives me a 404 tomcat error message. I tried http://localhost:8080/index.html just in case, but didn't work either.

Bear Bibeault wrote:The form action should use a servlet-relative UTL for the servlet -- that is, beginning with the context path. The page-relative URL you are currently using is either guaranteed to fail now, or at some point in the future.

I'm not sure how to form that servlet-relative URL. Should I include the package name? I tried with <FORM METHOD=GET ACTION="myapp\HelloServlet"> and didn't work either. But, of course, that's when I open the index.html file by double clicking on it, since http://localhost:8080/test/index.html isn't working for me.

Bear Bibeault wrote:
Also, using all uppercase for HTML went out in 1998. Use lowercase.

LOL. That is when I first learned HTML. I try to write as I learned it at university, but sometimes I just forget :P
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997

65

Patricia Miguel
Greenhorn

Joined: Feb 26, 2012
Posts: 22
Bear Bibeault wrote:

Great. Thank you

Btw, I got the index.html page to show AND the servlet call to work! The problem was that I was using a single "/" after the port in http://localhost:8080//test/index.html. A beginner's fault!
Thank you all
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670

18

Strange. A single / after the 8080 is all that is needed.

I agree. Here's the link: http://aspose.com/file-tools

subject: servlet beginner confused