You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Roberto Bottoni (Modernviaggi)" <ro...@modernviaggi.com> on 2007/04/04 19:08:26 UTC

Share Bean between two webapps

I spend several hours to do this :
I want to share a javabean between two different webapps .... i want to get
fields and methods of a javabean that is stored in webapp2 from webapps1
I use Tomcat 5.0 and i have read
http://tomcat.apache.org/tomcat-5.0-doc/jndi-resources-howto.html but i get
no success!
Please.. do you have a simple example??.. that is.. code and configurations
(server.xml, web.xml, context.xml...)

follow my codes...
jndiWebShare1 is the folder under tomcat\webapps of webapp1
--------------------------------------------------------------------------
SharedBean1.java .. . under jndiWebShare1\WEB-INF\com\afterbit
---------------------------------------------------------------------------
/*
 * SharedBean1.java
 */

package com.afterbit;

/**
 *
 * @author Admin
 */
public class SharedBean1 {

    /** Creates a new instance of SharedBean1 */
    public SharedBean1() {
    }

    private String foo = "Shared Bean 1 : Value of FOO";
    private int bar = 0;

    public String getFoo() {
        return (this.foo);
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public int getBar() {
        return (this.bar);
    }

    public void setBar(int bar) {
        this.bar = bar;
    }


}


--------------------------------------------------------------------------
context.xml .. .under  jndiWebShare1\META-INF
---------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/jndiWebShare1" docBase="webapps/jndiWebShare1"
crossContext="true" debug="0" reloadable="true">
   <Resource name="bean/SharedBean1Factory" auth="Container"
            type="com.afterbit.SharedBean1"/>
   <ResourceParams name="bean/SharedBean1Factory">
      <parameter>
        <name>factory</name>
        <value>org.apache.naming.factory.BeanFactory</value>
      </parameter>
      <parameter>
        <name>bar</name>
        <value>23</value>
      </parameter>
    </ResourceParams>
</Context>

--------------------------------------------------------------------------
web.xml .. .under jndiWebShare1\WEB-INF
---------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<resource-env-ref>
  <description>Object factory for SharedBean1 instances.</description>
  <resource-env-ref-name>bean/SharedBean1Factory</resource-env-ref-
name>
  <resource-env-ref-type>com.afterbit.SharedBean1</resource-env-ref-
type>
</resource-env-ref>
</web-app>

--------------------------------------------------------------------------
index.jsp.. .JSP page with test bean under jndiWebShare1\
---------------------------------------------------------------------------
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import = "javax.naming.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
  </head>
  <body>
    <h1>JSP Page</h1>

    <%

    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    SharedBean1 bean = (SharedBean1) envCtx.lookup
("bean/SharedBean1Factory");
    out.println("foo = " + bean.getFoo() + ", bar = " + bean.getBar());

        %>
  </body>
</html>



but I get an exception...

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 17 in the jsp file: /index.jsp
Generated servlet error:
C:\jakarta-tomcat-5.0.27\work\Catalina\localhost\jndiWebShare1\org\apache\js
p\index_jsp.java:69: cannot resolve symbol
symbol  : class SharedBean1
location: class org.apache.jsp.index_jsp
    SharedBean1 bean = (SharedBean1)
envCtx.lookup("bean/SharedBean1Factory");

Of course if i put in my JSP page
<%@page import = "com.afterbit.*" %>  it works...but.. SharedBean1 should be
a shared resource.. so no need to import com.afterbit..... no?!!

Thanks!
Roberto


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Share Bean between two webapps

Posted by Martin Gainty <mg...@hotmail.com>.
Hi Roberto-

I think you may be speaking of demand loading multiple hierarchical 
container instances in which case I would look at implementing your app with 
Spring IOC (Injection of Control) and locate/allocate/init your bean via 
ContextSingletonBeanFactoryLocator class
http://www.springframework.org/docs/reference/beans.html

HTH,
Martin --
This email message and any files transmitted with it contain confidential
information intended only for the person(s) to whom this email message is
addressed.  If you have received this email message in error, please notify
the sender immediately by telephone or email and destroy the original
message without making a copy.  Thank you.

----- Original Message ----- 
From: "Roberto Bottoni (Modernviaggi)" <ro...@modernviaggi.com>
To: <us...@tomcat.apache.org>
Sent: Wednesday, April 04, 2007 1:08 PM
Subject: Share Bean between two webapps


>I spend several hours to do this :
> I want to share a javabean between two different webapps .... i want to 
> get
> fields and methods of a javabean that is stored in webapp2 from webapps1
> I use Tomcat 5.0 and i have read
> http://tomcat.apache.org/tomcat-5.0-doc/jndi-resources-howto.html but i 
> get
> no success!
> Please.. do you have a simple example??.. that is.. code and 
> configurations
> (server.xml, web.xml, context.xml...)
>
> follow my codes...
> jndiWebShare1 is the folder under tomcat\webapps of webapp1
> --------------------------------------------------------------------------
> SharedBean1.java .. . under jndiWebShare1\WEB-INF\com\afterbit
> ---------------------------------------------------------------------------
> /*
> * SharedBean1.java
> */
>
> package com.afterbit;
>
> /**
> *
> * @author Admin
> */
> public class SharedBean1 {
>
>    /** Creates a new instance of SharedBean1 */
>    public SharedBean1() {
>    }
>
>    private String foo = "Shared Bean 1 : Value of FOO";
>    private int bar = 0;
>
>    public String getFoo() {
>        return (this.foo);
>    }
>
>    public void setFoo(String foo) {
>        this.foo = foo;
>    }
>
>    public int getBar() {
>        return (this.bar);
>    }
>
>    public void setBar(int bar) {
>        this.bar = bar;
>    }
>
>
> }
>
>
> --------------------------------------------------------------------------
> context.xml .. .under  jndiWebShare1\META-INF
> ---------------------------------------------------------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <Context path="/jndiWebShare1" docBase="webapps/jndiWebShare1"
> crossContext="true" debug="0" reloadable="true">
>   <Resource name="bean/SharedBean1Factory" auth="Container"
>            type="com.afterbit.SharedBean1"/>
>   <ResourceParams name="bean/SharedBean1Factory">
>      <parameter>
>        <name>factory</name>
>        <value>org.apache.naming.factory.BeanFactory</value>
>      </parameter>
>      <parameter>
>        <name>bar</name>
>        <value>23</value>
>      </parameter>
>    </ResourceParams>
> </Context>
>
> --------------------------------------------------------------------------
> web.xml .. .under jndiWebShare1\WEB-INF
> ---------------------------------------------------------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
> <resource-env-ref>
>  <description>Object factory for SharedBean1 instances.</description>
>  <resource-env-ref-name>bean/SharedBean1Factory</resource-env-ref-
> name>
>  <resource-env-ref-type>com.afterbit.SharedBean1</resource-env-ref-
> type>
> </resource-env-ref>
> </web-app>
>
> --------------------------------------------------------------------------
> index.jsp.. .JSP page with test bean under jndiWebShare1\
> ---------------------------------------------------------------------------
> <%@page contentType="text/html"%>
> <%@page pageEncoding="UTF-8"%>
> <%@page import = "javax.naming.*" %>
>
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>   "http://www.w3.org/TR/html4/loose.dtd">
>
> <html>
>  <head>
>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
>    <title>JSP Page</title>
>  </head>
>  <body>
>    <h1>JSP Page</h1>
>
>    <%
>
>    Context initCtx = new InitialContext();
>    Context envCtx = (Context) initCtx.lookup("java:comp/env");
>    SharedBean1 bean = (SharedBean1) envCtx.lookup
> ("bean/SharedBean1Factory");
>    out.println("foo = " + bean.getFoo() + ", bar = " + bean.getBar());
>
>        %>
>  </body>
> </html>
>
>
>
> but I get an exception...
>
> org.apache.jasper.JasperException: Unable to compile class for JSP
>
> An error occurred at line: 17 in the jsp file: /index.jsp
> Generated servlet error:
> C:\jakarta-tomcat-5.0.27\work\Catalina\localhost\jndiWebShare1\org\apache\js
> p\index_jsp.java:69: cannot resolve symbol
> symbol  : class SharedBean1
> location: class org.apache.jsp.index_jsp
>    SharedBean1 bean = (SharedBean1)
> envCtx.lookup("bean/SharedBean1Factory");
>
> Of course if i put in my JSP page
> <%@page import = "com.afterbit.*" %>  it works...but.. SharedBean1 should 
> be
> a shared resource.. so no need to import com.afterbit..... no?!!
>
> Thanks!
> Roberto
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org