You can create on e ArrayList of empname
ArrayList<String> empList = new ArrayList<String>();
while (rs2.next())
{
String emp= rs2.getString("empname");
empList.add(emp);
}
Then you can use JSTL isLast()
<c:forEach items="${empList}" var="empName" varStatus="loop">
<c:out value="${empName}" />
<c:if test="${!loop.last}">,</c:if>
</c:forEach>
You can use isLast() method of ResultSet
but,
I would recommend you not to use scriptlets in JSP
See how to avoid Java Code in JSP-Files?
For working with JSTL you just need to put jstl-1.2.jar in /WEB-INF/lib
and in JSP
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Update answer to comment
it does not printing result.
Scripts are raw java embedded in the page code, and if you declare variables in your scripts, then they become local variables embedded in the page.
In contrast, JSTL works entirely with scoped attributes, either at page, request or session scope.
So, for using ArrayList empList
created in Scriptlets you need to modify the code. see this answer
<%
ArrayList<String> empList = new ArrayList<String>();
while (rs2.next())
{
String emp= rs2.getString("empname");
empList.add(emp);
}
pageContext.setAttribute("empList", empList);//pageContext is implicit object available
%>
then above mentioned JSTL code will work fine.
Related links