English 中文(简体)
从Servlet到JSP的转发
原标题:Forwarding from Servlet to JSP
  • 时间:2011-02-11 20:21:50
  •  标签:
  • jsp
  • el

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);  
}  

在index.jsp中:我从请求中获取用户,如果我使用scriptlets标记,我可以打印它的名称,但当我使用EL时,不会打印任何内容:

<@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?

问题回答

EL使用请求属性。您没有currUser作为请求属性。如果您尝试${user.name},它会起作用。

我不知道你说的“会话bean”是什么意思。EJB是会话bean,但是您没有在视图中显示任何关于它的内容。但是无论如何,EJB在视图中不应该是可访问的。

简而言之,您正在使用正确的方法,只需将EL与您设置的属性一起使用即可。





相关问题
Convert typed-in Text to lowercase

I ve got an index.jsp with [snip] <% String name = request.getParameter("name"); String pass = request.getParameter("pass"); String globalname = "webeng"; String globalpass = "2009"; ...

session transfer issue from Tomcat to ASP.Net

I am using Tomcat to host JSP and using IIS 7.0 to host aspx (C# + .Net 3.5 + VSTS 2008), and I have some session transfer issue from JSP page to ASPX page. JSP page is in one domain and all other ...

Setting the default value in Struts2

I am setting the value(kind of default value) for a drop down select value from action class in a page(given below). When the page loads the value is beig displayed but the other elements of the ...

Evaluate dynamically constructed JSP at runtime

I have a requirement where in the JSP page itself is created by the user and stored in the database. When viewing results we need to render this JSP to the client, evaluating all tags inside this JSP. ...

How to Pack/Encrypt/Unpack/Decrypt a bunch of files in Java?

I m essentially trying to do the following on a Java/JSP-driven web site: User supplies a password Password is used to build a strongly-encrypted archive file (zip, or anything else) containing a ...

JSP exception - class not found (tomcat)

I m setting up an existing application on a new Tomcat 5.5 server connecting to a Postgres database (running on Debian Lenny). When I access it I get a series of stack traces with the following root ...

ArrayList to Table in JSP

I have an ArrayList and i am trying to display it in a table ..... ArrayList rows = .... ..... <table cellspacing="1" cellpadding="4" border="3"> <tr> <TH>...

热门标签