32

I have a servlet that forwards to a HTML page, using redirect. Because I am using ajax and php on the html page to do other functions. Can turn into a jsp. Is there a way I can get the name -"poNumber" I get in servlet in the session attributes. I was to get it and display it's value.

I am new to programming.

Can get this working in jsp.

However need to get this working in a html page. Can I do it with javascript?

I have tried:

      <script type="text/javascript">
      var purchaseOrderNo = session.getAttribrute("pONumb");
      document.write("pONumb");
      </script> [

This does not output any values on the HTML page.

Tried:

       <script type="text/javascript">
       var purchaseOrderNo = (String) session.getAttribrute("pONumb");
           document.write("pONumb");
           </script> 

Again get no output on page.

Tried:

            <script type="text/javascript">
            String purchaseOrderNo = (String) session.getAttribrute("pONumb");
            document.write("pONumb");
            </script> 

Again get no output on page?

Can not think of any thing else to try. The servlet that redirects to this HTML page creates and set session attribute pONumb.

2
  • 1
    Can you add the complete JSP to the question? What is the HTML output you are seeing? If the JSP is outputting the <script> block you might want to try var purchaseOrderNo = <%= session.getAttribrute("pONumb"); %> Commented Aug 2, 2011 at 20:45
  • 2
    What is (String) session.getAttribrute("pONumb");? It is not even a correct JavaScript syntax... And JAVA is not the same as JavaScript. Read more about JSP here: Wikipedia - JSP Commented Aug 2, 2011 at 22:15

4 Answers 4

43

No, you can't. JavaScript is executed on the client side (browser), while the session data is stored on the server.

However, you can expose session variables for JavaScript in several ways:

  • a hidden input field storing the variable as its value and reading it through the DOM API
  • an HTML5 data attribute which you can read through the DOM
  • storing it as a cookie and accessing it through JavaScript
  • injecting it directly in the JS code, if you have it inline

In JSP you'd have something like:

<input type="hidden" name="pONumb" value="${sessionScope.pONumb} />

or:

<div id="product" data-prodnumber="${sessionScope.pONumb}" />

Then in JS:

// you can find a more efficient way to select the input you want
var inputs = document.getElementsByTagName("input"), len = inputs.length, i, pONumb;
for (i = 0; i < len; i++) {
    if (inputs[i].name == "pONumb") {
        pONumb = inputs[i].value;
        break;
    }
}

or:

var product = document.getElementById("product"), pONumb;
pONumb = product.getAttribute("data-prodnumber");

The inline example is the most straightforward, but if you then want to store your JavaScript code as an external resource (the recommended way) it won't be feasible.

<script>
    var pONumb = ${sessionScope.pONumb};
    [...]
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

I know this is an old thread, but I still would like to ask why the inline example (the last one) does not work if the JS code is in an external file. Thanks!
You could have it in an external file, but the file would need to be pre-processed, you can no-longer serve it as a static file. You lose a bunch of advantages that way - caching, CDNs etc.
Using a meta tag (versus a hidden input) is also an option
5
<%
String session_val = (String)session.getAttribute("sessionval"); 
System.out.println("session_val"+session_val);
%>
<html>
<head>
<script type="text/javascript">
var session_obj= '<%=session_val%>';
alert("session_obj"+session_obj);
</script>
</head>
</html>

Comments

4

Below code may help you to achieve session attribution inside java script:

var name = '<%= session.getAttribute("username") %>';

2 Comments

Please read this how-to-answer and follow the guideline there to provide quality answer.
this indeed helped me - could you tell me, why to use the <% %> tags inside the <script></script> tags?
0
<?php 
    $sessionDetails = $this->Session->read('Auth.User');

    if (!empty($sessionDetails)) {


        $loginFlag = 1;
        # code...
    }else{
        $loginFlag =  0;
    }


?>

<script type="text/javascript">

    var sessionValue = '<?php echo $loginFlag; ?>';
    if (sessionValue = 0) {

        //model show
    }
</script>

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.