Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Javascript can you get the value from a session attribute set by servlet in the HTML page

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.

like image 436
user878195 Avatar asked Aug 02 '11 20:08

user878195


People also ask

How to get session attribute value in JavaScript?

To get the value in client side (javascript), you need a routine to pass the session id to javascript. This can be done using a hidden field runat=server" and pass the session id value to hidden field in code behind page_load method.

What is session variable in Servlet?

A servlet can use the session of user to create some variables. These variables occupy server memory. Users cannot deny creation of session variables. One servlet can create session variables and other servlets can fetch or change the value of session variables.

What is the default value of session attribute in JSP?

session = "true" Note : By default, session attribute is set to true.

What is session attribute?

The session attribute indicates whether or not the JSP page uses HTTP sessions. A value of true means that the JSP page has access to a builtin session object and a value of false means that the JSP page cannot access the builtin session object.


3 Answers

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>
like image 127
Alex Ciminian Avatar answered Sep 28 '22 11:09

Alex Ciminian


<%
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>
like image 21
user2552572 Avatar answered Sep 28 '22 11:09

user2552572


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

var name = '<%= session.getAttribute("username") %>';
like image 34
Nimesh Dadhaniya Avatar answered Sep 28 '22 11:09

Nimesh Dadhaniya