Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tony Evans
Ranch Hand
Posts: 597
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Evans
Ranch Hand
Posts: 597
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64194
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic