lixw 发表于 2013-1-14 23:07:08

什么都要嵌入式

1、Derby数据库
简介:
       Derby是一个纯Java开发的关系型数据库引擎。
使用:
      加载驱动:Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
      获取连接,执行查询、更新,开启、提交、回滚事务等操作完全符合JDBC规范,这里不做赘述。
组件包:
      derby.jar
2、Jetty服务器
简介:
      Jetty是一个开源的Servlet容器。
使用:
      创建Server:Server httpServer = new Server();
      创建连接器:SelectChannelConnector connector = new SelectChannelConnector();
      设置http内部端口:connector.setPort(8008);
                                    //connector.setHost("127.0.0.1");
                                    httpServer.addConnector(connector);
      创建WEB应用程序上下文: WebAppContext context = new WebAppContext();
      加载java Web项目:context.setContextPath("/pms");  
                                      context.setWar("./pms.war");
      //web项目路径
      //context.setResourceBase("./"); //相对路径
      httpServer.setHandler(context);//设置处理器器
      httpServer.setStopAtShutdown(true);//设置是否同时停止服务器线程,默认为false
      启动服务器:
try {   httpServer.start();   System.out.println("HTTPServer服务启动成功");} catch (Exception e) {   e.printStackTrace();   System.out.println("HTTPServer服务启动异常:" + e);   System.exit(-1);} 上面的硬编码比较繁琐,我们同样可以采用XML来配置。
配置文件格式(jetty.xml):
 
    <?xml version="1.0"?>    <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">    <Configure id="Server" class="org.mortbay.jetty.Server">      <Set name="ThreadPool">          <New class="org.mortbay.thread.BoundedThreadPool">            <Set name="minThreads">10</Set>            <Set name="lowThreads">50</Set>            <Set name="maxThreads">250</Set>          </New>      </Set>         <Call name="addConnector">          <Arg>            <New class="org.mortbay.jetty.nio.SelectChannelConnector">                <Set name="port"><SystemProperty name="jetty.port" default="8080"/></Set>                <Set name="maxIdleTime">30000</Set>                <Set name="Acceptors">2</Set>                <Set name="confidentialPort">8443</Set>            </New>          </Arg>      </Call>            <Call name="setStopAtShutdown">         <Arg type="boolean">true</Arg>      </Call>      <Set name="handler">          <New id="handlers" class="org.mortbay.jetty.handler.HandlerCollection">            <Set name="handlers">             <Array type="org.mortbay.jetty.Handler">               <Item>               <New id="contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection"/>               </Item>               <Item>               <New id="defaultHandler" class="org.mortbay.jetty.handler.DefaultHandler"/>               </Item>               <Item>               <New id="requestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>               </Item>             </Array>            </Set>          </New>      </Set>            <New id="pms" class="org.mortbay.jetty.webapp.WebAppContext">          <Arg><Ref id="contexts"/></Arg>          <Arg>D:\\apache-tomcat-5.5.23\\pms</Arg>          <Arg>/pms</Arg>          <Set name="classLoader">            <New class="org.mortbay.jetty.webapp.TransformingWebAppClassLoader">            <Arg><Ref id="pms"/></Arg>            </New>          </Set>          <Set name="virtualHosts">            <Array type="java.lang.String">            <Item>169.254.11.139</Item>            </Array>          </Set>               <Get name="SessionHandler">            <Set name="SessionManager">            <New class="org.mortbay.jetty.servlet.HashSessionManager">                <Set name="maxInactiveInterval">600</Set>            </New>            </Set>          </Get>      </New>    </Configure> 读取XML配置参数,启动服务器:
try{   Server server = new Server();   XmlConfiguration configuration = new XmlConfiguration(Main.class.getClassLoader().getResource("jetty.xml"));    configuration.configure(server);   server.start();   }catch(Exception e){   e.printStackTrace();   System.exit(-1);} 组件包:
       jetty-6.0.0.jar
       jettty-util-6.0.0.jar
      servlet-api-2.5-6.0.0.jar
3、虚拟机JRE
      要真正实现嵌入式虚拟机,需要我们的应用程序中同时包含JRE(从SUN官方可以获得,一般比较大,可以进行精简),然后利用批处理或者shell脚本的方式运行,例如在windows上的一个批处理:
jre\bin\java -cp .;lib\ant-1.6.5.jar;lib\commons-logging-1.0.4.jar;lib\core-3.1.1.jar;lib\derby.jar;lib\jetty-6.0.0.jar;lib\jetty-util-6.0.0.jar;lib\jsp-2.1.jar;lib\jsp-api-2.1.jar;lib\jstl-1.1.2.jar;lib\pms.jar;lib\servlet-api-2.5-6.0.0.jar;lib\standard-1.0.6.jar cn.com.Main
页: [1]
查看完整版本: 什么都要嵌入式