开发者

How can i access javascript variables in JSP?

I want to access Javascript variables in JSP code开发者_开发问答. How can I do that?


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")%>
<%   
} 
%>


I came across the same issue. I wanted to fetch couple of data in jsp. What I did is, in javascript function I concatenated the data in one variable with delimiter and passed that variable in parameter like this.

function myname()
 {
var fname = "FirstName";
var lname = "LastName";
var fullname = fname+","+lname;
document.location.href = "demopage.jsp?name="+fullname;
}

And then in jsp page, we can fetch the full name as,

<c:set var="varname" value='<%= request.getParameter("name") %>' />

Now we can split the first name and last name using delimiter ','.


function call()

   {
    var name="xyz";
    window.location.replace=("a.jsp?m="+name);
   }

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

 if(name!=null){
    out.println(name);        
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜