0

I am turnig my jsp code:

<%

    for (int i = 0; i < list.size(); i++) {
        Entry var = (Entry) list.get(i);
        out.println(var.getCn().replace("cn=", ""));
        String cn = var.getCn();
        out.println("<form method=\"get\" action=\"Controller\">"
                + "<input type =\"hidden\" name=\"act1\" value = \"" + cn.replace("cn=", "") + "\">"
                + "<button type=\"submit\" name=\"act\"  value=\"SHOW\" id=\"act\" >Show</button>"
    }

into JSTL:

  <c:forEach var="item" items="${list}">
    <p>
        <c:out value="${item.getCn()} "></c:out>
        <p>
        <c:set var="item" value="${item.getCn()}"></c:set>
          <input type ="hidden" name="act1" value ="??????????????">
        <button type="submit" name="act"  value="SHOW" id="act">Show</button>
</c:forEach>

I don't know how to pass the parameter "value", easily uploadable through cn.replace("cn=", "") with jsp, to my servlet.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
MdC
  • 107
  • 1
  • 4
  • 16

2 Answers2

1

First remove the below line:

<c:set var="item" value="${item.getCn()}"></c:set>

It's overriding the <c:forEach var="item">.

As to the concrete question, you can just use singlequotes to represent a string in EL.

<input ... value="${item.cn.replace('cn=', '')}">

Do note that on javabean properties you don't necessarily need to specify the whole method name as in ${item.getCn()}. You can just use ${item.cn}.

See also:


Unrelated to the concrete problem, also note that this is still prone to a XSS attack hole if it concerns user-controlled input, but your original scriptlet approach was too.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

I believe you can just use the variable item the same way you used cn on your scriplet. Try something like this:

value ="${item.replace('cn=', '')}"

I'm not sure if you'll have problems with two variables named item in your JSP, you might want to change the name of one of them just in case. This would be the whole code:

<c:forEach var="item" items="${list}">
    <p>
        <c:out value="${item.getCn()} "></c:out>
        <p>
        <c:set var="cn" value="${item.getCn()}"></c:set>
          <input type ="hidden" name="act1" value ="${cn.replace('cn=', '')}">
        <button type="submit" name="act"  value="SHOW" id="act">Show</button>
Fred Porciúncula
  • 8,533
  • 3
  • 40
  • 57