It's not a secret anymore!
The moose likes Servlets and the fly likes Calling a servlet from an HTML page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling a servlet from an HTML page" Watch "Calling a servlet from an HTML page" New topic

Calling a servlet from an HTML page

And Green

Joined: Sep 28, 2007
Posts: 26
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

Joined: Jan 10, 2002
Posts: 62127

You map the path to the servlet in the web.xml and use that. It is independent of the location of the HTML page.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Yogendra Joshi
Ranch Hand

Joined: Apr 04, 2006
Posts: 213
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" />

Hope this helps.
Yogendra Joshi.

Meri Zindagi Hain Tab Tak.. Jab Tak Tera Sahara.... Har Taraf Tu Hi Tu Hain SAI Tera Hi Hain Nazara.....
Consider Paul's rocket mass heater.
subject: Calling a servlet from an HTML page