1

I have a jsp page in which I want to show 2 list to be populated using the values return by the servlet .this should happen as soon as the jsp page gets loaded so I called the servlet through the onload function of the jsp page . I am able to get the lists on the page but it went up to an infinite loop calling servlet again and again and displaying the same values.

In my test2.jsp,i am using this to call servlet on loading the jsp page

function load()
{

document.location.href="/OnaUIDemo/ona?";

}

in my ona servlet i am using this ,

RequestDispatcher rd = getServletContext().getRequestDispatcher("/test2.jsp");

what I figure out is that , because its calling the servlet through page load and also the servlet is redirecting to the same jsp. jsp is getting load again causing infinite load again and again. please help me to control the servlet call to only once.

tckmn
  • 57,719
  • 27
  • 114
  • 156
user2024234
  • 217
  • 1
  • 4
  • 8
  • Rephrase you question, clearly stating what you do, what are the problems you face and what you want to achieve, paying attention to capitalization of words in the beginning of each sentence. – skuntsel Mar 05 '13 at 14:34
  • 2
    Get rid of that `load()` nonsense and just change the URL in the link or browser's address bar to be the one of the servlet instead of the JSP. Does that now make sense? – BalusC Mar 05 '13 at 18:18
  • BalusC is right. Just call the servlet, that does the business logic and dispatch to your test2.jsp site. With Expression Language you can access the lists from your .jsp that you computed in your servlet. – Gero Apr 16 '15 at 11:37

1 Answers1

-1

Do you really need to use servlet?It can be done using POJO.

Make a class that will have that will return those two list.

public class test{

public List getList1()
{
//do your stuff here to add values in list
return list;//return your list 
}
public List getList2()
{
//do stuff here
return list;//return your list
}
}

In your jsp.Just call those two functions

<%
test t = new test();
List l1=t.getList1();
List l2=t.getList2();
//now you have the two list,just show them
%>

As scriptlets are discouraged as pointed out.You can use jstl

<jsp:useBean id="obj_name" type="package_name.class_name" />

this is equivalent to <%test t=new test();%> Now to iterate and show list values in jstl

<c:forEach var="content" items="${obj_name.list1}">
c:out value="${content}" />
</c:forEach>
ntstha
  • 1,187
  • 4
  • 23
  • 41
  • 1
    *Scriptlets* are [discouraged](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202) since a decade. – BalusC Mar 05 '13 at 18:16