Two Laptop Bag
The moose likes Servlets and the fly likes using a href link to pass parameters to servlet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "using a href link to pass parameters to servlet." Watch "using a href link to pass parameters to servlet." New topic

using a href link to pass parameters to servlet.

Dave Bosky
Ranch Hand

Joined: Dec 16, 2003
Posts: 72
What's the syntax for using an href link to pass parameters to a servlet?
Example: <a href="/actionController?id=1&sid=2">Menu Option</a>

It works fine if I use: <a href="/actionController">Menu Option</a>

Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Keep in mind, it is ultimately the HTTP protocol that determines how parameters get past. As such, the same syntax is used for Servlets that is used for regular HTML pages.

So the syntax you listed is correct. When using a href link, the request method will be a GET and thus Servlet�s doGet() method is called (if the parameters are past as part of a from, and the form�s method is post, the doPost() method is called).

When using the link <a href="/actionController?id=1&sid=2">Menu Option</a> is your Servlet not working? If so, what is the problem or error message you are getting. In addition, can you post the code from your Servlet in which you are accessing the Parameters.

If this is a case where you did not create the Servlet, one thing to be aware of, it is not an uncommon practice (for security purposes) for a Servlet to allow parameters only to be received via a POST. Sometimes the Servlet may even be coded such that if any parameters are received via a GET, it does not simply ignore them and function as if none were received; it may display an error.
aihadus tisidns

Joined: Nov 14, 2008
Posts: 9
This post is old but have some valuable information.
If somebody visits it please let me know if we donot want to use href GET method property ,is there some way we can use POST method in href.
Reason for my question is I want to use href links only for a application where form cannot be used due to some resons.but as in href parameters being passed are displayed in URL as soon as they are clicked it is ,i donot want it to be displayed in URL.

If the above case doesn't works in any condition then do we have any alternative to this situation where POST method should be used without using form tag......

Nitin Pathak
Ranch Hand

Joined: Sep 25, 2006
Posts: 70
You can try and emulate the HTML Form structure and submission through HTTPClient. This is to get you started.
I agree. Here's the link:
subject: using a href link to pass parameters to servlet.
It's not a secret anymore!