`
cobo85
  • 浏览: 115044 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ubuntu netbeans 内嵌式tomcat

阅读更多

ubuntu下安装jdk和netbeans就不多说了,今天把eclipse里面的项目转到netbeans下感觉也不错,贴上来分享下经验 :D 。不多说了看图吧。

   1,建立起项目,可以手工建立也可以从eclipse里面导入
   2,打开属性对话,选择源选项---》在源包文件夹下添加所有的项目文件夹,它相当于eclipse里面“用户环境”

  3,选择运行选项--》主类里面的内嵌tomcat的启动类, VM选项,配置内嵌tomcat的启动参数:如

  -Dwebroot.path=/home/sky/workspace/***/src/web/root -Dpath=/test   -Dserver.port=9000  -Dreloadable=true

4,内嵌tomcat的启动类如下:

public class EmbeddedTomcat {

    private String webrootPath;    //WEB应用程序路径
    private String contextPath;    //WEB上下文名称
    private boolean reloadable;   //是否允许热交换class
    private int port;//应用程序服务端口

    private Embedded tomcat;     //嵌入式Tomcat

    public EmbeddedTomcat() {
    	    
    	webrootPath = System.getProperty("webroot.path");
        contextPath = System.getProperty("path");
        reloadable = Boolean.valueOf(System.getProperty("reloadable"));
        port=Integer.parseInt(System.getProperty("server.port"));
    }

    /**
     * 启动服务
     */
    public void startup() throws Exception {

        tomcat = new Embedded();
        Engine engine = tomcat.createEngine();
        tomcat.setCatalinaHome(webrootPath);

        Host host = tomcat.createHost("localhost", webrootPath);
        Context context = tomcat.createContext(contextPath, webrootPath);
        if(reloadable) context.setReloadable(true);
        host.addChild(context);
        engine.addChild(host);
        engine.setDefaultHost(host.getName());

        engine.setName("EmbeddedServer");
        tomcat.addEngine(engine);
        Connector connector =
            tomcat.createConnector(InetAddress.getByName("0.0.0.0"),port, false);
        connector.setURIEncoding("GBK");
        tomcat.addConnector(connector);
        tomcat.start();
    }

    /**
     *停止服务
     */
    public void shutdown() throws Exception {
        tomcat.stop();
    }

    public static void main(String[] args) {
        try {
            new EmbeddedTomcat().startup();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

     具体的jar包可以从tomcat的官网下载。好了,现在运行上面的启动代码,看下效果。ps:在ubuntu下贴图真不方便,截图就不传了,呵呵。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics