Question

I have anchor-link in jsp page as shown below inside the <td> in table.

<td>
    <span>
        <a href="AddDescriptionForEvent.jsp?" name="count"><%=(cnt)%></a>
    <span>
</td>

here cnt inside scriplet is an integer. The tags are in the <form> and action attribute of the <form> is directing to the correct next page.
I have to take the value of that integer in the next page.

I am using like below,

int day = nullIntconv(request.getParameter("count"));

here nullIntconv will convert string to integer.

but I am not getting the value which I selected. It is giving me 0 always.

Kindly suggest.

Was it helpful?

Solution

You need some changes in your href , a href doesn't submit as a form element(eg- textbox,textarea etc..)

Try using like this..

<td><span> <a href="AddDescriptionForEvent.jsp?count=<%=(cnt)%>">Click to get count</a><span></td>

In Above count will send as a query string
On the next page read count from request ...

String c= request.getParameter("count");
if(c!=null)
{
int count=Integer.parseInt(c);//converting back into integer
}

-----your custom code here----------

OTHER TIPS

<a> cannot be used as you think you can. It is not one of the HTML elements which is dependent on a <form> for a submit like <input>, <textarea>, <select> etc.

You can read more about using <a> here and about how to pass request-parameters in a URL here. Also somethings about HTML form and its elements.

so if your code is something like this:

<form action="/AddDescriptionForEvent.jsp" name="myForm">
    <td>
        <input type="text" name="someText" value="some Value" />
    </td>
    <td>
        <span>
            <a href="AddDescriptionForEvent.jsp?" name="count"><%=(cnt)%></a>
        <span>
    </td>

    <input type="submit" value="Press me to Submit" />
</form>

Then on-clicking of the submit button you would only be sending the value of the input someText and not of count.
To send the value of count with other values make the following form:

<form action="/AddDescriptionForEvent.jsp" name="myForm">
    <td>
        <input type="text" name="someText" value="some Value" />
    </td>
    <td>
        <span>
            <!-- changed the <a> tag to <input> -->
            <input type="text" name="count" value="<%=(cnt)%>" />
        <span>
    </td>

    <input type="submit" value="Press me to Submit" />
</form>

or you can just use the following without the <form>:

<td>
    <span>
        <a href="AddDescriptionForEvent.jsp?count=<%=cnt%>">Click this link to Add</a>
    <span>
</td>
<!-- Notice the placement of the "cnt" variable of JSP -->

To also pass the other parameter on click of this <a> link, modify the href to href="AddDescriptionForEvent.jsp?count=<%=cnt%>&someText=some value"

These are the two way you can achieve your desired result. Your java code to fetch request-parameter is fine.

<%=(cnt)%>

Instend of

use

" name="count"><%=(cnt)%>

   thanks for all your replies.

     I did like this in the main page, added id
     <td align="center" height="35" id="day_<%=(cnt)%>">
     <span><a href="AddDescriptionForEvent.jsp?id=<%=(cnt)%>"><%=(cnt)%></a></span></td>

     And in the next page i got the required output as

     int d=nullIntconv(request.getParameter("id"));

        Where nullIntconv is the string to integer converter.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top