Passing values from one jsp page to another jsp page

无人久伴 提交于 2019-12-11 02:26:20

问题


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 to new jsp page, but I couldn't get the values using request.getParameter().

How can I solve this?


回答1:


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 <%= .. %>)




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/2385483/passing-values-from-one-jsp-page-to-another-jsp-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!