web项目嵌入Jetty运行的两种方式(Jetty插件和自制Jetty服务器)

  在开发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>
时间: 2024-08-09 04:09:26

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

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

从PB8.0/9.0开始,就已经提供Web Service Proxy功能,能够直接进行相关程序的编写. 但是,部分老项目使用PB6.5开发 研究后发现,其实PB6.5要操作Web Service也挺容易. 说明:1.本例子用于pb对Web Service的操作.       2.pb可使用两种方式实现.   3.proxy方式使用pb9自带的Web Service Proxy功能实现,需要将sharedPowerBuilderpbsoapclient90.pbd包含到应用中来.   4.OLE

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

下面我将介绍使用maven构建的SpringBoot项目中实现热部署的两种方式,使得部署变得异常简单,同时两种方式也非常的简单. 热部署 devtools Pom.xml中直接添加依赖即可: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>provided</

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

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

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

在网站开发中,数据库操作是经常要用到的操作,ASP.NET中一般做法是在web.config中配置数据库连接代码,然后在程序中调用数据库连接代码,这样做的好处就是当数据库连接代码需要改变的时候,我们只要修改web.config中的数据库连接代码即可,而不必在修改每一个页面中的数据库连接代码. 在ASP.Net中有两种配置数据库连接代码的方式,它们分别是 appSettings 和 connectionStrings .在使用 appSettings 和 connectionStrings 配置数

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

参考大家导出的方式,基本上是如下两种: 1.使用 html2Canvas + jsPDF 导出PDF, 这种方式什么都好,就是下载的pdf太模糊了.对要求好的pdf这种方式真是不行啊! 2.调用浏览器自身的方法.window.print() 来打印(打印时可选下载),这种方式打印出来很清楚,但纯在浏览器兼容问题. 谷歌浏览器比较好用点. 两种导出pdf清晰度对比: --------------左边 html2canvas + jspdf:-----------------------------

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

 1  第一种方式是:在连网的情况下,通过在helpàInstall下安装. 新的地址是:http://download.eclipse.org/technology/m2e/releases 2  第二中方式是: 通过插件配置的方式进行安装插件. A  将maven所需的插件放到maven的安装目录下,截图如下: B 在Eclipse的dropins目录下创建以下三个插件: 三个文件中的内容分别是: m2e.txt path=E:/Installed/apache-maven-3.1.0/

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

1.创建maven web项目 2. 配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0

Web应用解决中文乱码的两种方式

(1)第一种:常用的是我们自定义的过滤器 package com.lc.filter; //ctrl+shift+o 导入各种包 即可 /** * 过滤器的使用方法 * 在创建类的时候 要用到接口Filter * 要自己在web.xml文件中配置过滤器 * @author xuliugen * */ public class MyFilter extends HttpServlet implements Filter { private String encoding=null; public

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

以本机环境为例,ip地址为172.20.16.148,windows平台,虚拟环境路径为d:\VirtualEnv,项目存放位置为d:\DjangoProject 命令行方式 1.进入虚拟环境创建项目django-admin startproject projectname 项目的存放位置为django-admin执行时的路径,因此要在创建项目之前创建和进入项目文件存放的目录. 执行后会在d:\DjangoProject目录下形成一个first_project文件夹,文件夹下包含一个与项目名称相