开发者

Passing values from one jsp page to another jsp page

I'm retrieving values from database to table in jsp.(to a column)

I want to insert that value into another table in database. To do that I'm using another jsp table to insert that value in db and I call that jsp page in my previous jsp's page form action tab.

I use request.getParameter() method to get the values in my first jsp page 开发者_Python百科to new jsp page, but I couldn't get the values using request.getParameter().

How can I solve this?


When opening the 2nd JSP you have to pass the relevant parameters (an ID I suppose) as GET parameters to the second page. That is, the link in your table should look like:

<a href="edit.jsp?itemId=${item.id}" />

Then you can read that parameter with request.getParameter("itemId") on the second page.

(if not using JSTL and EL, the ${..} is replaced by <%= .. %>)


You just need to pass the values as request parameters to the next request. This way they'll be available in the next request as, well, request parameters.

If the request from page A to page B happens to be invoked by a link, then you need to define the parameters in the URL:

<a href="update.jsp?userid=${user.id}&fieldid=${field.id}">update</a>

This way they'll be as expected available by request.getParameter() in update.jsp.

If the request from page A to page B happens to be invoked by a POST form, then you need to define the parameters as input fields of the form. If you want to hide them from the view, then just use <input type="hidden">.

<form method="post" action="update.jsp">
    ...
    <input type="hidden" name="userid" value="${user.id}">
    <input type="hidden" name="fieldid" value="${field.id}">
</form>

This way they'll be as expected available by request.getParameter() in update.jsp.


If you are still having trouble passing values with your form and request.getParameter() there is another method you can try.

In your code set each value pair to a session variable.

session.setAttribute("userId", userid);
session.setAttribute("fieldId", fieldid);

These values will now be available from any jsp as long as your session is still active.

Use:

int userid = session.getAttribute("userId");
int fieldid = session.getAttribute("fieldId");

to retrieve your values on the update.jsp page. Remember to remove any session variables when they are no longer in use as they will sit in memory for the duration of the users session.


You should construct your url with the url tag:

<c:url value="edit.jsp" var="url">
    <c:param name="itemId" value="${item.id}"/>
</c:url>
<a href="${url}">edit</a>

This will give you URL-rewriting (append jsessionid when necessary) and URL-encoding of your params for free.

A solution that doesn't use JSTL or EL should be considered a special case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜