开发者

send more than one parameter to servlet through anchor tag

i want to send more than one parameter to servlet through anchor tag . but the problem is one parameter is the return v开发者_如何学Pythonalue 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?


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>


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}">


page1:

[a href="NewServlet?id=name&password=1234"/]

page2:

String a=request.getParameter("id");
String b=request.getParameter("password");

output:

a=name
b=1234

by using & symbol we can add n parameters...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜