Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i access javascript variables in JSP?

I want to access Javascript variables in JSP code. How can I do that?

like image 881
Tushar Ahirrao Avatar asked Jun 25 '10 06:06

Tushar Ahirrao


People also ask

Can I use JavaScript variable in JSP?

simple, you can't! JSP is server side, javascript is client side meaning at the time the javascript is evaluated there is no more 'jsp code'.

How do I pass a JavaScript variable to another JSP page?

Here, To pass data to another JSP page,You can try like this. Now You can use this value in another JSP page like. String test = request. getParameter(test);

How do you assign a JavaScript variable value to a JSP variable?

You can set it in some hidden fields and then you can send it over the form submision. Something like this: <input type="hidden" value="returnVariable()" name="javaScriptVariable" /> ?

How do you access variables in JavaScript?

In JavaScript, variables can be accessed from another file using the <script> tags or the import or export statement. The script tag is mainly used when we want to access variable of a JavaScript file in an HTML file. This works well for client-side scripting as well as for server-side scripting.


1 Answers

JavaScript variable is on client side, JSP variables is on server side, so you can't access javascript variables in JSP. But you can store needed data in hidden fields, set its value in client and get it on server over GET or POST.

Client side:

<script type="text/javascript">
var el = document.getElementById("data");
el.value = "Needed_value";
</script>
<form action="./Your_JSP.jsp" method="POST">
<input id="data" type="hidden" value="" />
<input type="submit" />
</form>

server side:

<%
if (request.getParameter("data") != null) { %>
 Your value: <%=request.getParameter("data")%>
<%   
} 
%>
like image 52
SageNS Avatar answered Oct 23 '22 20:10

SageNS