Passing values from one jsp page to another jsp pa

2019-07-15 10:00发布

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?

标签: jsp jsp-tags
4条回答
女痞
2楼-- · 2019-07-15 10:11

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

查看更多
干净又极端
3楼-- · 2019-07-15 10:14

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.

查看更多
在下西门庆
4楼-- · 2019-07-15 10:25

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.

查看更多
来,给爷笑一个
5楼-- · 2019-07-15 10:29

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.

查看更多
登录 后发表回答