Passing an object from JSP page back to Servlet

前端 未结 3 920
执念已碎
执念已碎 2020-11-22 15:09

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 fo

3条回答
  •  眼角桃花
    2020-11-22 16:11

    Learn how HTTP works:

    • 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.

    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.

    
    

    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.

    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.

    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);
    

    And, in the forwarded JSP page:

    ...

    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);
    // ...
    

    See also:

    • How do servlets work? Instantiation, sessions, shared variables and multithreading
    • How can I store state for an individual browser tab/window?

提交回复
热议问题