• 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

Calling a servlet from an HTML page

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question will probably make the more experienced users of servlets laugh due it's simplicity/stupidity!

If my form action in an HTML page is :

<FORM ACTION="/servlet/HelloYall">

and my servlet is in the directory:

apache_install_dir\Tomcat 5.5\webapps\ROOT\WEB-INF\classes

Where should I run my HTML from, for the path to the servlet to be valid?

Thanks in advance!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You map the path to the servlet in the web.xml and use that. It is independent of the location of the HTML page.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,

Any question asked here at Ranch isnt a stupid question. We are all learners here and its always a pleasure helping.

now , to the main point ,

in web.xml file , you have an element called <servlet> which has child elements called <servlet-name> and <servlet-class> , You can give any name you want to the <servlet-name> , however you need to give fully qualified classname for the <servlet-class> element , If your servlet resides inside WEB-INF/classes folder , then simply the name of the java class would suffice and as Bear told , once this entry is done you need to map your java class to a URL Pattern which will be accessed from your HTML page.

For URL mapping also , there is a tag called <servlet-mapping> which further has to 2 child elements called <servlet-name> and <url-pattern>

So , If your servlet name is TestHelloWorld, then you need to have these entries in your web.xml



So now from your HTML page , if you have an form action like below then it would hit your Servlet and execute the logic written inside this.

<form action="/hello/abc" method="post">
<input type="Submit" value="Get Value from My First Servlet" />
</form>

Hope this helps.
Yogendra Joshi.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic