Saturday, November 5, 2011

Passing variable values from one jsp to another jsp

Here is a very simple example to pass variable value from one jsp to another jsp.


Main JSP where variables are initialized.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title> Display</title>
    </head>

    <SCRIPT LANGUAGE="JavaScript">
        var no = 4;
        var eventName = new Array(no);


        eventName[3] = "E1";
        eventName[2] = "E2";
        eventName[1] = "E3";
        eventName[0] = "E4";
       
        function getDetails()
        {
            //window.location = "./Timeline - All.html";
            window.open('./child.jsp','','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no');
        } 
            
    </SCRIPT>

    <body>
        <h1>Hello!</h1>
        <form name ="f1" method="post">
            <table border="0" cellpadding="30">
                <tr>
                    <td>
                        <font size="3" face="arial" color="Black">Enter ID</font>
                    </td>
                    <td>
                        <input type="text" name="id">
                    </td>
                </tr>   

                <tr>
                    <td>
                        <input type="button" value="Submit" name="showId" onclick="getDetails()"/>
                    </td>
                </tr>

            </table>
        </form>

    </body>

</html>


Child JSP where we get the values of main jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Child</title>
    </head>

    <SCRIPT LANGUAGE="JavaScript">
        var no = 4;
        var eventName = window.opener.eventName;
       
       
        function getDetails()
        {
            for(i=0;i<no;i++) {
                alert(eventName[i]);
            }
        } 
            
    </SCRIPT>

    <body onload="getDetails()">

    </body>

</html>

Total Pageviews