> My problem is to call a servlet from a method of a javabean.. i.e:
If the servlet calls the bean's method(s), directly or indirectly, then the
bean should not call the servlet's. The servlet should retrieve the result of
the bean method and do all the forwarding itself.
Having the bean do servlet things is very bad design. It's especially bad
having it do navigation things. Do not have the bean do servlet things.
public class Controller extends HttpServlet
{
protected void doPost( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
{
Map parms = request.getParameterMap();
Result res = doSomething( parms );
request.setAttribute( "result", res );
RequestDispatcher rd;
switch( res.getStatus() )
{
case SUCCESS:
rd = request.getRequestDispatcher( "/success.jsp" );
break;
default:
case FAILURE:
rd = request.getRequestDispatcher( "/failure.jsp" );
break;
}
rd.forward( request, response );
}
}

Signature
Lew
After - 12 Nov 2007 11:13 GMT
> Having the bean do servlet things is very bad design. It's especially bad
> having it do navigation things. Do not have the bean do servlet things.
you are right..I know that this is a bad design ... but my schema is :
JSP page --calls-> Servlet --calls--> JSP page ..
this JSP page calls a javabean and this javabean should call a servelt
(do somethig) .. and finally the JSP page display itself!
it is why i have to call a servlet inside a javabean method .. so i
have to allow access to ServletContext inside the javabean.. so it can
call the servlet and get its results!!
Lew - 12 Nov 2007 14:45 GMT
>> Having the bean do servlet things is very bad design. It's especially bad
>> having it do navigation things. Do not have the bean do servlet things.
[quoted text clipped - 9 lines]
> have to allow access to ServletContext inside the javabean.. so it can
> call the servlet and get its results!!
False. You do not call a servlet from the bean. You call the bean from the
servlet, then embed it in a request attribute and call it from the destination
JSP.

Signature
Lew
Lew - 12 Nov 2007 14:47 GMT
After wrote:
>> it is why i [sic] have to call a servlet inside a javabean method .. so i [sic]
>> have to allow access to ServletContext inside the javabean.. so it can
>> call the servlet and get its results!! [sic]
> False. You do not call a servlet from the bean. You call the bean from
> the servlet, then embed it in a request attribute and call it from the
> destination JSP.
Did you look at the code example I provided for how the servlet would do that?
On the JSP side you use Expression Language or <jsp:useBean> to retrieve the
bean, which would have some sort of getResult() for the JSP to use to get the
result.

Signature
Lew