1

We have data being displayed in JSP like the image below , all the data is getting displayed from single Array List [![enter image description here][1]][1]

Our requirement is to select Champion Strategy & Challenger Strategy for multiple records and send same to Servlet. Please suggest a method in which we can select and pass multiple changed \ updated values from JSP to servlet.

All the data is coming from database in a single arraylist and getting displayed in JSP , now my requirement is I should be able to submit jsp only once for multiple records , like I should be able to change Champion Strategy for 2-3 records , challenger Strategy for 2-3 records and with in one submit be able to send data to servlet

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Vector"%>
<%@page import="java.util.TreeSet"%>
<%@page import="java.util.Iterator"%>

<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="com.BTParameterStrategyMaintenanceDetails"%>
<%@page import="javax.servlet.http.HttpSession" %>

<script>
    function getCListt(val) {
        alert(val);
    }
</script>



<script>
 function OpenClose(obj) {
   HeadObj = eval(obj + "_Head");
   BodyObj = eval(obj + "_Body");

   if (BodyObj.style.display=='block') {
     BodyObj.style.display='none';
     SignOffset = HeadObj.innerHTML.indexOf("-");
     HeadObj.innerHTML = "+" + HeadObj.innerHTML.substring(SignOffset+1);
   } else {
     BodyObj.style.display='block';
     SignOffset = HeadObj.innerHTML.indexOf("+");
     HeadObj.innerHTML = "-" + HeadObj.innerHTML.substring(SignOffset+1);
   }
 }
</script>

 <script type="text/javascript">
    function GetSelectedTextValue(ddlFruits) {
        var selectedText = ddlFruits.options[ddlFruits.selectedIndex].innerHTML;
        var selectedValue = ddlFruits.value;

        alert("Selected Text: " + selectedText + " Value: " + selectedValue);
    }
</script>








<%
    HttpSession session1 = request.getSession();
    ArrayList sampleStrategyRecords = (ArrayList) session1.getAttribute("allRiskCat");
    System.out.println("number of records to be displayed : " + sampleStrategyRecords.size()); 

    %>
    <form action="/getChanges" >

    <%for(int i=0;i<sampleStrategyRecords.size();i++){
    String id = "ClickMe"+i+"_Head";
    String id1 = "ClickMe"+i+"_Body";
    String onClick = "ClickMe"+i ;
    %>

    <div  id="<%= id %>" onClick="OpenClose('<%= onClick %>')" onMouseOver="this.style.cursor='pointer';" style="color: #000099;">
    <button>
    Risk Category <%= i %> 
    </button>
    </div>


        <%ArrayList ListIterator = (ArrayList) sampleStrategyRecords.get(i);
        System.out.println("ListIteartor :: "+ListIterator.size());
        System.out.println("**********");%>
        <div id="<%= id1 %>" style="display:none; margin-left:20px"><table>

<tr>
<th>Risk Category</th>
<th>Channel Description</th>
<th>Bureau Level Description</th>
<th>Champion Strategy</th>
<th>Challenger Strategy</th>
<th>S2PC</th>
</tr>
        <%
        for(int j=0;j<ListIterator.size();j++){

            BTParameterStrategyMaintenanceDetails bean = (BTParameterStrategyMaintenanceDetails)ListIterator.get(j);

            ArrayList updatedList = new ArrayList();%>

            <tr>
            <td align = "left"><%= bean.getRiskCategory() %></td>
            <td align = "left"><%= bean.getChannelDescription() %></td>
            <td align = "left"><%= bean.getBureauLevelDescription() %></td>
            <td align = "left"><select name="champDesc" id="champDesc" onchange=updatedList.add(bean) >

            <%System.out.println("updated list size :: "+ updatedList.size()); %>

                <%if (bean.getChampStrategyDesc() != null) {
                    for(int s=1;s<10;s++){

                    if(bean.getChampStrategyValue().equals(Integer.toString(s))){

                    %> 

                    <option value="<%=bean.getChampStrategyValue()%>"  selected="selected" > Champion Strategy <%=bean.getChampStrategyValue()%> </option>
                <%}else{

                    %>
                    <option value="<%= s %>">Champion Strategy <%=s %></option>
                <%} 
                }
                }%>



            </select></td>
            <td align = "left"><select name="clngrDesc" id="clngrDesc">
            <%if (bean.getChallengerDesc()!= null) {
                    for(int s=1;s<10;s++){

                    if(bean.getChallengerValue().equals(Integer.toString(s))){%> 
                    <option value="<%=bean.getChallengerValue() %>" selected="selected"> Challenger Strategy <%=bean.getChallengerValue() %> </option>
                <%}else{

                    %>
                    <option value="<%= s %>">Challenger Strategy <%=s %></option>
                <%}
                }
                }
                 %>
                 </select>
                 </td>
            <td align = "left">
            <select name="s2pc" id="s2pc">
            <%if (bean.getS2pc()!= null) {
                    for(int s=0;s<101;s++){

                    if(bean.getS2pc().equals(Integer.toString(s))){%> 
                    <option value="<%=bean.getS2pc() %>" selected="selected"> <%=bean.getS2pc() %> </option>
                <%}else{

                    %>
                    <option value="<%= s %>"><%=s %></option>
                <%}
                }
                }
                 %>
                 </select>
            </td>

            </tr>


        <%} %>
        </table></div>
 <% }
    session1.setAttribute("checkList",sampleStrategyRecords);
    ArrayList checkList = (ArrayList) session.getAttribute("checkList");
    System.out.println("checkList size :: "+checkList.size());
%>

<input class="DataEntryButton" name="submit_button" type="submit" value="Submit" />



</form>
Shally Dhar
  • 121
  • 1
  • 3
  • 6
  • Can you show a bit more, e.g. the jsp? Is the size of your array static or are you displaying it in a loop? Usually I'd say that it's a simple form you have to submit after you set your values in the jsp. – The Frozen One Sep 26 '17 at 09:49
  • All the data is coming from database in a single arraylist and getting displayed in JSP , now my requirement is I should be able to submit jsp only once for multiple records , like I should be able to change Champion Strategy for 2-3 records , challenger Strategy for 2-3 records and with in one submit be able to send data to servlet – Shally Dhar Sep 26 '17 at 09:56

3 Answers3

0

Use <jsp:param name="name" value="value"> JSP action tag for adding multiple records in a JSP file and a servlet using request.getParameter("ParamName") to get a record from a JSP file. To insert the required data into a table, use a JDBC connection.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

Form a JSON string out of your multiselected values in the jsp, send it to the server via post/ajax/form submit, process the json into a list of the records in the backend.

Sujal Mandal
  • 975
  • 1
  • 14
  • 29
0

In general I'd recommend to use an MVC Framework (e.g. Spring mvc) to seperate Java logic from the view which would make all your code more structured, read- and maintainable. If you want to keep on working on the plain HttpRequests (& responses), I think you could just define your updated list as an input param:

<form action="/getChanges" method="POST" >
....
    <input type = "hidden" name="<%= "updatedList" %>" value="<%= updatedList %>" />
    <input class="DataEntryButton" name="submit_button" type="submit" value="Submit" />
</form>

See this question

The Frozen One
  • 281
  • 2
  • 10
  • Haven't checked your code in detail but I was assuming that your `onChange` attribute is working properly. If not, you can use a scriptfunction to populate the list. – The Frozen One Sep 26 '17 at 11:06
  • how , my concern is how can i add values to a list in java script function – Shally Dhar Sep 26 '17 at 11:11