user3458482
user3458482

Reputation: 1

How to pass a variable from javascript to another jsp page?

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 ?

Upvotes: 0

Views: 11801

Answers (4)

rakeeee
rakeeee

Reputation: 1073

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.

Upvotes: 0

Nick Holt
Nick Holt

Reputation: 34321

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>

Upvotes: 1

chintan
chintan

Reputation: 471

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?

Upvotes: 0

Max Langerak
Max Langerak

Reputation: 1207

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> 

Upvotes: 0

Related Questions