zoukankan      html  css  js  c++  java
  • Tomcat

    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!
  • 相关阅读:
    Leetcode 16.25 LRU缓存 哈希表与双向链表的组合
    Leetcode437 路径总和 III 双递归与前缀和
    leetcode 0404 二叉树检查平衡性 DFS
    Leetcode 1219 黄金矿工 暴力回溯
    Leetcode1218 最长定差子序列 哈希表优化DP
    Leetcode 91 解码方法
    Leetcode 129 求根到叶子节点数字之和 DFS优化
    Leetcode 125 验证回文串 双指针
    Docker安装Mysql记录
    vmware虚拟机---Liunx配置静态IP
  • 原文地址:https://www.cnblogs.com/huey/p/4780397.html
Copyright © 2011-2022 走看看