Question

I can successful get the object from the servlet to the jsp page but can quite figure how to get the individual elements in that object to display, I tried to get the JSTL tag lib working as shown below but to no avail. I Would be very grateful for any help on this I know its only something small as the object is there on the jsp.

Also, Is this the best method? What are the good alternatives?

Servlet to supply Welcome.jsp

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession(true);
    if (session.getAttribute("currentSessionUser") != null) {
        user = (User) session.getAttribute("currentSessionUser");
        userData = (User) ProfileDAO.displayUserProfile(user);

        request.setAttribute("userData", userData);
        request.getRequestDispatcher("Welcome.jsp").forward(request, response);
    } else {
        response.sendRedirect("LoginFailure.html");
    }
}

Welcome.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    :
    :
    :
<title>Welcome</title>
</head>
<body>
Welcome to BLAHBLAH.COM

<%
    Object userData = request.getAttribute("userData");

%>
<table>
    <c:choose items="${userData}" var="userData">
        <tr>
            <td>${userData.name}</td>
            <td>${userData.information}</td>
            <td>${userData.address1}</td>
       </tr>
    </c:choose>
</table>

<p>userData></p>

Was it helpful?

Solution

Using Scriptlets in jsp is not recommended.We need to use

1.El expression
or

2.TagLibrary

The problem with your code is you are using a scriptlet in jsp which is not necessory at all

  Object userData = request.getAttribute("userData");

because userData object is already available in request scope with this code

request.setAttribute("userData", userData);

3.You can print the values of userData object directly by using El expressions like this

            <td>${userData.name}</td>
            <td>${userData.information}</td>
            <td>${userData.address1}</td>

4.You can also print the values using jstl which is always recommended approach

  <c:out value="${userData.name}"/>
  <c:out value="${userData.information}"/>
  <c:out value="${userData.address1}"/>

OTHER TIPS

I'm not sure If you would want JSTL as a requirement, but here is a code sample that should work in JSP

<%
     User userData = (User)request.getAttribute("userData");
%>
<table>
    <tr>
       <td><%= userData.name %></td>
       <td><%= userData.information %></td>
       <td><%= userData.address1 %></td>
    </tr>
</table>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top