知识库--The Simple Container Application
2016-12-06 09:50
169 查看
The Simple Container Application
The main purpose of the application in this chapter is show how to use the default connector. It consists of two classes:
The SimpleContainer class implements org.apache.catalina.container so that it can be associated with the connector. The Bootstrap class is used to start the application, we have removed the connector module and the ServletProcessor and StaticResourceProcessor classes in the application accompanying 3.0, so you cannot request a static page.
Bootstrap class:
The main method associates the connector with the container by calling the connector’s setContainer method, passing the container. Next, it calls the connector’s initialize and start methods. This will make the connector ready for processing any HTTP request on port 8080.
You can terminate the application by pressing a key on the console.
Running the Application
java -classpath ./lib/servlet.jar;./….Bootstrap
The main purpose of the application in this chapter is show how to use the default connector. It consists of two classes:
The SimpleContainer class implements org.apache.catalina.container so that it can be associated with the connector. The Bootstrap class is used to start the application, we have removed the connector module and the ServletProcessor and StaticResourceProcessor classes in the application accompanying 3.0, so you cannot request a static page.
public class SimpleContainer implements Container { public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"; ...// other method omit @Override public void invoke(Request request, Response response) throws IOException, ServletException { String servletName = ((Httpservletrequest)request).getURI(); servletName = servletName.subString(servletName.lastIndexof("/")+1); URLClassLoader loader = null; try{ URL[] urls = new URL[1]; URLStreamHandler streamHandler = null; File classpath = new File(WEB_ROOT); String repository = (new URL("file",null,classpath.getCanonicalpath() + File.separator)).toString(); urls[0] = new URL(null,repository,streamHandler); loader = new URLClassLoader(urls); }catch(IOException e){ System.out.println(e.toString()); } Class myClass = null; try{ myClass = loader.loadclass(servletName); }catch(classNotFoundException e){ System.out.ptintln(e.toString()); } servlet servlet = null; try{ servlet = (Servlet)myClass.newInstance(); servlet.service((HttpServletRequest)request,(HttpServletResponse)response); }catch(Exception e){ ... }catch(Throwable e){ ... } } }
Bootstrap class:
public final class Bootstrap{ public static void main(String[] args){ HttpConnector connector = new HttpConnector(); SimpleContainer container = new SimpleContainer(); connector.setContainer(container); try{ connector.initialize(); connector.start(); System.in.read(); }catch(Exception e){ ... } } }
The main method associates the connector with the container by calling the connector’s setContainer method, passing the container. Next, it calls the connector’s initialize and start methods. This will make the connector ready for processing any HTTP request on port 8080.
You can terminate the application by pressing a key on the console.
Running the Application
java -classpath ./lib/servlet.jar;./….Bootstrap
相关文章推荐
- Killing container
- Kubernetes网络分析-Container间通信
- docker镜像,容器和存储驱动
- 用Harbor实现容器镜像仓库的管理和运维
- 6大Container OS介绍
- scottlowe-20160216谈论容器、虚拟化和Interop
- 运维人员的解放----Docker快速部署
- docker容器端口IP规划及端口动态扩容
- 初识Docker:了解image和container
- Windows Container技术的一些疑惑
- 操作系统对应用的包围圈在逐渐缩小,VM->Container->Unikernel
- Hermes Container Hack Day参赛回顾
- Hadoop运行因为container的内存大小而抛错
- Flex容器_Box_Label自动适应容器宽度问题
- Container With Most Water
- Red Hat Enterprise Linux 7部署Google容器管理工具Kubernetes
- Google容器管理工具Kubernetes简介
- Java进阶06 容器
- STL--Container共性。
- 弯道超车:容器技术究竟为云计算带来了什么?