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!
  • 相关阅读:
    Redis 发布与订阅
    Redis 数据持久化的理解
    自定义shell脚本快速搭建LNMP环境
    PHP环境配置与优化(Ubuntu16.04/PHP7)
    记录在ios系统上,自研app,灰度环境遇到的一个vue页面dom节点已渲染,但是显示部分空白的情况
    记录在苹果6p/6sp,10版本上,app内交互token等用户信息丢失的问题
    记录在苹果X手机上运行遇到的代码Dom被阻塞不更新的一个坑
    巧用flex(一)
    谷歌浏览器调试手机app内置网页
    与app交互因异步造成的坑记录
  • 原文地址:https://www.cnblogs.com/huey/p/4780397.html
Copyright © 2011-2022 走看看