You can't use jstl inside scriptlet..
Save the value of ${result.fname}
using <c:set>
in page scope. And use the variable inside the scriptlet.
e.g.
<c:forEach items="${query.rows}" var="result">
<c:set var="lname" value="${result.lname}" />
<c:set var="fname" value="${result.fname}" />
<%
ArrayList l= new ArrayList();
l.add((String)pageContext.getAttribute("fname"));
l.add((String)pageContext.getAttribute("lname"));
for(int i=0; i<l.size(); i++)
{
out.println(l.get(i));
}
%>
</c:forEach>
For more information, http://docs.oracle.com/javaee/1.4/tutorial/doc/JSTL4.html
http://javapapers.com/jsp/jsp-life-cycle-explain/
http://docs.oracle.com/cd/E13222_01/wls/docs81/taglib/handler.html
UPDATE
EL attributes are stored in the scope - page, request, session, application
The <c:set>
tag example doesn't specify a scope, so therefore you could get it like this:
<c:set var="fname" value="${result.fname}" />
<%
String fname = (String)pageContext.getAttribute("fname");
System.out.println(fname);
%>
alternatively exploit the feature of the useBean
tag that creates a scriptlet variable:
<c:set var="fname" value="${result.fname}" />
<jsp:useBean id="fname" type="java.lang.String"/>
<%
System.out.println(fname);
%>
Note that the EL variable and the Scriptlet variable are initially pointing at the same String.
But changing the string in scriptlet code will change the value it is pointing at, while leaving the EL variable untouched.