This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Calling a servlet via a link how do I pass a parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Calling a servlet via a link how do I pass a parameter" Watch "Calling a servlet via a link how do I pass a parameter" New topic
Author

Calling a servlet via a link how do I pass a parameter

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 573
I Have a JSP page with the following links
<TD><a href="vecconsulting.data">select all employees</a></TD>
<TD><a href="vecconsulting.data">select all Managers</a></TD>
<TD><a href="vecconsulting.data">select all Customers</a></TD>
selecting any of these links will take me to the data servlet.
What I want to do is pass a integer to the servlet so that the servlet knows which link was selected.
I understand there are many ways using
request.setAttribute("dataID","1"),
the session etc
But I am not sure how to include the parameter within the link so that it can be picked up by the data servlet.
Thanks for any help.
Tony
Faisal Khan
Ranch Hand

Joined: Jun 29, 2003
Posts: 285
Originally posted by Tony Evans:
I Have a JSP page with the following links
<TD><a href="vecconsulting.data">select all employees</a></TD>
<TD><a href="vecconsulting.data">select all Managers</a></TD>
<TD><a href="vecconsulting.data">select all Customers</a></TD>
selecting any of these links will take me to the data servlet.
What I want to do is pass a integer to the servlet so that the servlet knows which link was selected.
I understand there are many ways using
request.setAttribute("dataID","1"),
the session etc
But I am not sure how to include the parameter within the link so that it can be picked up by the data servlet.
Thanks for any help.
Tony

Tony,
You can simply append the parameter to the URL, like:
<TD><a href="vecconsulting.data?dataID=1">select all employees</a></TD>
<TD><a href="vecconsulting.data?dataID=2">select all Managers</a></TD>
<TD><a href="vecconsulting.data?dataID=3">select all Customers</a></TD>
Like you said there are a number of design concerns that do come into play.
I hope this helps.
Faisal


The secret to creativity is knowing how to hide your sources.
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 573
Hi Faisal,
Thanks for your help, but I wanted to pass the dataInfo in the session.
I have managed to do this.
In case anyone else has this problem, this is my link.
<a href="vecconsulting.data">select all employees
<%session.setAttribute("dataID","1");%></a>
When this link is selected 1 is added to the session and can be picked up by the servlet.
Thanks again Tony
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Using the session for this is a bit heavy-handed and a poor choice. By doing so your data is logically disassociated with the request it is augmenting, it incurs some extra overhead, and the lifetime of the data lasts long beyond its required scope. Using a request parameter (such as Faisal wisely advised) is straight-forward, logically associated with the request, and limits the lifetime of the data to it required usage.
A rough (very rough) analogy I just concocted: I take Thermos of home-brewed coffee to work with me every day. The most straight-foward means of accomplishing this task is to just carry it with me in my vehicle. I could also give the Thermos to FedEx to deliver it my office and pick it up when I get there. It works, but isn't the most optimal mechanism for accomplishing the task.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Originally posted by Tony Evans:
When this link is selected 1 is added to the session and can be picked up by the servlet.

Actually, that piece of code will get executed on the server during the process of serving up the page and not when the link is clicked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a servlet via a link how do I pass a parameter
 
Similar Threads
Passing parameter to servlet from another servlet
dynamic parameters
jquery accordion is causing jquery treeview picker to breakup
Newbie Question---Help?
Building tables in XSL