2
function submit() {
    var name = document.getElementById('name').value;
    var mob = document.getElementById('number').value;

    sessionStorage.setItem("name", name);
    sessionStorage.setItem("mob", mob);
    document.getElementById("leadForm").action = "/confirm";
    document.leadForm.submit();
}

On the confirm page, my JSP is called where I want to print this name and mob passed in a session, and along with it, one more attribute selected on this page, I have to pass it to another page.

I am not sure how to get session values from JavaScript to JSP.

Writing in my JSP page:

String name = (String) session.getAttribute("name");
out.println(name);

I am getting null value.

2
  • You have to send it to the web server, via a form, or AJAX. Commented Feb 10, 2015 at 7:17
  • And please, don't use scriptlets in a JSP page, it is so 90's, and not in a good way :) Commented Feb 10, 2015 at 7:33

3 Answers 3

1

sessionStorage is related to the browser and the variables name , mob are set in the client side not in the request.

You could either pass them to the server side , or display their values using the javascript again ,

  sessionStorage.getItem('name');
Sign up to request clarification or add additional context in comments.

Comments

0

JS sessionStorage and JSP session are not the same.

If you had submitted the html form with name and mobile no then this will do:

submit.jsp

<form action='confirm.jsp'>
   Name: <input type='text' name='name'/><br/>
   Mobile No: <input type='text' name='number'/><br/>
   <input type='submit' value='Submit'/>
</form>

confirm.jsp

Name: ${param.name}<br/>
Mobile: ${param.number}<br/>

Comments

0

You could create hidden fields for these variables in leadForm.

<form name="leadForm" method="post" action="/confirm">
    <input type="hidden" name="name"/>
    <input type="hidden" name="mob"/>
    ...
</form>

Then you can set them in your #input() function as:

document.leadForm.name.value = name;
document.leadForm.mob.value = mob;

Now, if you post the form, the values will be available for you:

String name = (String) request.getParameter("name");
String mob = (String) request.getParameter("mob");

They won't be in the session, as the session is a server side concept (remember, HTTP is stateless), but they will be in the HTTP request. If you need them in the session from now on, you can put them into it.

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.