将对象从 JSP 页面传递回 Servlet [英] Passing an object from JSP page back to Servlet

查看:32
本文介绍了将对象从 JSP 页面传递回 Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我想知道如何将 object 从 JSP 页面传递回 Servlet.具体来说,我有一个表单元素,它的操作标签引用了一个 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 创建 new HttpServletRequestHttpServletResponse 对象.
  • Servletcontainer 使用这些对象调用适当的 servlet.
  • Servlet 处理请求并将请求和响应转发给 JSP.
  • JSP 写入响应正文.
  • Servletcontainer 提交 HTTP 响应并销毁请求和响应对象.
  • 服务器将 HTTP 响应发送回客户端.
  • 客户端检索 HTTP 响应并对其进行处理(显示 HTML、应用 CSS、执行 JS).
  • Client (usually, a web browser) fires HTTP request.
  • Server retrieves HTTP request.
  • Servletcontainer creates new HttpServletRequest and HttpServletResponse 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}" />

转换为 String 是必要的,因为 HTTP 和 HTML 不理解 Java 对象.从 Java 的角度来看,HTML 基本上是一个大的 String(在 webbrowser 中右键单击并View Source 来查看它).如果您不将 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.

因此,在准备数据并转发到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);
// ...

另见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆