• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Distributed applications

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this question on the Internet:


Which statements are true about distributed web applications?

1) There will be one instance of ServletContext in each VM
2) There will be only one instance of the default ServletContext in only one VM(the default ServletContext is one created for all servlets not deployed as part of an application are assigned)
3) All request that are part of a session must be handled by one VM at a time
4) Container must notify any session attributes implementing the HttpSessionActivationListener interface during migration of a session from one VM to another

According to the author, all the four options are correct. However I feel, only 1,2 and 4 should be correct. Requests which are part of a session can be handled by any number of JVMs, since the session migrates to whichever JVM contains the web resources required to satisfy the request. Please correct me if i am wrong.
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kanishka,

Requests which are part of a session can be handled by any number of JVMs, since the session migrates to whichever JVM contains the web resources required to satisfy the request. Please correct me if i am wrong.


Correct, in other words: when you have a distributed web-app: there is only one Session(-object) per session. So the request is either handled by this JVM or handled by another JVM(after migration).

Conclusion: a request is always handled by one JVM, but not necessarily the same JVM as the other requests. However a single request is never handled by two JVM's.

Regards,
Frits
 
Kanishka Ajwani
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frits, that made things clear. But now I have another question in mind; what happens when a servlet in JVM1, forwards to/includes another servlet in JVM 2? In this case, the same request will be handled by 2 JVMs, not at the same time though.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what happens when a servlet in JVM1, forwards to/includes another servlet in JVM 2? In this case, the same request will be handled by 2 JVMs, not at the same time though.


That is not possible.

There are two ways of forwarding:
  • Using the RequestDispatcher
  • Using the HttpServletResponse.sendRedirect() method

  • With the RequestDispatcher you can forward to another web-app in the same JVM, but not to another JVM!
    With the response.sendRedirect(), a new request is issued at the client side

    Regards,
    Frits
     
    Kanishka Ajwani
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Got it, thanks a lot.

    One more question, when <c:import url=""> is used in a jsp, the resource to be sought can be in any JVM, and any web-app, how does that work?
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3412
    320
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    One more question, when <c:import url=""> is used in a jsp, the resource to be sought can be in any JVM, and any web-app, how does that work?


    Yes, just try it. You can just include a whole (HTML) page from another server.

    Also have a look at the jstl1.1 spec:

    7.4 <c:import>



    Regards,
    Frits
     
    Ranch Hand
    Posts: 183
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Could you please mention the URL from where you got this question.
     
    Kanishka Ajwani
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Frits, my concepts are clear now.

    Simran, I found this question from the link : http://www.javaranch.com/carl/SCWCD.htm.
    Check out question no 45.
     
    Simran Dass
    Ranch Hand
    Posts: 183
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Frits wrote -

    With the RequestDispatcher you can forward to another web-app in the same JVM, but not to
    another JVM!




    But Frits using getRequestDispatcher you can dispatch a request to only the current web-app
    not any other web-app.Is not it so.


    This is from ServletRequest's method -

    public RequestDispatcher getRequestDispatcher(java.lang.String path):-


    The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.

     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3412
    320
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    But Frits using getRequestDispatcher you can dispatch a request to only the current web-app
    not any other web-app.Is not it so.


    If you obtain the RequestDispatcher from the ServletContext, you actually can:

    Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
    The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.



    Regards,
    Frits
     
    Simran Dass
    Ranch Hand
    Posts: 183
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Sorry.Should have read it carefully.

    Earlier I had tried out getContext() , should not have forgotten such an
    important thing.
     
    Bartender
    Posts: 543
    4
    Netbeans IDE Redhat Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This thread is a treasure trove of clarity in troubled waters. Thank you Frits!
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3412
    320
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    This thread is a treasure trove of clarity in troubled waters. Thank you Frits!


    Welcome

    Regards,
    Frits
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic