Nathan
Nathan

Reputation: 11

Forwarding from Servlet to JSP

I am trying to implement the MVC2 model. I have a Servlet that fetches data from a session bean and forwards the entity from the servlet to a jsp:

public class MyServlet extends HttpServlet{

@EJB UserFacade userFacade;  

//Fetch the user from the session bean  
Users currUser=userFacade.find(userName);  
...
request.setAttribute("user", currUser);  
getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);  
}  

in index.jsp: I get the user from the request, and I can print its name if I use scriptlets tags, but when I use EL nothing is printed:

<@page import="Entities.Users">  
 <"Users currUser = (Users)request.getAttribute("user");">  
 <= currUser.getName() > -OK!  
 ${currUser.name}-Nothing is printed!  

How should I include/forward the session-bean into the JSP in order to be able to use EL (and avoid using scriptlets)?
Is this the preferred way to implement the Model View controller?

Upvotes: 1

Views: 884

Answers (1)

Bozho
Bozho

Reputation: 597076

EL uses request attributes. You don't have currUser as request attributes. If you try ${user.name} it will work.

I'm not sure what you mean by "session bean". EJBs are session beans, but you didn't show anything about it in the view. But anyway - EJBs should not be accessible in the view.

In short - you are using the correct approach - just use EL with the attributes that you've set.

Upvotes: 2

Related Questions