简体   繁体   中英

How to include <HTML> inside <%%> in JSP?

I am new to jsp and am stuck at building a dropdown using a list variable. For static content, I used to following code:

<form method="post" action="Index.jsp">
  <select name="item">
    <option selected="selected"> -- Please choose --</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
  </select>
  <input type="submit" value="Submit">
</form>

This worked well. Now to populate the list using a List variable 'list' I searched for any solution but in vain. After much search I couldn't find how to populate the dropdown list, after which I tried the following which did not work as well:

    <form method="post" action="Index.jsp">
      <select name="item" path="list">
        <%for(String element : list){
              PrintWriter writer = response.getWriter();
              writer.println("<option value="xyz">"+element+"</option>");
          }
        %>
      </select>
      <input type="submit" value="Submit">
    </form>

Is there a way I can accomplish this? Thanks in advance.

Use jsp expression tag <%="string"%>

   <%for(String element : list){%>

         <option value="<%=element%>"><%=element%></option>
   <%}%>

Check

Try this

    <select name="item" path="list">
    <%for(String element : list){
          %>
               <option value="<%=element %>"><%=element %></option>
           <%
      }
    %>
  </select>

Here you can see the value of 'element' in SELECT and get the same in the click

<%=bla%> this is the Expression tag in JSP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM