Abhishek Jain Abhishek Jain - 2 months ago 10
Java Question

Why am I getting parameter value as null?

I hooked login.jsp and wrote following code in it:-

<script>

function a(){
var otp=document.getElementById("otpText").value;
document.location.href="?otp="+otp;
<% System.out.println(request.getParameter("otp")); %> //printing null
return false;
}

</script>

<form name="otp" method="post" action="" >
Enter otp:
<input type="text" name="otpText" id="otpText"/>
<input type="submit" value="Submit" onClick="return a()"/>
</form>


I typed some value inside otpText and that value is displayed in the url when I submitted the form. But when I am printing that value as in above code,it is printing null. I need that value as I want to store the same into session.Please help, any help would be appreciated.

Answer

If you change your code to

var otp=document.getElementById("otpText").value;
console.log (otp);  // or alert (otp);
document.location.href="?otp="+otp;

you will see the value.