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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4412758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:20:46  来源:igfitidea点击:

Pass Data from one jsp page to another jsp page

javascriptjsp

提问by nikhil

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.

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

回答by BalusC

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,在那里我查询数据库以检索其他字段

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

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

Assuming that the links in page1.jspare displayed like follows:

假设链接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>

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

然后,你就可以进行预处理的HTTP请求doGet()的方法,HttpServlet这是上听<url-pattern>/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.jspyou will be able to access the dataas follows:

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

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

See also:

也可以看看:

回答by wajiw

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

您可以通过将 GET 或 POST 变量传递到相应的 jsp 页面并使用 jsp 读取它们来实现:

using GET method:

使用 GET 方法:

<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:

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

GET using jQuery js library:

使用 jQuery js 库获取:

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

有很多方法可以使用 GET/POST 将数据从一个页面发送到另一个页面,这和我写一个一样快:-) 使用表单提交是另一个你应该研究的地方,如果这种方式行不通为你。

回答by Juan Mendes

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.

l如果你说的是一个不能序列化为字符串并作为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" )
?>