如何将数据从一个jsp页面发送到另一个jsp页面[重复]

时间:2022-06-06 10:17:17

This question already has an answer here:

这个问题在这里已有答案:

I am getting the problem in sending data and id from one jsp page to another jsp page. Actualy i want to send data and id from first jsp(a.jsp) page to second jsp(b.jsp) page.

我在将数据和id从一个jsp页面发送到另一个jsp页面时遇到了问题。 Actualy我想从第一个jsp(a.jsp)页面向第二个jsp(b.jsp)页面发送数据和id。

2 个解决方案

#1


2  

There are three ways to send data from one JSP to another

将数据从一个JSP发送到另一个JSP有三种方法

1.

String name="Hello";

request.setAttribute("Name",name);

From Another JSP Access by:

从另一个JSP访问:

request.getAttribute("Name");//Hello

2.

String name="Hello";

session.setAttribute("Name",name);

From Another JSP Access by:

从另一个JSP访问:

session.getAttribute("Name");//Hello

3.

String name="Hello";

localStorage.setItem("Name",name);

From Another JSP Access by:

从另一个JSP访问:

localStorage.getItem("Name")//Hello

#2


1  

You can also use sessions to store data that can then be accessed by any page.

您还可以使用会话来存储任何页面都可以访问的数据。

In your servlet:

在你的servlet中:

HttpSession session = request.getSession(true);

To set data in session (page a):

要在会话中设置数据(第a页):

String data = "myDataAsString";
session.setAttribute("myData", data );

To read data from session (page b):

要从会话中读取数据(第b页):

String data = (String) session.getAttribute("myData"); 

#1


2  

There are three ways to send data from one JSP to another

将数据从一个JSP发送到另一个JSP有三种方法

1.

String name="Hello";

request.setAttribute("Name",name);

From Another JSP Access by:

从另一个JSP访问:

request.getAttribute("Name");//Hello

2.

String name="Hello";

session.setAttribute("Name",name);

From Another JSP Access by:

从另一个JSP访问:

session.getAttribute("Name");//Hello

3.

String name="Hello";

localStorage.setItem("Name",name);

From Another JSP Access by:

从另一个JSP访问:

localStorage.getItem("Name")//Hello

#2


1  

You can also use sessions to store data that can then be accessed by any page.

您还可以使用会话来存储任何页面都可以访问的数据。

In your servlet:

在你的servlet中:

HttpSession session = request.getSession(true);

To set data in session (page a):

要在会话中设置数据(第a页):

String data = "myDataAsString";
session.setAttribute("myData", data );

To read data from session (page b):

要从会话中读取数据(第b页):

String data = (String) session.getAttribute("myData");