wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes 2 webs apps one class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "2 webs apps one class" Watch "2 webs apps one class" New topic
Author

2 webs apps one class

Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
hi guys i have a query

if i have 2 separate web apps , mapped to 2 separate urls eg

lousite.com/appone

lousite.com/apptwo

and both of these call the same Servlet class when hit (except the servlet exists in each webapp/WEB_INF/classes dir).

How many servlets are loaded in the container?

is the servlet loaded twice once from each webapp? and does the correct servlet service the correct web app then?

[ August 13, 2007: Message edited by: Louis Moloney ]

[ August 13, 2007: Message edited by: Louis Moloney ]
[ August 13, 2007: Message edited by: Louis Moloney ]

Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi,

1. put the servlet in one webapp.
Lets us say servletmapping url-pattern will be "/serviveFromAppOne"

example put it in appone.
lousite.com/appone

2. lousite.com/apptwo

2.1 declare a filter.
2.2 map it to the url-pattern "/serviveFromAppOne".
2.3 define filter such a way that it will redirect calls
"/appone/serviveFromAppOne".


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Louis!

Is it, this like situation?



And you access the servlet from the browser:

http://localhost:8084/rootcontext/Servlet.do

Thanks,


cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Srinivasan,

What do you think of the case I mentioned in just above post?



Thanks,
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

1. He is having two web-apps
2. wants to call the same servlet from both the web-apps.

like this:
lousite.com/appone/serviceFromAppOne
lousite.com/apptwo/serviceFromAppOne

I wunderstood his query like this.

How you got ?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
In this way the servlet contexts would be different. So there should not be a
problem because they would be treated as two different servlet (resource).
One would be /appOne and another /appTwo.

You took some redirecting example to forward the request to particular
servlet (resource) (It is Front Controller, Right?).

Is it like that? You please consider my case too. Sorry for hijacking but
I think the problem could be taken in that way too.

My case assumes it is on same web application.

Thanks,
[ August 13, 2007: Message edited by: Chandra Bhatt ]
Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
To the original post, I think two instances of the servlet will be loaded once for each webapp.

Chandra,
In your case when the container finds the first exact match, it will stop, it will ignore the second exact match. So Servlet1 will service the request.

[Christophe : Unnecessary comment removed]

[ August 13, 2007: Message edited by: Promod Mahajan ]
[ August 13, 2007: Message edited by: Christophe Verre ]
Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
Chandra,
I tried your scenario on Tomcat and Servlert2 is servicing the request. So what I said early on without trying it is not right. I would like someone else to try this and confirm. Here is my code

Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
If I reverse the order in web.xml, Servlet1 is servicing the request. Basically the second one overrides the first one or the last one overrides the previous ones.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

and both of these call the same Servlet class when hit (except the servlet exists in each webapp/WEB_INF/classes dir)

If you are accessing the same class, but in different contexts, this is not the same servlet. The container will hold a different instance of the servlet in each context.


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Promod,
Please be careful about your comments. I have removed an unnecessary comment from one of your posts.
This kind of comment is not quite appreciated here. Thank you for your comprehension.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Louis Moloney,

Please respond to replies of your post.
Is it a test for us ?
or
you are facing some problem ?
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
I wanted to see if I had 2 webapps
/app1
/app2

both of which have a Servlet (MyServlet.class) in webapp/WEB-INF/classes that was compiled from the same source code (MyServlet.java).

I am using boths apps (eg app1/getMyServlet , app2/getMyServlet).
What i wanted to know is does the container instantiate 2 instances of MyServlet or 1 ?

and if 2 made are they serviced by the correct instance for the webapp.
Christophe Verre and Promod Mahajan have understood this best

ps

Srinivasan I replied this morning 1st thing when i saw the posts
[ August 14, 2007: Message edited by: Louis Moloney ]
Baiju Varugese
Ranch Hand

Joined: Dec 16, 2003
Posts: 74
Hi,

Srinivasan thoyyeti
I feel Thats container Dependant.

Yes it is container dependent. JBoss uses Unified Class Loader, which loads class only once. We can disable this also.


Srinivasan thoyyeti
I don't see any problem in creating Only one instance re-using the same for other web-apps also.

But there are problems. If the servlet reading any context/initial values it wont differ for different application context. It will be same for the server.


Measure twice cut once carpenter theory <br /> <br />Baiju <br />SCJCP, SCBCD, IBM-486, IBM-484, SCEA Part I
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Baiju:
I have deleted my previous post.
I felt it very bad.

But there are problems. If the servlet reading any context/initial values it wont differ for different application context. It will be same for the server.


Yes you are right on target.

1. I don't know whether container can handle this scenario with single servlet instance. I didn't ran this scenario yet

2.But the best thing for container is to have one instance of servlet per context.

3.But I am sure one aspect.If I need one servlet re-used for all web-apps, then I am gonna configure it in one web-app context.

and in all other web-apps I will use a filter to redirect calls to that servlet.

[ August 14, 2007: Message edited by: Srinivasan thoyyeti ]
[ August 14, 2007: Message edited by: Srinivasan thoyyeti ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Baiju Varugese:
Hi,

Srinivasan thoyyeti
I feel Thats container Dependant.

Yes it is container dependent. JBoss uses Unified Class Loader, which loads class only once. We can disable this also.


Srinivasan thoyyeti
I don't see any problem in creating Only one instance re-using the same for other web-apps also.

But there are problems. If the servlet reading any context/initial values it wont differ for different application context. It will be same for the server.


But, what if there are classes in two *different* web application residing in the same container happen to have the *same name* ?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Raghavan,

Please remove my comments.
It doesn't sound good to myself.
Oricio Ocle
Ranch Hand

Joined: Nov 30, 2004
Posts: 284

Originally posted by Raghavan Muthu:


But, what if there are classes in two *different* web application residing in the same container happen to have the *same name* ?


Literally form j2ee 1.4 specification:
Any J2EE product must be able to accept a J2EE application delivered as a .ear file or a stand-alone J2EE module delivered as a .jar,.war, or .rar file (as appropriate to its type). If the application is delivered as a .ear, an enterprise bean module devliered as a .jar file, or a web application delivered as a .war file, the deployment tool must be able to deploy the application such that the Java classes in the application are in a separate namespace from classes in other Java applications. Typically this will require the use of a separate class loader for each application.


SCJP, OCMJD, OCMJEA
kieran duggan
Greenhorn

Joined: Aug 24, 2007
Posts: 1
Originally posted by Louis Moloney:
[qb]hi guys just lookin for an old freind called louis moloney who lived in Ireland.


[ August 24, 2007: Message edited by: kieran duggan ]
[ August 24, 2007: Message edited by: kieran duggan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2 webs apps one class
 
Similar Threads
Confusion about autoboxing
Overriding Clarification
Method which isn't a constructor should have a return type
Default servlet context
Generics Question: