将数据从一个jsp页面传递到另一个jsp页面 [英] Pass Data from one jsp page to another jsp page

查看:349
本文介绍了将数据从一个jsp页面传递到另一个jsp页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个页面(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.

推荐答案


当点击一个链接时,必须将相关值传递给page2.jsp,我在其中查询数据库以检索其他字段

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

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

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

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

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>

然后您就可以在 doGet()中预处理HTTP请求 HttpServlet 的方法,该方法正在侦听< url-pattern> / page2

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);

page2.jsp 您将能够访问数据,如下所示:

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>



参见:




  • 所有关于Servlets的信息 - 包含Hello World示例和有用的链接

  • See also:

    • All about Servlets - contains Hello World example and useful links
    • 这篇关于将数据从一个jsp页面传递到另一个jsp页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆