将对象从JSP页面传递回Servlet [英] Passing an object from JSP page back to Servlet
问题描述
简而言之,我想知道如何将object
从JSP页面传递回Servlet.具体来说,我有一个form元素,其action标签引用了一个servlet.提交表单时,我想在HttpServletRequest
请求object
中嵌入object
,以便servlet可以取消引用.
In short, I want to know how to pass an object
from JSP page back to a Servlet. Specifically, I have a form element whose action tag references a servlet. On form submission, I want to embed an object
in HttpServletRequest
request object
so that it can be dereferenced by servlet.
到目前为止,我已经尝试在JSP页面中设置request.setAttribute(object)
.但是仍然从servlet检索它时将其设置为null.任何指针将不胜感激.
So far, I have tried setting request.setAttribute(object)
in JSP page. But still retrieving it from servlet gives it as null. Any pointers would be appreciated.
推荐答案
了解HTTP的工作原理:
Learn how HTTP works:
- 客户端(通常是网络浏览器)触发HTTP请求.
- 服务器检索HTTP请求.
- Servletcontainer创建新
HttpServletRequest
和HttpServletResponse
对象. - Servletcontainer使用那些对象调用适当的servlet.
- Servlet处理请求并将请求和响应转发到JSP.
- JSP写入响应正文.
- Servletcontainer提交HTTP响应以及 destroys 请求和响应对象.
- 服务器将HTTP响应发送回客户端.
- 客户端检索HTTP响应并进行处理(显示HTML,应用CSS,执行JS).
- Client (usually, a web browser) fires HTTP request.
- Server retrieves HTTP request.
- Servletcontainer creates new
HttpServletRequest
andHttpServletResponse
objects. - Servletcontainer invokes appropriate servlet with those objects.
- Servlet processes request and forwards request and response to JSP.
- JSP writes to the response body.
- Servletcontainer commits HTTP response and destroys request and response objects.
- Server sends HTTP response back to client.
- Client retrieves HTTP response and processes it (display HTML, apply CSS, execute JS).
通过提交表单发送新请求时,它不会重复使用相同的请求和响应对象.
When you send a new request by submitting the form, it won't reuse the same request and response objects.
有两种方法可以克服HTTP的这种无状态本质.您需要将该对象转换为String
,并将其包含在JSP的HTML表单的隐藏输入字段中,以便在提交时将其用作请求参数.
There are two ways to overcome this stateless nature of HTTP. You need to convert this object to String
and include it in a hidden input field of the HTML form in the JSP so that it'll be available as request parameter upon submission.
<input type="hidden" name="myObject" value="${myObjectAsString}" />
由于HTTP和HTML无法理解Java对象,因此必须转换为String
.从Java的角度来看,HTML基本上是一个很大的String
(在Webbrowser中单击鼠标右键和查看源代码可以看到它).如果不将Java对象转换为String
,则默认情况下,Java对象的toString()
结果将打印为HTML,但按定义不能转换回原始Java对象.复杂对象最常用的String
格式是JSON和XML.有很多Java库可供使用,它们可以在复杂的Java对象和JSON或XML格式的String
之间进行转换.
The conversion to String
is necessary because HTTP and HTML doesn't understand Java objects. HTML is in Java's perspective basically one large String
(do a rightclick and View Source in webbrowser to see it). If you don't convert a Java object to String
, then by default Java object's toString()
result will be printed to HTML, which is not per definition convertible back to the original Java object. Most commonly used String
formats of complex objects are JSON and XML. There are a lot of Java libraries available which can convert between complex Java objects and a String
in JSON or XML format.
或者,如果对象太大或太复杂而无法转换为String
,反之亦然,那么您需要将其存储在服务器的内存或某些数据库中,并将其唯一标识符作为隐藏的输入值传递给周围.通常,会话范围用于此目的.
Or, if the object is too large or too complex to be converted to String
and vice versa, then you need to store it in the server's memory or in some database and instead pass its unique identifier around as hidden input value. Usually the session scope is been used for this.
因此,在准备数据并转发到的servlet中 JSP页面:
So, in the servlet which prepares the data and forwards to the JSP page:
String myObjectId = UUID.randomUUID().toString();
request.getSession().setAttribute(myObjectId, myObject);
request.setAttribute("myObjectId", myObjectId);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
并且,在转发的JSP页面中:
And, in the forwarded JSP page:
<form action="nextservlet" method="post">
<input type="hidden" name="myObjectId" value="${myObjectId}" />
...
</form>
最后,在下一个处理表单提交的servlet中:
Finally, in the next servlet which processes the form submit:
String myObjectId = request.getParameter("myObjectId");
Object myObject = request.getSession().getAttribute(myObjectId);
request.getSession().removeAttribute(myObjectId);
// ...
另请参见:
- 小服务程序如何工作?实例化,会话,共享变量和多线程
- 如何存储单个浏览器标签/窗口的状态?
- How do servlets work? Instantiation, sessions, shared variables and multithreading
- How can I store state for an individual browser tab/window?
See also:
这篇关于将对象从JSP页面传递回Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!