简体   繁体   English

从jsp页面onload事件仅调用一次servlet页面

[英]Call a servlet page from a jsp page onload event only once

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 . 我有一个jsp页面,我想在其中显示2个列表,该列表将使用servlet返回的值进行填充。这应该在加载jsp页面后立即发生,因此我通过jsp页面的onload函数调用了servlet。 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. 我能够获取页面上的列表,但是它上升到一个无限循环,一次又一次调用servlet并显示相同的值。

In my test2.jsp,i am using this to call servlet on loading the jsp page 在我的test2.jsp中,我在加载jsp页面时使用它来调用servlet。

function load()
{

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

}

in my ona servlet i am using this , 在我的ona servlet中,我正在使用它,

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. 我发现的是,因为它通过页面加载来调用servlet,并且servlet也重定向到相同的jsp。 jsp is getting load again causing infinite load again and again. jsp再次获取负载,一次又一次导致无限负载。 please help me to control the servlet call to only once. 请帮助我将Servlet调用控制为仅一次。

Do you really need to use servlet?It can be done using POJO. 您真的需要使用servlet吗?可以使用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 在您的jsp中,只需调用这两个函数

<%
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 如所指出的那样,不鼓励使用脚本。您可以使用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 这等效于<%test t = new test();%>现在迭代并显示jstl中的列表值

<c:forEach var="content" items="${obj_name.list1}">
c:out value="${content}" />
</c:forEach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM