在开发Java web项目时候,可以在项目中嵌入Jetty服务的方式来运行web程序。

  由于最近开发web项目,自己使用的是比较旧的eclipse不支持导入tomcat来运行项目,于是就学习了下使用项目中Jetty来运行项目。

  • 采用Jetty Plugin

      在pom文件中引入Jetty Plugin配置即可:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>

    <build>

            <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
    <source>1.7</source>
    <target>1.7</target>
    <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
    </plugin> <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.1.15.v20140411</version>
    <configuration>
    <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <webAppConfig>
    <contextPath>/web</contextPath> //你自己的工程名称
    </webAppConfig>
    <connectors>
    <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
    <port>8080</port>
    <maxIdleTime>60000</maxIdleTime>
    </connector>
    </connectors>
    <contextHandlers>
    <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
    <resourceBase>${project.parent.basedir}/src/main/webapp</resourceBase>
    <contextPath>/web</contextPath>
    </contextHandler>
    </contextHandlers>
    </configuration>
    </plugin> </plugins>
    </build>

    运行时候只要maven build->输入jetty:run即可。

    这种插件方式运行的项目不支持@ServerEndpoint websocket功能,不知道是不是我这种方式使用问题还是什么,知道的指导下。我下面自制JettyServer就可以支持websocket,目前我开发的时候采用的方式。

  • 自制Jetty服务类

      这种方式可以支持websocket,如果项目中需要使用到可以试试这种。

      首先pom.xml引入jetty的依赖:

          <dependency>
    <groupId>org.eclipse.jetty.aggregate</groupId>
    <artifactId>jetty-all</artifactId>
    <version>9.2.14.v20151106</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    </dependency>
    <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-jsp</artifactId>
    <version>9.2.15.v20160210</version>
    </dependency>

     接下来自己写一个JettyServer类:我自己的完整代码如下

    import javax.websocket.server.ServerContainer;
    
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.webapp.WebAppContext;
    import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
    import org.json.JSONObject; import com.web.test.MyWebSocket; public class JettyServer {
    public static void main(String[] args) {
    int port = 8080;
    Server server = new Server(port);
    WebAppContext webAppContext = new WebAppContext("webapp","/web"); webAppContext.setDescriptor("webapp/WEB-INF/web.xml");
    webAppContext.setResourceBase("src/main/webapp");
    webAppContext.setDisplayName("web");
    webAppContext.setClassLoader(Thread.currentThread().getContextClassLoader());
    webAppContext.setConfigurationDiscovered(true);
    webAppContext.setParentLoaderPriority(true); server.setHandler(webAppContext);
    System.out.println(webAppContext.getContextPath());
    System.out.println(webAppContext.getDescriptor());
    System.out.println(webAppContext.getResourceBase());
    System.out.println(webAppContext.getBaseResource()); try {
    ServerContainer wscontainer = WebSocketServerContainerInitializer.configureContext(webAppContext);
    // Add WebSocket endpoint to javax.websocket layer
    wscontainer.addEndpoint(MyWebSocket.class); //这行是如果需要使用websocket就加上,不需要就注释掉这行,mywebsocket是自己写的websocket服务类 server.start();
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println("server is start, port is "+port+"............");
    } }

    运行项目就只要运行这个main函数即可。

    假如正式发布需要放到tomcat里运行,需要把下面这个依赖去掉,tomcat和下面的依赖不兼容,会报错(javax.servlet.ServletException: Not running on Jetty, JSR-356 support unavailable)

    这种方式运行项目可以在开发的时候用用

  • <dependency>
    <groupId>org.eclipse.jetty.aggregate</groupId>
    <artifactId>jetty-all</artifactId>
    <version>9.2.14.v20151106</version>
    </dependency>

web项目嵌入Jetty运行的两种方式(Jetty插件和自制Jetty服务器)的更多相关文章

  1. pb对Web Service的操作可使用两种方式实现

    从PB8.0/9.0开始,就已经提供Web Service Proxy功能,能够直接进行相关程序的编写. 但是,部分老项目使用PB6.5开发 研究后发现,其实PB6.5要操作Web Service也挺 ...

  2. 基于Maven的SpringBoot项目实现热部署的两种方式

    转载:http://blog.csdn.net/tengxing007/article/details/72675168 前言 JRebel是JavaEE中比较流行的热部署插件,可快速实现热部署,节省 ...

  3. Asp.net Web API 返回Json对象的两种方式

    这两种方式都是以HttpResponseMessage的形式返回, 方式一:以字符串的形式 var content = new StringContent("{\"FileName ...

  4. 5.Maven和Eclipse整合(两种方式进行插件的安装),Maven相关设置,Eclipse下创建Maven项目

     1  第一种方式是:在连网的情况下.通过在helpàInstall下安装. 新的地址是:http://download.eclipse.org/technology/m2e/releases 2 ...

  5. vue项目中导出PDF的两种方式

    参考大家导出的方式,基本上是如下两种: 1.使用 html2Canvas + jsPDF 导出PDF, 这种方式什么都好,就是下载的pdf太模糊了.对要求好的pdf这种方式真是不行啊! 2.调用浏览器 ...

  6. springmvc+maven搭建web项目之二 通过另一种方式配置spring

    1.创建maven web项目 2. 配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...

  7. web.config中配置数据库连接的两种方式

    在ASP.NET中有两种配置数据库连接代码的方式,它们分别是 appSettings 和 connectionStrings .在使用 appSettings 和 connectionStrings ...

  8. 【IDEA】向IntelliJ IDEA创建的项目导入Jar包的两种方式

    转载请注明出处:http://blog.csdn.net/qq_26525215 本文源自[大学之旅_谙忆的博客] 今天用IDEA,需要导入一个Jar包,因为以前都是用eclipse的,所以对这个id ...

  9. 第一个django项目-通过命令行和pycharm两种方式

    以本机环境为例,ip地址为172.20.16.148,windows平台,虚拟环境路径为d:\VirtualEnv,项目存放位置为d:\DjangoProject 命令行方式 1.进入虚拟环境创建项目 ...

随机推荐

  1. 偶然发现关于网页JavaScript脚本无法正常运行的原因

    客户常常打电话投诉公司的销售系统有问题, 后来发现有的客户直接把网址设为受限网站,才导致系统无法正常执行.改动后正常.

  2. Android-L-Samples

    https://github.com/s3xy4ngyc/Android-L-Samples

  3. 支持向量机(SVM)非线性数据切割

    支持向量机(SVM)非线性数据切割 1.目标 本指导中你将学到: l  当不可能线性切割训练数据时,如何定义SVM最优化问题. l  在这样的问题上.如何配置CvSVMParams中的參数满足你的SV ...

  4. Tomcat安装、配置、优化及负载均衡详解

    一.常见JavaWeb服务器      1.WebLogic:是BEA公司的产品.WebSphereAS:是IBM公司的产品.JBossAS:红帽公司的产品,可以自行了解      2.Tomcat服 ...

  5. Line Search and Quasi-Newton Methods

    Gradient Descent 机器学习中很多模型的参数估计都要用到优化算法,梯度下降是其中最简单也用得最多的优化算法之一.梯度下降(Gradient Descent)[3]也被称之为最快梯度(St ...

  6. apache 配置order allow deny讲解

    http://www.111cn.net/phper/apache/43025.htm

  7. MySQL(19):SQL语句(MySQL)大全

    SQL语句大全一.创建和删除数据库 1.创建用户 //创建用户且置密码,在MySQL中行,但在Oracle中行  ----必须在超级管理员身份下操作 create user hncu identifi ...

  8. Android(java)学习笔记198:Android下的逐帧动画(Drawable Animation)

    1.帧动画: 帧动画顾名思义,一帧一帧播放的动画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在相同区域快速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,其实不过是N张图片在一帧一帧 ...

  9. Android(java)学习笔记85:案例拨号程序

    手机拨号程序:(只有程序代码) package cn.itcast.phone; import android.app.Activity; import android.content.Intent; ...

  10. Android(java)学习笔记83:finally关键字的作用

    package cn.itcast_07; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...