java 如何将对象从jsp发送到servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10681229/
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
how to send an object from jsp to servlet
提问by user1407310
Possible Duplicate:
Passing Object from JSP to Servlet
可能的重复:将
对象从 JSP 传递到 Servlet
I have an object that is being sent successfully from a servlet to a jsp. In that jsp page I have a button and when user click on that button the same object should be sent from that jsp page to another servlet. how can I do that ??
我有一个从 servlet 成功发送到 jsp 的对象。在那个 jsp 页面中,我有一个按钮,当用户单击该按钮时,应该将同一对象从该 jsp 页面发送到另一个 servlet。我怎样才能做到这一点 ??
回答by developer
Send it as request attribut when clicking on button a) Set request attrbute in jsp
单击按钮时将其作为请求属性发送 a) 在 jsp 中设置请求属性
request.setAttribute("thatobject",thatobject);
In Servlet retrive it as
在 Servlet 中将其检索为
Thatobject obj = (ThatObject) request.getAttribute("thatobject");
b) Set the object in session session.setAttribute("thatobject",thatobject)
and retrive it as
b) 在会话中设置对象session.setAttribute("thatobject",thatobject)
并将其检索为
Thatobject obj = (ThatObject) session.getAttribute("thatobject");
Oh my mistake Updated...
哦,我的错误已更新...
Note:Also iam not providing null check. I think you can handle it
注意:我也不提供空检查。我想你能应付
Edit:
编辑:
Even you can do other way, which is very sofistiacted way,
即使你可以做其他方式,这是非常sofistiacted方式,
a) Create a Bean class
b) provide Object as a attribute in bean class and provide getter and setter methods too
C) in jsp use that bean to set the object when the page is submitting to servlet
d) retrive the object from the bean in servelt
a) 创建一个 Bean 类
b) 在 bean 类中提供 Object 作为属性并提供 getter 和 setter 方法
C) 在 jsp 中使用该 bean 在页面提交给 servlet 时设置对象
d) 从 bean 中检索对象服务员
Edit 2:
编辑2:
Please check this link there is clear explanation. JSP2Servlet
请检查此链接有明确的解释。 JSP2Servlet
Edit 3
编辑 3
Note : If you are submiting form , the request becomes new request and the object will become null, when you use request.getattribute in servlet. So above approach will not work on Form submit in jsp.
注意:如果您正在提交表单,则当您在 servlet 中使用 request.getattribute 时,请求将成为新请求并且对象将变为空。所以上述方法不适用于jsp中的表单提交。
So to overcome this please folw the instructions provide in below link
因此,要克服此问题,请按照以下链接中提供的说明进行操作