mavi
mavi

Reputation: 1138

How to set variable value from javascript to JSP?

I need to draw a table dynamically depending on the data store in a database. When the first web page (validator.jsp) is loaded it goes to a dataBase and returns an ArrayList called cert. (cert hast Description, value, etc).

 <% java.util.ArrayList<Certificate> cert = OperacionesVidaDollars.getCertificates();%>

After that when the page finishes loading, a javascript function is called (function drawCertificates). This function will draw as many tables as certificates the ArrayList has.

  <script type="text/javascript">
  window.onload = drawCertificates;

        function drawCertificates(){
            alert("page finish loading, staring to draw certificates");
            var i;
            for(i=0;i<<%=cert.size()%>;i++){
                createTable(i);

                }
            }
    </script>

As you can see in the function create table, the variable text is suppost to change depending on i

text = document.createTextNode("<%=cert.get(i).getDescription()%>");

In order to update that variable i, I first call the JSP setVariable, to update the counter and then I try to use it in getDescription like:

text = document.createTextNode("<%=cert.get(request.getAttribute("count")).getDescription()%>");

I have this setVariable.jsp

    <% 

int num = Integer.valueOf(request.getParameter("number"));
request.setAttribute("count", num);
request.getRequestDispatcher("VidaDollarsCC.jsp").forward(request, response);

Cookie cookie = new Cookie("countCookie",String.valueOf(num));
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
    %>

In other JSP (validator.jsp)I have this javascript function who it supposed to change the variable value.

function setVariable(number){
alert("setting the number " + number);
    var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {

        }
        xmlhttp.open("POST", "setVariable.jsp", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send("number="+number);


}

In the same jsp (validator.jsp) I have this function to createTable(uniqID) where I need that the number is updated depending on the uniqID because I have an ArrayList which has some information that I want to be shown.

function createTable(uniqID){

    setVariable(uniqID);    
    text = document.createTextNode("<%=cert.get(request.getAttribute("count")).getDescription()%>");


    }

But is not working. Does someone knows why? How can I solve it? if you have other ideas that I can implement, that also would be great.

Upvotes: 0

Views: 455

Answers (1)

tsolakp
tsolakp

Reputation: 5948

I am assuming that your AJAX call is successfully sending number to setVariable.jsp.

1) You have to realize that AJAX call is a different request and is different then the request you have in your validator.jsp page.

2) You cant write JSP expression from Javascript and have it being resolved to HTML since your JSP needs to be reprocessed by server side.

To answer to your question on how to solve this, we need to know what you are trying to do in the first place.

Update:

1) Looks like the uniqID and count are same number. Why not just use uniqID in your javascript.

2) Why not pass certificate description into the createTable too. Like so:

    <% java.util.ArrayList<Certificate> cert = OperacionesVidaDollars.getCertificates();

            StringBuilder jsCerts = new StringBuilder("[");
            boolean first = true;
            for (Certificate cr : certs){
                if (!first) jsCerts.append(",");
                first = false;
                jsCerts.append("\"").append( cr.getDescription() ).append("\"");
            }
            jsCerts.append("]");
    %>

<script type="text/javascript">
  window.onload = drawCertificates;

        function drawCertificates(){
            alert("page finish loading, staring to draw certificates");
            var certArray = <%=jsCerts.toString()%>;
            var i;
            for(i=0;i<certArray.length;i++){
                createTable(i, certArray[i]);

                }
            }
    </script>

function createTable(uniqID, desc){

    setVariable(uniqID);    
    text = desc;


    }

The point here is that you need to write all the necessary data into the HTML to be able to use it in JavaScript, you cant access request attributes from JavaScript.

Upvotes: 1

Related Questions