curiousity
curiousity

Reputation: 4741

set Attribute via href

I am trying to set a variable (named as "o") in jsp in the body of tag - how could I do it without scriplets? I have wrote this piece of code but it is not working:

<a class="overfl" href="myServlet?action=request.setAttribute('o',i)"> ${values[i]} </a>

Upvotes: 0

Views: 1982

Answers (2)

Braj
Braj

Reputation: 46861

Try with JSTL Core c:set Tag to set the attribute in any scope.

Sample code:

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

 <c:set var="salary" scope="request" value="${2000*2}"/>

ServletRequest#setAttribute() method doesn't return any value.

Get the value back in the same way as you are doing here ${values[i]} or try with JSTL Core c:out Tag to get the value back.


In your case simply pass the action values as query parameter as shown below:

<a class="overfl" href="myServlet?action=${i}"> ${values[i]} </a>

And get the value back at server side using

String action = servletRequest.getParameter("action");

Upvotes: 2

Mifmif
Mifmif

Reputation: 3190

If the varaible is not already defined in your request's attribute so call <%request.setAttribute('o',i); %> then if you want to write it to the jsp output you have to to write <%request.getAttribute('o') %> in the place where you want to add it's value like this :

<%request.setAttribute('o',i); %>
<a class="overfl" href="myServlet?action=<%=request.getAttribute('o') %>"> ${values[i]} </a>

Upvotes: 0

Related Questions