How to pass object from Servlet to JSP?

UPDATED: 16 September 2011


To pass an objects from Servlet to JSP you can use session. You can pass simple object, List, etc... in session but you have to cast to its original type at JSP.

//Servlet Code
setNotification _sN = new setNotification();
_sN.setError("Your message");
request.setAttribute("error", _sN);
RequestDispatcher rd = request.getRequestDispatcher("./settings.jsp");
rd.forward(request, response);

//JSP Code
<%
setNotification _sN = new setNotification();
if (request.getAttribute("error") != null) {
 _sN = (setNotification) request.getAttribute("error");
}
%>
<%= _sN.getError()%>

0 comments :