任何人都可以帮我解决以下代码
servlet:
下面的servlet用于静态定义一个数组。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class SampleAjax extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
{
response.setContentType("text/html");
string plociyno = "abd1234";
PrintWriter pw = response.getWriter();
if (policyno.equals("abc1234"))
{
List dataList= new ArrayList();
dataList.add("automated refund possible");
request.setAttribute("data",dataList);
RequestDispatcher dispatcher = request.getRequestDispatcher("refund.jsp");
if (dispatcher != null){
dispatcher.forward(request, response);
}
}
}
和我的 JSP: 用于在表中显示数组列表的值的 JSP。我想做同样的事情,但使用 Ajax。请帮忙。
<html
<body><table id= "table" border="0" width="303">
<tr>
<td width="250"><b>Your Policy Refund Details is:</b></td>
</tr>
<%Iterator itr; %>
<% ArrayList refund= (ArrayList)request.getAttribute("data");
if(refund != null){
for(itr=refund.iterator(); itr.hasNext();){
%>
<tr>
<td><%=itr.next()%></td>
</tr>
<%}}%>
</table>
</body>
</html>
如何使用 ajax 显示此数组列表值?请帮忙。
最佳答案
嗯,
首先创建一个函数,即。一个将显示您的数组列表值的网址。
在你的 doGet() 里面
if(request.getParameter("get").equals("array")){
//code to display array elements
}
这里使用 ajax 来使用 servlet?get=array 调用您的 servlet
$.ajax({
url: servlet?get=array,
data: data, //if you need any
success: success,
dataType: dataType // The type of data expected from the server xml? json?text?html?pdf?
});
现在您可以显示来自 AJAX 代码的文本。
如果你只想在ajax中使用jsp,
很简单,在 ajax 代码中调用整个页面。
$.get(
"http://some-remote-site",
function(data) { alert(data); },
);
我不明白你遇到了什么问题。 ;)
关于java - 如何使用ajax调用显示servlet中定义的数组列表的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9992064/