Send value from 1 jsp to another jsp

好久不见. 提交于 2019-12-26 14:36:12

问题


I have 1 JSP, say 1.jsp in which I have a value which am getting from request.getAttribute("testvalue") from action class, as I am using Struts framework. I need to send this value from 1.jsp to another 2.jsp. Please let me know how to do this?

Also going ahead,i need to send this value from 2.jsp to 3.jsp. Am trying to avoid using setting the value in session. So let me know how to do this?


回答1:


Getting value from the request, you should be putting a value to a request. This code

request.getAttribute("testvalue");

is getting a value, but that code

request.setAttribute("testvalue", value);

is putting it.

This is because you want to avoid using the HTTP session.




回答2:


If this is within a single request, the request attributes are available to all pages in the forwarding chain. This means all servlets and JSP's have access. So at any point during the request cycle, after the attribute has been set, the value can be retrieved using request.getAttribute(String).




回答3:


Here are 4 another way that i know:

use get Parameter:for example you have a link to second jsp file. add your parameter at the end of your link. like this: mysite.com/second.jsp?param1=value1&param2=value2

use form in the firstjsp page and in this use hidden input:

<form action="second.jsp" method="post">
<input type="hidden" name="param1" value="value1" />
<input type="hidden" name="param2" value="value2" />
<input type="submit" name="next" value="Next Page" />
</form>

use application variable but if your data is general for all, or use speciall param name for store your value. for example you can use:

<% application.setAttribute("user1_param1","value1"); %>

use RequestDispatcher in your code. and set your param in the request object instead of session.

<%
request.setAttribute("param1","value1");
RequestDispatcher r=request.getRequestDispatcher("second.jsp");
r.forward(request, response);
%>

please Subtilize that you can use each other in the different state. for example you can use RequestDispatcher when you will forward a request before send any result ro user. and the other ways use when you will your current data back to you in the second page in the next use request.



来源:https://stackoverflow.com/questions/19713016/send-value-from-1-jsp-to-another-jsp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!