2

I want to pass values of two variables when a link is clicked to another page I am using query parameter but I am only able to send one variable through it. I know about session.setAttribute() but don't know how can I use it based upon links...Foreg:

<a href="Search.jsp?item=<%=search%><%session.setAttribute("val",value1);%>" class="classname" style=" margin-top:20px;margin-left:800px;"><p> < </p></a>
            <a href="Search.jsp?item=<%=search%><%session.setAttribute("val",value);%>" class="classname1" > > </a>

This is my code I know its wrong..I just want is If I click on first link than value1 should be passed and If I click on 2nd link value should be passed.P.S.:I have already passes search variable through query parameter but now If I try to pass second parameter through session only the final value i.e second initialized value only counts? what to do?
EDIT: suppose my code is this:

 <form class="navbar-form navbar-right" action="Search.jsp" method="get">
            <input type="text" class="form-control" placeholder="Search..." name="search">

Here one variable search is passes through form How can I pass another variable value?should it be like:

<form class="navbar-form navbar-right" action="Search.jsp?item1=<%=value%>" method="get">
                <input type="text" class="form-control" placeholder="Search..." name="search">
cooljohny
  • 656
  • 5
  • 13
  • 31
  • Parameters are passed from client to server; attributes are server-side variables, scoped to a session, request or page http://stackoverflow.com/questions/16175861/servlet-parameters-vs-attributes – enlait Jun 17 '14 at 06:11
  • You also really, really should use expression language instead of scriptlets http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202 – enlait Jun 17 '14 at 06:13

5 Answers5

4

You can send multiple parameters like,

href="Search.jsp?item=<%=search%>&item2=value2&item3=value3.."

Also to add <%session.setAttribute("val",value1);%> will be executed at server side irrespective of the click of the hyperlink.

For the form you can add another input parameter in the form,

<input type="text" name="item1" value="<%=value%>">

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

You need to add some separator in between two values e.g.#

And while reading at server side you can split those values based on that separator

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
0

You may try using this example to send multiple values:

<a href="same.jsp?item=cap&item=pen&item=ball">With same name</a>
<a href="diff.jsp?gender=male&fname=Adman&lname=Smith">With diff name</a>
0

Separate the two values by using &:

<a href="Search.jsp?item=<%=search%>&item2=<%=value2%>">

On your search.jsp fetch values as :

request.getParameter("item");
request.getParameter("item2");
Java Enthusiast
  • 654
  • 7
  • 19
0

You need to call session.getAttribute in the place where you are calling session.setAttribute and session.setAttribute should be called in controller or in the jsp before the link tag to set the value. Please separate values like Search.jsp?item1=value1&item2=value2

Pranav Maniar
  • 1,545
  • 10
  • 22