Dialvive
Dialvive

Reputation: 386

How can I set a parameter into the next reload of a jsp?

I have a JSP file that has 10 buttons, clicking on any of the ten should set a parameter into itself and reload, so that the reloading page would read that parameter and then decide which array location needs to be passed to a java function.

So for example, clicking on button0 should set "election" parameter to 0, button1 to 1, etc. So that the url looks like somesite/somepage.jsp?election=0, somesite/somepage.jsp?election=1, etc.

I try to do so declaring each button inside a form like so:

<form name="form1" action="SomePage.jsp" method="post">

    <button class="btn" type="submit" onclick="btns(0)">
        See product 0
    </button>

    <button class="btn" type="submit" onclick="btns(1)">
        See product 1
    </button>

    etc...

</form>

and using the following method every time any of the ten buttons is clicked:

<script language="JavaScript">
    function btns(num){
        document.form1.btnElection.value = num;
        form1.submit();
    } 
</script>

but every time I get btnElection it returns null instead of {0 .. 9}

The way I get and check the parameter is this way:

<%  if (request.getParameter("btnElection") == null) {
        setElection("0");
        session.setAttribute("eleccion", "0");
        requestData("0");
    } else {
        String e = request.getParameter("btnElection");
        if(e != null){
               session.setAttribute("eleccion", e);
               setElection(e);
               requestData(e);
        } else {
            error = "Error: btnElection = " + e + ".";
        }
    }
%>

Any ideas? Thanks

Upvotes: 1

Views: 644

Answers (1)

Dialvive
Dialvive

Reputation: 386

As Swati said in the comment above, the problem with my previous code was not putting a hidden <input> inside the <form> which is modified by the <script> each time a button is pressed, assigned every time the page is reloaded with the parameter in the url ie:somesite/somepage.jsp?election=0, and read by the jsp itself.

That would be declaring each button inside a form like so:

<form name="form1" action="SomePage.jsp">

    <button class="btn" type="submit" onclick="btns(0)">
        See product 0
    </button>

    <button class="btn" type="submit" onclick="btns(1)">
        See product 1
    </button>

    etc...

</form>

Using the following method every time any of the ten buttons is clicked, by calling btns() assigned in the arguments of each button's onClick:

<script language="JavaScript">
    function btns(num){
        document.form1.btnElection.value = num;
        form1.submit();
    } 
</script>

And reading the parameter in this particular case, this way:

<%  if (request.getParameter("btnElection") == null) {
        session.setAttribute("eleccion", "0");
    } else {
        String e = request.getParameter("btnElection");
        if(e != null){
               session.setAttribute("eleccion", e);
        } else {
            ...
        }
    }
%>

Upvotes: 1

Related Questions