Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

servlet access to another context

Thread view: 
After - 06 Nov 2007 13:24 GMT
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
Juha Laiho - 06 Nov 2007 16:57 GMT
After <support@afterbit.com> 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) ?

See http://tomcat.apache.org/tomcat-6.0-doc/config/context.html ;
crossContext attribute for context might be what you're looking
for.
Signature

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)

After - 07 Nov 2007 17:12 GMT
> After <supp...@afterbit.com> said:
>
[quoted text clipped - 12 lines]
>          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!
Lew - 08 Nov 2007 01:41 GMT
> 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.

Signature

Lew

After - 08 Nov 2007 15:02 GMT
> > Can I do this inside a javabean too?? (ServletContext is not available
> > in a bean... ??) .. In other words.. How can i make available a
[quoted text clipped - 5 lines]
> --
> Lew

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!!
Lew - 08 Nov 2007 15:41 GMT
> 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

Arne Vajhøj - 07 Nov 2007 02:38 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.