1. Create Your JavaBean Class
Create the JavaBean class which will be instantiated each time that the resource factory is looked up. For this example, assume you create a class com.huey.hello.bean.HelloBean
, which looks like this:
package com.huey.hello.bean; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @NoArgsConstructor public class HelloBean { @Getter @Setter private String personName; public String sayHello() { if (personName != null && personName.length() > 0) { return "Hello, " + personName + "!"; } return "Hello, world!"; } }
2. Declare Your Resource Requirements
Next, modify your web application deployment descriptor (/WEB-INF/web.xml
) to declare the JNDI name under which you will request new instances of this bean. The simplest approach is to use a <resource-env-ref>
element, like this:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>helloServlet</servlet-name> <servlet-class>com.huey.hello.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloServlet</servlet-name> <url-pattern>/hello.html</url-pattern> </servlet-mapping> <resource-env-ref> <resource-env-ref-name>bean/HelloBeanFactory</resource-env-ref-name> <resource-env-ref-type>com.huey.hello.bean.HelloBean</resource-env-ref-type> </resource-env-ref> </web-app>
3. Code Your Application's Use Of This Resource
A typical use of this resource environment reference might look like this:
package com.huey.hello.servlet; import java.io.IOException; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.huey.hello.bean.HelloBean; public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 2684083672082632268L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); HelloBean helloBean = (HelloBean) envCtx.lookup("bean/HelloBeanFactory"); resp.getWriter().println(helloBean.sayHello()); } catch (Exception e) { e.printStackTrace(); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
4. Configure Tomcat's Resource Factory
To configure Tomcat's resource factory, add an element like this to the <Context>
element for this web application.
<Resource name="bean/HelloBeanFactory" auth="Container" type="com.huey.hello.bean.HelloBean" factory="org.apache.naming.factory.BeanFactory" personName="huey"/>
Note that the resource name (here, bean/HelloBeanFactory
must match the value specified in the web application deployment descriptor. We are also initializing the value of the personName
property, which will cause setPersonName("huey")
to be called before the new bean is returned.
5. Verification
Send a http GET request to http://localhost:8080/hellotomcat/hello.html.
C:Usershuey>curl http://localhost:8080/hellotomcat/hello.html Hello, huey!