将值从一个 jsp 页面传递到另一个 jsp 页面

发布于 2024-08-24 03:35:37 字数 260 浏览 3 评论 0原文

我正在从数据库检索值到 jsp 中的表。(到列)

我想将该值插入到数据库中的另一个表中。为此,我使用另一个 jsp 表将该值插入到数据库中,并在之前的 jsp 页面表单操作选项卡中调用该 jsp 页面。

我使用 request.getParameter() 方法将第一个 jsp 页面中的值获取到新的 jsp 页面,但无法使用 request.getParameter() 获取值。

我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

惯饮孤独 2024-08-31 03:35:37

打开第二个 JSP 时,您必须将相关参数(我想是 ID)作为 GET 参数传递到第二个页面。也就是说,表格中的链接应如下所示:

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

然后您可以在第二页上使用 request.getParameter("itemId") 读取该参数。

(如果不使用 JSTL 和 EL,则 ${..} 将替换为 <%= .. %>

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

雨落□心尘 2024-08-31 03:35:37

您只需将这些值作为请求参数传递给下一个请求。这样它们就可以在下一个请求中作为请求参数使用。

如果从页面 A 到页面 B 的请求恰好是通过链接调用的,那么您需要在 URL 中定义参数:

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

这样它们将按预期通过 request.getParameter() 获取在 update.jsp 中。

如果页面A到页面B的请求恰好是通过POST表单调用的,那么您需要将参数定义为表单的输入字段。如果您想在视图中隐藏它们,只需使用

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

这样,它们将按预期由 update.jsp 中的 request.getParameter() 提供。

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.

余生再见 2024-08-31 03:35:37

如果您在使用表单和 request.getParameter() 传递值时仍然遇到问题,您可以尝试另一种方法。

在代码中将每个值对设置为会话变量。

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

现在,只要您的会话仍处于活动状态,这些值就可以从任何 jsp 中获得。

使用:

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

检索 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.

茶花眉 2024-08-31 03:35:37

您应该使用 url 标签构建您的 url:

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

这将为您免费提供 URL 重写(必要时附加 jsessionid)和参数的 URL 编码。

不使用 JSTL 或 EL 的解决方案应被视为特殊情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文