Link to the correct item page by selecting the list of items in for loop

141 views Asked by At

This is my jsp page that retrieve the list of items from database using for loop

<%
    itemManager mgr = new itemManager();
    Item[] items = mgr.getAllItems();
    for(int i = 0; i < items.length; i++){
    %>
    <tr>
    <td> <img border="0" src="<%=items[i].getItemImage() %>" width="100" height="100">
                </td>
                <td>
                <%=items[i].getItemName()%>
                <input type="text" name="itemID" value="<%=items[i].getItemID()%>">
                        <br/>
                <%=items[i].getItemDesc()%>
                <br/>
                Start Bid : <%=items[i].getStartBid()%>
                <br/>
                Buy it now : <%=items[i].getEndBid()%>
                <br/>
                Bidding close on : <%=items[i].getDuration()%>
                <br/>
                <input type="submit" value="View"> 
    <%
    }
    %></table>

This is the jsp page that link to the item you selected previously

<table border="1" align="center">
<%
itemManager mgr = new itemManager();
Item items = mgr.getItem((Integer)session.getAttribute("ITEM_DATA"));
%>
<tr>
                <td> <b> <%=items.getItemName() %></b> </td>
</tr>

</table>

This is the servlet to store the session of the selected item id and forward to the correct item jsp page.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        int id = Integer.parseInt(request.getParameter("itemID"));
        session.setAttribute("ITEM_DATA",id);
        RequestDispatcher rd = request.getRequestDispatcher("viewItem.jsp");
        rd.forward(request, response);
    }

However, after I clicked the view button. It keeps linking to the itemID = 1.

The URL dispalys "/ItemServlet?itemID=1&itemID=2" .

In fact, if I click on itemID=2 the URL should display like this:

"/ItemServlet?itemID=2"

As a result, how can I achieve this? Thanks in advance!

2

There are 2 answers

0
Sathesh S On BEST ANSWER

The problem in your code is you are using a single form and dynamically creating Input box inside the form. So all the input box will be having same name. That's why when you submit the form all the input box values are sent as request parameters. I just reduced some part in your code for better under standing. Take this as your code

<form action="item" method="get">
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>

<tr>
     <td>
            <%=items[i].getItemName()%>
            <input type="text" name="itemID" value="<%=items[i].getItemId()%>">
            <input type="submit" value="View"> </td></tr>

<%
}
%></table>
</form>  

When you run this code and if you check the rendered HTML code it will be look

<form action="item" method="get">
<table>
<tr><td>
            aaa
            <input type="text" name="itemID" value="1">
            <input type="submit" value="View"> </td></tr>



<tr>
     <td>
            bbb
            <input type="text" name="itemID" value="2">
            <input type="submit" value="View"> </td></tr>
<tr><td>
            ccc
            <input type="text" name="itemID" value="3">
            <input type="submit" value="View"> </td></tr>    
</table>
</form>

Here All the Input box having same name as "itemID". As a solution you can create the form inside the for loop, so that while submitting only one Input box value will be sent as request.

Create form inside your for loop like below code.

<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<form action="item" method="get">
<tr>
     <td>
            <%=items[i].getItemName()%>
            <input type="text" name="itemID" value="<%=items[i].getItemId()%>">
            <input type="submit" value="View"> </td></tr>
            </form>
<%
}
%></table>  

Hope This will help you.

0
shadab On

change the name of text field dynamically.
And use getQueryString() in servlet to find the itemId name and value.
by using EL.
I hope this will help you