servlet access to another context

A

After

I have two different context in Tomcat 5. I use Tomcat to develop JSP
pages
Is it possible inside a servlet in a <Context> to call a different
servlet in another <Context>?? .. run the servlet in the second
<context> and get back data (i.e in the request) ?

for example:
one context is :
\webapps\application1 (here resides serveltContext1.java)
the second context is :
\webapps\application2 (here resides serveltContext2.java)

i would like to do something like this:


serveltContext1 {

call to serveltContext2
running of serveltContext2 {
// do something
}

get data from serveltContext2 (i.e. stored in the request)

}

Thanks in advance
Roby
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

After said:
I have two different context in Tomcat 5. I use Tomcat to develop JSP
pages
Is it possible inside a servlet in a <Context> to call a different
servlet in another <Context>?? .. run the servlet in the second
<context> and get back data (i.e in the request) ?

A primitive but easy solution would be to use (Http)URLConnection
to request the servlet and just parse the output.

Arne
 
A

After

Seehttp://tomcat.apache.org/tomcat-6.0-doc/config/context.html;
crossContext attribute for context might be what you're looking
for.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)



Ok.. i have read the documentation about Context..
Now I found a solution.. so i can call a servelt of another context..
I want to pass Object using ServletContext of the servlet .... and it
seems that it works!!

Now the question is :

Can I do this inside a javabean too?? (ServletContext is not available
in a bean... ??) .. In other words.. How can i make available a
ServletContext in a generic bean???

Thanks!
 
L

Lew

After said:
Can I do this inside a javabean too?? (ServletContext is not available
in a bean... ??) .. In other words.. How can i make available a
ServletContext in a generic bean???

You could just pass it in, but it makes more sense just to pass in the
specific items out of the context that you need in the bean.
 
A

After

You could just pass it in, but it makes more sense just to pass in the
specific items out of the context that you need in the bean.

My problem is to call a servlet from a method of a javabean.. i.e:

class aBean... {
public doCallServlet() {

// teh following statements give an error!!!
ServletContext sc = application.getContext("/context2");
RequestDispatcher rd = sc.getRequestDispatcher("/index.jsp");
rd.forward(request, response);

}
}

but the code above is wrong!!!
do you suggest to pass the ServletContext as a parameter of the method
doCallServlet().
Any other solution!?!?

Thanks!!
 
L

Lew

After said:
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 );
}
}
 
A

After

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!!
 
L

Lew

After said:
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!!

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.
 
L

Lew

After said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top