5

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.

Santosh
  • 17,667
  • 4
  • 54
  • 79
user1602657
  • 141
  • 2
  • 6
  • 8

5 Answers5

17

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");
%>
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
  • I'm relatively new to web development with Java servlets. However, I would like to draw attention to the second code block. I do believe there is an error here. You cannot directly call the variable that you defined in the Java servlet, in this case, 'shared'. This is because servlets rely on reflection to find their associated variables/values. Again, I may be wrong with my analysis, but I don't believe this will work. – Matthew Jul 29 '19 at 02:41
4

Well, firstly you need to set the value so you can access it from your page, something like:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) {
    // Do some work.
    Person value = new Person("Matthew", "Abbott";

    request.setAttribute("person", person);

    // Forward to to the JSP file.
    request.getRequestDispatcher("showValue.jsp").forward(request, response);
  }
}

Next thing, you can access the properties of your value, using Expression Language:

<!DOCTYPE html>
<html>
  <head>
    <title>${person.forename} ${person.surname}</title>
  </head>
  <body>
    <h1>Hello ${person.forename}!!!</h2>
  </body>
</html>
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
0

Something like this should work

request.setParameter("nameOfmyObjectParam",MyObject); //or request.setAttribute
String yourJSP = "/WEB-INF/pages/yourJSP.jsp";

        RequestDispatcher rd = getServletContext().getRequestDispatcher(yourJSP);
        rd.forward(request, response);
KD.
  • 2,015
  • 3
  • 28
  • 59
0

Set the bean as request attribute in servlet using the Servlet API as follows -

request.setAttribute("viewBean", viewBean);

and retrieve (use) it in the JSP using EL as follows -

${requestScope.viewBean}
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
0

Add that ViewBean object in session attribute in servlet. And get that variable in jsp.

in servlet

ViewBean viewbwanObject= new ViewBean() session.setAttribyte("obj",vi);

in jsp.

<%

ViewBean v= (ViewBean)session.getAttribute("obj") %>

Dipen Jogi
  • 151
  • 1
  • 7