美文网首页
Jetty 简单整理

Jetty 简单整理

作者: Nirvana丶_ | 来源:发表于2018-07-18 23:11 被阅读0次

简介

Jetty可以作为服务器像Tomcat一样,运行web项目,作为Servlet的容器。也可以在项目中单独引用Jetty的Jar包,通过Java代码来对web项目进行启动。这样做的目的主要是对多个web项目进行统一的部署启动。
这里主要使用的方法是第二种,即在一个Java项目中托管web项目,第一种方式未进行过测试。

Maven 依赖

我试过手动引用jar包,但是都不太好用,最后还是通过这个Maven依赖解决的,可能这个依赖有很多重复的,但至少比网上的那些好用。
经过后来的测试,将这些依赖文件的Jar包放到本地,依旧好用。
补充一下项目中pom.xml的创建方式,我使用的方法是通过idea直接创建的Maven项目。
一下是项目的Maven配置:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- Jetty -->
        <dependency>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>jetty-all</artifactId>
            <version>8.0.4.v20111024</version>
        </dependency>

        <!-- Jetty Webapp -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>8.0.4.v20111024</version>
        </dependency>

        <!-- JSP Support -->
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.servlet.jsp</artifactId>
            <version>2.2.3</version>
        </dependency>

        <!-- EL Support -->
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
            <version>2.2.3</version>
        </dependency>

        <!-- JSTL Support -->
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.servlet.jsp.jstl</artifactId>
            <version>1.2.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>jstl-api</artifactId>
                    <groupId>javax.servlet.jsp.jstl</groupId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
</project>

Demo 示例

此 Demo 启动的是war包,上下文路径指的是项目启动后的上下文地址。例如 http://ip:port/path

    /**
     * 启动war包 Demo
     *
     * @param war  war包路径
     * @param path 上下文路径
     * @param port 端口号
     */
    public void startApplication(String war, String path, Integer port) {
        Server server = new Server(port);

        WebAppContext context = new WebAppContext();
        context.setContextPath(path);
        context.setWar(war);

        server.setHandler(context);

        try {
            server.start();  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

注意的问题

war包的路径可以使用绝对路径和相对路径两种,建议使用相对路径,但是使用相对路径读取war包的时候在项目打包的时候会出现一些问题,这些问题在项目读取诸如.xml、.properties等配置文件的时候也会出现问题,主要原因是打包的时候如果将这些文件打到一起,就会缺少上级目录,如果单独打包代码,需要将配置文件单独放出去,后续会详细介绍。

相关文章

网友评论

      本文标题:Jetty 简单整理

      本文链接:https://www.haomeiwen.com/subject/blgapftx.html