• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlet beginner confused

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Pat Mig
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange. A single / after the 8080 is all that is needed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic