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.
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.
The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very simple in which all template text is converted to println( ) statements and all JSP elements are converted to Java code. This code implements the corresponding dynamic behavior of the page.
When a request is mapped to a JSP page, the web container first checks whether the JSP page's servlet is older than the JSP page. If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class.
Learn how HTTP works:
HttpServletRequest
and HttpServletResponse
objects.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.
<input type="hidden" name="myObject" value="${myObjectAsString}" />
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:
<form action="nextservlet" method="post"> <input type="hidden" name="myObjectId" value="${myObjectId}" /> ... </form>
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); // ...
once the JSP is rendered the request object is over. So the object you set on request of JSP is available for that JSP page request alone. Do you have any constraint on using session instead of request. So, session can hold data between fresh requests until the session expires.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With