简体   繁体   中英

How should I do to send data between two jsp with a servlet

I want to display something from one jsp page in another jsp page by clicking a button. I did it using request.setAttribute request.getAttribute but it doesn't work for me, for some reason the variable I send is null or the page is blank.

you can pass the variables through request scope or session scope.

request.setAttribute("variable name","value of its");

session.setAttribute("variable name","value");

Here a detailed exmple http://www.jsptut.com/sessions.jsp

From your original question: When you are doing setAttribute(), its scope is limited to the request when the main page is loading and hence will not be available on the next page as it will be a new request.

<%Object product=ptp;
                   request.setAttribute("purchase", ptp.getId());
          %>

What you can do is, submit this value in URL param as GET or in a form (get/ post) to fetch it on next JSP using request.getParameter().

Or you can use session scope by session.setAttribute()

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM