Abhijit Kumbhar
Abhijit Kumbhar

Reputation: 961

How to pass hidden values using form in jsp

My code is as follows

<form method="post" action="Answer.jsp">
    <input type="submit" value="answer">
    <%String q_id=rs.getString("q_id"); %>
    <input type="hidden" value="<%out.print(q_id);%>"> 
</form> 

I want to pass q_id to page Answer.jsp I got the value of q_id but I didn't understand how to pass (or using any different method) value?

Upvotes: 1

Views: 9147

Answers (2)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

In your JSP form you'll need

<form method="post" action="Answer.jsp">
    <input type="hidden" name="q_id" value="<%= rs.getString("q_id") %>">
    <input type="submit" value="Answer">
</form>

Then you can receive q_id in your Answer.jsp as

<p>Question ID: <%= request.getParameter("q_id") %></p>

or, using JSP EL (recommended)

<p>Question ID: ${param.q_id}</p>

Upvotes: 4

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29296

On page where you want to hide the value.

<form method="post" action="Answer.jsp">
    <input type="hidden" id="q_id" name="q_idName" value="someValue">
<input type="submit">   
</form>

Then on the 'Answer.jsp' page you can get the value by calling the getParameter(String name) method of the implicit request object, as so:

<% String q_id = request.getParameter("q_idName"); %>
 Value :  <%=q_id %>

Upvotes: 0

Related Questions