开发者

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 preferre开发者_如何转开发d way to implement the Model View controller?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜