0

In my application i have dynamic data in JavaScript function i need to send that dynamic data through a variable to another jsp page how to solve this ?

1
  • You need to push the 'dynamic data' back to the server, where the JSP is run Commented Mar 25, 2014 at 11:22

4 Answers 4

1

These two pages will do what you want.

First the source page where the variable is set:

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

<!DOCTYPE html>

<html>

  <head>

    <script >

      function next()
      {
        window.location.href = '<c:url value="/next.jsp"/>?dymanicValue=' + document.getElementById('dynamicValue').value        
      }

    </script>

  </head>

  <body>

    <label for="dynamicValue">Dynamic Value</label>
    <input id="dynamicValue" type="text"/>

    <p/>

    <a href="javascript:next()">Next</a>

  </body>

</html>

This page will display a text input, the value set in that input will be added as a parameter to the URL used to navigate to the next page when the 'Next' link is clicked.

The next.jsp can then read the value from the request like this:

<!DOCTYPE html>

<html>

  <body>

    The dynamic value is: ${pageContext.request.dynamicValue}

  </body>

</html>
Sign up to request clarification or add additional context in comments.

Comments

0

What I'd like to do is using hidden input tags and passing them through a post to the next page using PHP. Say I want to send A to another page.

<!DOCTYPE html>
<html>
   <body>
      <form action='nextPage.php' method='post'>
          <input id='varInput' type='text' style='display:none;' name='var'/>
          <input type='submit' value='send'/>
      </form>
   </body>
   <script>
       var A = 3;
       $("#varInput").val(A);
   </script>
</html>

nextPage.php

 <?php
    $var = $_POST['var'];
 ?>
 <!DOCTYPE html>
 <html>
   <body>
      <div id='value'></div>
   </body>
   <script>
      var A = parseInt(<?php echo $var; ?>);

      $("#value").append("<p>"+A+"</p>");
   </script>
</html> 

1 Comment

There's an input type hidden that means you don't need to use the style. Oh and the OP is asking about, JSP, would be nice to give the example code as JSP too
0

Here, To pass data to another JSP page,You can try like this.

<script>
      var fun = "Your value";
</script>
<input type="hidden" name="test" id="test" />
<script> document.getElementById("test").value=fun;</script>

Now You can use this value in another JSP page like.

String test = request.getParameter(test);

Is this ok?

2 Comments

You don't have access to the session from JavaScript
Yes, You are right.We have to use hidden field concept here.
0

If i understand your question correct manner. Assume, You have two jsp pages like jsp1 and jsp2.

I will include jsp2 in jsp1 to send a variable to jsp2 html elements from jsp1.

<jsp:include page="jsp2"/>
var current = "some text";// or a function 
$(.readText).text(current);

In jsp2 page

<div class ="readText"></div>

This might give an idea.

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.