tags:

views:

55

answers:

2

i want to send more than one parameter to servlet through anchor tag . but the problem is one parameter is the return value of java script method . For more Explanation ;

<% int cnt = 1;%>
<c:forEach items="${requestScope.AllUsers}" var="user">
    <tr id="<%=cnt%>">
        <td >${user.userName}</td>
        <td>${user.emailAddress}</td>
        <td><a onclick="return getUserName(<%=cnt%>)" href="servlet?">modify</a></td>      
     </tr>
    <%cnt++;%>
</c:forEach>

// here i want to use the return value returned form getUserName (java script method) and another parameter (it's name is page) and send it to servlet when click on modify hyberlink how can i make that?

+1  A: 

The return value in your onclick handler doesn't do what you expect it to do. onclick simply expects a true or false return value and cancels link navigation if you return false. Instead, you want to send the user to the URL you want directly from the onclick handler. Try something like this:

<a onclick="return redirect(<%=cnt%>)" href="#">modify</a>

<script type="text/javascript">

function redirect(cnt) {
  var user = getUserName(cnt);
  window.location = 'servlet?other_parameters_here&user=' + user;
  return false;
}

</script>
casablanca
+1  A: 

Just let JSP/EL generate the link accordingly. E.g.

<a href="servlet?page=pagename&username=${user.userName}">modify</a>

The & is there to separate multiple parameters.

Or better, use <c:url> to URL-encode the parameters appropriately to avoid invalid URL's due to URL-reserved characters in username.

<c:url value="servlet" var="servletUrl">
    <c:param name="page" value="pagename" />
    <c:param name="username" value="${user.userName}" />
</c:url>
<a href="${servletUrl}">modify</a>

Unrelated to the problem, have a look at varStatus attribute of c:forEach to replace that ugly cnt scriptlet.

<c:forEach items="${requestScope.AllUsers}" var="user" varStatus="loop">
    <tr id="${loop.index + 1}">
        ...

Note that HTML id may not start with a number. It's illegal. Rather use something like

    <tr id="row_${loop.index + 1}">
BalusC
ok that sounds clear , but how can i send another parameter to servler ? i wanna send pageName and userName !
Alaa
BalusC
Many thanx mr BalusC , promise not to say "wanna " again :)
Alaa
Alaa