1

Can anyone please help me with below code

servlet:

below servlet is for statically defining an array.

    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);
        }
        }

        }

and my JSP: JSP for displaying the values of the arraylist in a table. I want to do the same thing but using Ajax. Please help.

<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>

how can I display this arraylist values using ajax? Please help.

1

2 Answers 2

1

well,

first create a function ie. an url which will display your arraylist value.
inside your doGet()

if(request.getParameter("get").equals("array")){
//code to display array elements
}

here the ajax is made to call your servlet with servlet?get=array

$.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? 
});

Now you can display the text from AJAX code.
If you only want jsp inside ajax,
it's quite simple, call the entire page in your ajax code.

$.get(
"http://some-remote-site",
function(data) { alert(data); },
);

I don't see what problem you got here. ;)

Sign up to request clarification or add additional context in comments.

Comments

1

It is possible by simply adding

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

And then

<c:forEach var="row" items="${data}" varStatus="i">
<tr>
<td><c:out value="${row[0]}" /></td>
<td><c:out value="${row[1]}" /></td>
...
</tr>
</c:forEach>

By this way you can display the ArrayList data easily.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.