Q:

Session Setattribute is taking only last value from the loop

This is my Jsp code ,Here I get list of jobs and when I click it it should send the jobid as session to next jsp
<%
    ArrayList jobs = new ArrayList();
    jobs = master.getJobList(candidate.getEmailId());

    if (!jobs.isEmpty()) {
        for (int i = 0; i < jobs.size(); i++) {
            jobs.get(i).getJobTitle().toUpperCase().charAt(0);
%>
<div class="box inner" id="jobdetail<%=i%>">
    <span class="pull-left img-circles" id="titleChar<%=i%>"> <%=jobs.get(i).getJobTitle().toUpperCase().charAt(0)%>
    </span>
</div>

<script>
                                    $("#jobdetail<%=i%>").click(function() {
                                    <%String jobid = jobs.get(i).getJobId();
                    session.setAttribute("jobid", jobid);%>
                                        //var jobid=$("#jobid<%=i%>").val();                                                 
                                        //sessionStorage.setItem("jobid","jobid");
                                        alert("<%=jobs.get(i).getJobId()%>
    ");

                        window.location.location("jobdetail.jsp");
                    });
</script>
<%
    }
    } else {

        out.println("No Applied Jobs ");

    }
%>

In the jobdetail.jsp I have added session.getAttribute("jobid").If 5 jobs are displayed when I click one of the jobs  it returns only the job id of the last one  ,I added an alert box to check and the alert box returns the correct job id but the session attribute always stores the jobid of last one and I am not able to get other jobid's how can i sort this problem

A:

you try this code. it stores list of objects, then you can put it in session attribute. get it back.
HttpSession session = request.getSession();
List list = new ArrayList();
list.add(jobid);
list.add(jobid);
session.setAttribute("listObject", list);

