V
V
Viktoria Smirnova2018-05-23 15:05:45
JavaScript
Viktoria Smirnova, 2018-05-23 15:05:45

Can't get list from server?

Can't get ArrayList from server.

Server:

public class Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String val = request.getParameter("val");
        Double num = Double.parseDouble(val);
            if (num > 0 && num <9 ){
            List<String> listStrings = new ArrayList<String>();
            listStrings.add("One");
            listStrings.add("Two");
            listStrings.add("Three");
            request.setAttribute("listStrings",listStrings);
       } else {
            }
        request.getRequestDispatcher("index.jsp").forward(request,response);


On the client:
...
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                myFunction(this);
            }
        };
        xhttp.open("POST", 'Servlet', true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.send("val="+ val);

        function myFunction(???) {
            ???
            document.getElementById("demo").innerHTML = ???;
        }
...


I tried it like this:

$(document).on("click", "#somebutton", function() {            
    $.get("someservlet", function(responseXml) {              
        $("#somediv").html($(responseXml).find("data").html());     });});


but I need it in the response from the server and the main thing is that the table rows are in jstl, the table options in JS do not look like:

<table>
      <c:forEach items="${listStrings}" var="product">
        <tr>
          <td>${product}</td>
          <td><c:out value="${product}" /></td>
          </tr>
      </c:forEach>
    </table>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question