Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I call a servlet directly from HTML

 
Patrick Lim
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your servlet override doGet?
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Patrick Lim
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic