How do I pass JavaScript values to Scriptlet in JSP?

user601367 picture user601367 · Apr 18, 2011 · Viewed 167k times · Source

Can anyone tell me how to pass JavaScript values to Scriptlet in JSP?

Answer

harishtps picture harishtps · Apr 18, 2011

I can provide two ways,

a.jsp,

<html>
    <script language="javascript" type="text/javascript">
        function call(){
            var name = "xyz";
            window.location.replace("a.jsp?name="+name);
        }
    </script>
    <input type="button" value="Get" onclick='call()'>
    <%
        String name=request.getParameter("name");
        if(name!=null){
            out.println(name);
        }
    %>
</html>

b.jsp,

<script>
    var v="xyz";
</script>
<% 
    String st="<script>document.writeln(v)</script>";
    out.println("value="+st); 
%>