Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an Object from the servlet to the calling JSP [duplicate]

Tags:

java

jsp

servlets

How to pass an Object from the servlet to the calling JSP.

I have a JSP calling a servlet. From this servlet, I am setting the properties of a viewBean. Now, I want to get this property valued set from Servlet on a JSP page.

How to make this ViewBean object available on JSP from Servlet.

like image 373
user1602657 Avatar asked Aug 20 '12 06:08

user1602657


1 Answers

Put the object either in session or request in servlet like :

String shared = "shared";
request.setAttribute("sharedId", shared); // add to request
request.getSession().setAttribute("sharedId", shared); // add to session
this.getServletConfig().getServletContext().setAttribute("sharedId", shared); // add to application context

You can read it in jsp like :

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<body>
<cut value= "${shared}"/>
<cut value= "${requestScope.shared}"/>
<cut value= "${requestScope.request.shared}"/>
${shared} 

Or read it using scriptlet with code :

<%
 String shared = (String)request.getAttribute("sharedId");
 String shared1 = (String)request.getSession().getAttribute("sharedId");
 String shared2 = (String)this.getServletConfig().getServletContext().getAttribute("sharedId");
%>
like image 180
Nandkumar Tekale Avatar answered Sep 25 '22 09:09

Nandkumar Tekale