Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a servlet from an HTML page

 
And Green
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64847
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Yogendra Joshi
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic