0
package com.model;

public class Items {
    private int item_id;
    private int product_id;
    private int size_id;
    public Items(int item_id, int product_id, int size_id){
        this.item_id = item_id;
        this.product_id = product_id;
        this.size_id = size_id;
    }

    public int getItem_id() {
        return item_id;
    }
    public void setItem_id(int item_id) {
        this.item_id = item_id;
    }
    public int getProduct_id() {
        return product_id;
    }
    public void setProduct_id(int product_id) {
        this.product_id = product_id;
    }
    public int getSize_id() {
        return size_id;
    }
    public void setSize_id(int size_id) {
        this.size_id = size_id;
    }

}


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="com.model.*"%>

<%!Set<Items> item = new HashSet<Items>();
%> 
<%
item.add(new Items(1,1,1));
item.add(new Items(2,1,1));
item.add(new Items(3,1,1));
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%for(Items items : item){ %>
    <input type="text" id="settleAmt" name="settleAmt" size="10" value="<%=items.getItem_id()%>">
    <% } %>
</body>
</html>

I have this code to create textbox based on the Set. And it works because it displays 3 textbox. But the problem is each time I reload the page, it adds 3 new textBox

Karlx Swanovski
  • 2,869
  • 9
  • 34
  • 67

1 Answers1

1

Any reason why you are using declaration block? <%!Set<Items> item = new HashSet<Items>();%>. The problem is that the code within declaration block does not reset with new request.

You probably need to add Set<Items> item = new HashSet<Items>(); into your scriptlet (<% %>) block, to reset the variable with every new request.

Serhiy
  • 4,073
  • 3
  • 36
  • 66