java ee - Importing and retrieving a class object in a jsp -
i trying retrieve array list object of type queryclass
servlet have made , import class of queryclass
can use object in jsp called validate.jsp
" object seems not exist when attribute in jsp file, though in servlet initialized appropriate data , set right name.
in servlet have snippet
queryclass query = new queryclass("","","",""); string searchname = request.getparameter("searchname"); arraylist<queryclass> data = query.getsearchednames(searchname); request.setattribute("data",data); requestdispatcher rd = request.getrequestdispatcher("validate.jsp"); rd.forward(request, response);
in jsp have following
<%@page import="src.main.java.queryclass"%> <% if(request.getattribute("data")!=null) { arraylist<queryclass> value = (arraylist<queryclass>)request.getattribute("data"); } %>
your requirements fulfilled keeping in mind mvc approach made scriptlets obsolete , deprecated.
set data want request attribute in servlet method:
list<queryclass> data = createlist(...); request.setattribute("data",data); request.getrequestdispatcher("validate.jsp").forward(request, response);
access different properties of request (session, application, etc.) via el:
${data}
so, keep in mind desire traverse list, traversal have following style in jsp if used jstl:
<ul> <c:foreach var="element" items="${data}"> <li>${element.name}</li> </c:foreach> </ul>
the code above generate list of names of each element data object, provided class has getname()
method defined.