File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes Can I call a servlet directly from HTML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Can I call a servlet directly from HTML" Watch "Can I call a servlet directly from HTML" New topic
Author

Can I call a servlet directly from HTML

Patrick Lim
Greenhorn

Joined: Jun 09, 2005
Posts: 2
I'm using Tomcat 5.5.4. I'm trying to call a servlet that retrieves data from the database. I want to set up a hotlink(hyperlink) on the main web page but can't get it to call the servlet. I've set up the servlet entry in the web.xml file and it works if I open a form with a submit button. I just want a hotlink though to get the data. Does anyone know if this is possible?
Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Does your servlet override doGet?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Patrick yes, you can definately call your servlet directly from HTML infact that depends on which Method of the Form you call as there are 2 method types POST / GET.

To call a servlet from an HTML you can put the servlet name in the Form Action and depending on the type of get or put doGet or doPut is called. But yeah name defined in the form action should exist in web.xml

Cheers,
Rahul


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
Patrick Lim
Greenhorn

Joined: Jun 09, 2005
Posts: 2
Hi,
Thanks to everyone that replied! I have overridden doget with dopost but now that I think about it I don't need to override it for a select from the database. So I will change that and maybe it will work.
I don't want to use a form. I want to do a hotlink that calls the servlet directly. I can call the servlet directly if I use a form with a button so I know the web.xml file is set up correctly and the files are in the correct directories. I just can't seem to get it to work calling the servlet without a form. Any example code would be great!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Patrick Lim:
Hi,
I have overridden doget with dopost


I'm not sure what that means.
Also doPost and doGet are case sensitive.

In order for your servlet to respond to a form posting, you have to override the doPost method. To hit it directly, you have to override the doGet method.

If you want the same code to respond to both types of requests, just call one from the other.


Example:
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
I would take the form that works and change the method to "GET" - then carefully examine the URL that this constructs. That is the way your link will have to be formulated.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I call a servlet directly from HTML
 
Similar Threads
Starting process threads when Tomcat starts
Servlets
Dynamic forms and pages
How send file data to JSP/Servlet page and save to disk?
store browser status code in Servlets