0

I am developing a web application in which I which I have a JavaScript array and I want this array in my JSP page and iterate it save the data in database.

var value = response;
  for (var key in value) {
      if (value.hasOwnProperty(key)) {
        alert(key + " -> " + value[key]);
        var sample = new Array();
        sample=value[key];
        console.log(sample);
        // document.getElementById('');
      }
    }
});

I want this data on my JSP page is it possible. If it is possible, then how?

3
  • where you want to show this array in jsp?? Commented Apr 11, 2015 at 12:02
  • Is your js on the same jsp? Commented Apr 11, 2015 at 12:36
  • Use AJAX to send the data. Commented Apr 12, 2015 at 1:15

4 Answers 4

1

You can have javascript in jsp but that will always execute on client side. So if you want to save some stuff on trigger of some event inside browser you can do that by making ajax call or form submission. Ajax is preferred way because its faster and better experience for user

But if you have intention of execution of javascript on server side thats not possible

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

Comments

0

Java script client side script whereas jsp is server side script so you can't simply do this.

What you can do is submit the calculated variable from javascript to server by form-submission, or using URL parameter or using AJAX calls and then you can make it available on server to store in database.

Client Side:

<script type="text/javascript">
var n = document.getElementById("data");
var f=new Array( "apple", "orange", "mango" );
n.value = f;
</script>
<form action="Your_JSP.jsp" method="POST">
<input type="hidden" id="data" name="data" value="" />
<input type="submit" />
</form>

Server Side:

<%
String val = request.getParameter("data");
String aa[]=val.split(",");
for(String x : aa )
out.println(x);
//now hereyou can use aa array tostore in database or do whatever you want  
%>

2 Comments

i don't think he is asking the same.
he need to create some array in JavaScript and want to get in jsp then he want to save the same in DB.
0

Try this if you have two different jsp:

first.jsp

<script>
var response = [];
...
var put = function(form) {
  form.resp.value = response.join(','); //using comma as separator
};
</script>
<form onsubmit='put(this)' method='next.jsp' method='post'>
  <input type='hidden' name='resp' />
  <button>Submit</button>
</form>

next.jsp

<%
    String[] resp = request.getParameter("resp").split(",");
%>
Response is: ${param.resp} <!-- debugging -->

Comments

0

yes , its possible to do that. you can show array in HTML tag

<!DOCTYPE html>
<html>
 <head>
<script>
var value=response;
 for (var key in value) {
  if (value.hasOwnProperty(key)) {
    alert(key + " -> " + value[key]);
    var sample = new Array();
    sample=value[key];
    console.log(sample);
   // do here as 
    document.getElementById("array").innerHTML = sample;
     // here array is <p> tag in html and sample is array which will be shown in jsp page.
  }
}
</script> 
</head>
<body>
  <form action="get.jsp" method="POST">
     <p id="array" name="array" type="hidden"></p>
   <input type="submit" />
</body>
</html>

in get.jsp you will able to get value from array as:

 <%
String []array = request.getParameter("array").split(",");
//this String array you can use to store data in DB  
%>

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.