1

I am using JSTL to display the record in JSP that I search from database.

For example, these records returned from database are :

<tr>
    <td class="leavehistory">2012-01-31 18:16:36.0</td><td class="leavehistory">Present</td>
</tr>

<tr>
    <td class="leavehistory">2012-01-31 18:16:38.0</td><td class="leavehistory">Present</td>
</tr>

Here is my JSP :

<c:forEach items="${attendRecord}" var="attendRecord">
   <tr>
       <td class="leavehistory">${attendRecord.attendDt}</td><td class="leavehistory">${attendRecord.status}</td>
   </tr>
</c:forEach>

Here is my Servlet :

ArrayList<Attendance>  arrSub = new ArrayList<Attendance>();

while (rs.next()){
    Attendance att = new Attendance();
    att.setAttendDt(rs.getString(1));
    att.setStatus(rs.getString(2));
    att.setClsType(rs.getString(3));
    att.setSubID(rs.getString(4));
    arrSub.add(att);
}

request.getSession().setAttribute("attendRecord", arrSub);

Where I perform another search , the result will be like this :

<tr>
    <td class="leavehistory">2012-01-31 18:16:36.0</td><td class="leavehistory">Present</td>
</tr>

<tr>
    <td class="leavehistory">2012-01-31 18:16:38.0</td><td class="leavehistory">Present</td>
</tr>

<tr>
    <td class="leavehistory">2012-01-31 18:16:36.0</td><td class="leavehistory">Late</td>
</tr>

<tr>
    <td class="leavehistory">2012-01-31 18:16:38.0</td><td class="leavehistory">Late</td>
</tr>

Here is my code that used to call my servlet :

<script>

        function reviewAtt(){
            var f = document.review;

            f.hdSearch.value = f.cardID.value +";"+document.getElementById("clsType").value+";"+document.getElementById("sub").value;
            f.method = "POST";
            f.target = "_self";
            f.action = "../build/web/WEB-INF/classes/leave/searchAttendServlet";
            f.submit();
        }

    </script>

Then I continue to perform another search action , the latest search result supposed to replace the old record and display the latest result. But It will not, the latest result will be added at the below of old record. I want to display the latest search result only. The old search result will be eliminated.

Is there any solution to solve this problem ?

crchin
  • 9,473
  • 6
  • 22
  • 28

1 Answers1

2

This problem is not caused by JSP/JSTL. It's caused by a mistake somewhere in your code. However the code posted so far looks fine. I'd bet that you just oversimplified the code which magically made it to look like fine.

At least, the symptoms indicate that you're storing request scoped values as instance variable of the servlet class which made it to be threadunsafe (i.e. it's shared among all requests in all sessions). Something like this:

public class SearchAttendServlet extends HttpServlet {

    ArrayList<Attendance> arrSub = new ArrayList<Attendance>();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // ...
    }

}

This is not right.

Also storing it as a session attribute is not right, but this is not necessarily the cause of your problem (unless you explicitly retrieved the very same list from the session before adding new items to it, but the code posted so far doesn't indicate this in any way).

You should be creating the list in the thread local scope and storing it in the request scope.

public class SearchAttendServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<Attendance> arrSub = new ArrayList<Attendance>();

        // ...

        request.setAttribute("attendRecord", arrSub);
        request.getRequestDispatcher("/WEB-INF/yourpage.jsp").forward(request, response);
    }

}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555