The request object in the servlet must be used to set the object as attribute. The attribute name is used to in JSP to access the object.
request.setAttribute("foo", bar);
In the above example, foo
is the attribute name and bar
is the object being sent to JSP.
Now, we need to forward the request from servlet to JSP.
request.getRequestDispatcher("display.jsp").forward(request, response);
Forward does not change the url in the browser. It is a server side operation where the servlet is transferring the control to JSP. The user sees the same url as servlet in the browser. If the servlet was hosted at /webapp/customerInformation
, it will NOT change to /webapp/display.jsp
.
The complete request will be served by single url.
Below code helps us transfer bar
object to display.jsp
request.setAttribute("foo", bar);
request.getRequestDispatcher("display.jsp").forward(request, response);
We can access all the properties of the object using expression language.
<ul>
<li>${foo.name}</li>
<li>${foo.title}</li>
<li>${foo.desc}</li>
</ul>
Notice that we are not using bar.name
, but foo.name
as the attribute name is foo
. We can transfer complex objects using this mechanism.