将数据从一个 jsp 页面传递到另一个 jsp 页面

发布于 2024-10-07 01:43:36 字数 195 浏览 2 评论 0原文

我有两个页面(page1.jsp 和 page2.jsp)。在 page1.jsp 中,我从数据库检索一些值并显示与这些值相对应的超链接。现在我想要的是,当我单击这些超链接中的任何一个时,在 page2.jsp 中我应该显示与数据库中的 vlaue 对应的其他字段。所以我本质上想要的是,当单击链接时,关联的值必须传递到 page2.jsp,我在其中查询数据库以检索其他字段。

I have two pages (page1.jsp and page2.jsp). in page1.jsp i retrieve some values from database and display hyperlinks corresponding to those values. Now what i want is when i click any of those hyperlinks, in page2.jsp i should display other fields coressponding to the vlaue from the database. So what i essentially want is when a link is clicked the associated value must be passed to page2.jsp where i query the database to retrieve other fields.

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

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

发布评论

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

评论(3

绿萝 2024-10-14 01:43:37

如果您正在讨论一个无法按照建议序列化为字符串并作为 http 参数传递的变量,那么您需要将该变量存储在会话中。

--page1.asp
<?
session.setAttribute( "val", record );
?>


--page2.asp

<?
//Cast this to your type
Object record = session.getAttribute("val");

// Clean it up now that you're done
session.removeAttribute( "val" )
?>

lIf you are talking about a variable that can't be serialized as a string and passed as an http parameter, as suggested, then you need to store the variable within the session.

--page1.asp
<?
session.setAttribute( "val", record );
?>


--page2.asp

<?
//Cast this to your type
Object record = session.getAttribute("val");

// Clean it up now that you're done
session.removeAttribute( "val" )
?>
梨涡 2024-10-14 01:43:36

当单击链接时,关联的值必须传递到 page2.jsp,我在其中查询数据库以检索其他字段

那么,您想在显示 JSP 之前预处理 HTTP 请求吗?为此,您需要一个 servlet。

假设 page1.jsp 中的链接显示如下:

<ul>
    <c:forEach items="${ids}" var="id">
        <li><a href="page2?id=${id}">View data for ID ${id}</a></li>
    </c:forEach>
</ul>

那么您将能够在 HttpServlet< 的 doGet() 方法中预处理 HTTP 请求。 /code> 正在侦听 /page2

Long id = Long.valueOf(request.getParameter("id"));
Data data = dataDAO.find(id);
request.setAttribute("data", data); // Will be available as ${data} in JSP.
request.getRequestDispatcher("/WEB-INF/page2.jsp").forward(request, response);

page2.jsp 中,您将能够访问data 如下:

<p>The data for ID ${param.id} is:</p>
<p>Field 1: ${data.field1}</p>
<p>Field 2: ${data.field2}</p>

另请参阅:

when a link is clicked the associated value must be passed to page2.jsp where i query the database to retrieve other fields

So, you want to preprocess the HTTP request before displaying the JSP? For that you need a servlet.

Assuming that the links in page1.jsp are displayed like follows:

<ul>
    <c:forEach items="${ids}" var="id">
        <li><a href="page2?id=${id}">View data for ID ${id}</a></li>
    </c:forEach>
</ul>

Then you will be able to preprocess the HTTP request in the doGet() method of a HttpServlet which is listening on an <url-pattern> of /page2:

Long id = Long.valueOf(request.getParameter("id"));
Data data = dataDAO.find(id);
request.setAttribute("data", data); // Will be available as ${data} in JSP.
request.getRequestDispatcher("/WEB-INF/page2.jsp").forward(request, response);

In page2.jsp you will be able to access the data as follows:

<p>The data for ID ${param.id} is:</p>
<p>Field 1: ${data.field1}</p>
<p>Field 2: ${data.field2}</p>

See also:

淡看悲欢离合 2024-10-14 01:43:36

您可以通过将 GET 或 POST 变量传递到相应的 jsp 页面并使用 jsp:

使用 GET 方法读取它们来完成此操作:

<a href="page2.jsp?datatobesend=value">link</a>

如果您想动态执行此操作,则必须使用 javascript 在单击时添加:

使用 jQuery js 库的 GET :

<script>
$(document).ready(function(){
  $('#mylink').click(function(){
    $(this).attr('href', $(this).attr('href')+'?datatobesend=value');
 });
});
</script>
<a href="page2.jsp" id="mylink">link</a>

有很多方法可以使用 GET/POST 将数据从一个页面发送到另一个页面,这和我写一个页面一样快:-) 如果这种方法不起作用,那么使用表单提交是您应该考虑的另一个地方为你而出。

You can do that by either passing GET or POST variables to the corresponding jsp page and reading them using jsp:

using GET method:

<a href="page2.jsp?datatobesend=value">link</a>

If you want to do this dynamically you will have to add to that on click using javascript:

GET using jQuery js library:

<script>
$(document).ready(function(){
  $('#mylink').click(function(){
    $(this).attr('href', $(this).attr('href')+'?datatobesend=value');
 });
});
</script>
<a href="page2.jsp" id="mylink">link</a>

There are many ways to use GET/POST to send data from one page to another, this is just as quick as I could write one up :-) Using form submissions are another place you should look into if this way doesn't work out for you.

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