Example : How to pass value from java script to jsp in same page [passing value from js to jsp].
<%-- 
    Document   : PassValuefromJavaScripttoJSP1
    Created on : 3 Jun, 2019, 10:31:14 AM
    Author     : Codershelpline
--%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Pass Value from Java Script to JSP</title>
        <script> 
		function getdata()
		{ 
			var m="Hello India"; 
			window.location.replace("chartostring.jsp?store="+m); 
		} 
	</script>        
    </head>

    <body>
        <input type="button" onclick="getdata();" value="Click Me"> 
        <%
	   String message=request.getParameter("store"); 
	   //out.println(message); 
        %>
    </body>
</html>

NB :'chartostring.jsp' is the saved name of this page and 'store' is a variable where js value is 
     stored and used in JSP.
Example : How to pass value from java script to jsp in another page [passing value from js to jsp].
<%-- 
    Document   : PassValuefromJavaScripttoJSP
    Created on : 3 Jun, 2019, 10:31:14 AM
    Author     : Codershelpline
--%>

first.jsp (Source Page)

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Pass Value from Java Script to JSP2</title>        
        <script>
		var msg="Hello World"
		window.location.href="second.jsp?value=" + msg; 
	</script>
    </head>
    <body></body>
</html>
NB : Run this page first.
---------------------------------------------------------------
<%-- 
    Document   : PassValuefromJavaScripttoJSP
    Created on : 3 Jun, 2019, 10:31:14 AM
    Author     : Codershelpline
--%>

second.jsp (Destination Page)

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>show</title>
    </head>
    <body>
        <%
            String message = request.getParameter("value");
            //out.println("Message is: "+message);          
        %>
    </body>
</html>
NB : 'value' is the variable to store js value and used in jsp.

Loading

Categories: JSP

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.