jetty 中如何设置root app

 

 

jetty作为一个web容器,部署时只需要将部署包扔到 $JETTY_HOME/webapp目录下即可!

例如工程 test.war ,在启动jetty后的访问路径就是 http://localhost:8080/test ,若是我们期望访问 http://localhost:8080/ 即是访问test工程的呢?有两种办法可以解决。

 

1.直接将 test.war 改名为root.war 。

这个是最简单的做法,也是一般web容器都通用的做法,放到tomcat也是这种做法。

 

 

2.修改contexts目录下文件。

这个做法稍微有点麻烦,是通过设置 contexts来达到目的,不期望改变原来的配置文件,所以我们copy配置文件出来。

 

step1 准备工作

 

mkdir -p /home/inter12/jetty/webapp/virtual
mkdir -p /home/inter12/jetty/config
mkdir -p /home/inter12/jetty/config/contexts

cp $JETTY_HOME/etc/jetty.xml  /home/inter12/jetty/config/ 
 

 

step2 修改配置 

vim  /home/inter12/jetty/config/jetty.xml

修改其中的

 

 

....

<Call name="addLifeCycle">
      <Arg>
        <New class="org.mortbay.jetty.deployer.ContextDeployer">
          <Set name="contexts"><Ref id="Contexts"/></Set>
          
            <!----------------- 将contexts路径设置为我们新的 -------------------->
            <Set name="configurationDir">/home/inter12/jetty/config/contexts</Set> 
            
          <Set name="scanInterval">5</Set>
        </New>
      </Arg>
</Call>
.......
<Call name="addLifeCycle">
       <Arg>
         <New class="org.mortbay.jetty.deployer.WebAppDeployer">
           <Set name="contexts"><Ref id="Contexts"/></Set>
           
           <!--------------- 设置webapp 目录  指定到一个虚拟的webapp 。--------------->
           <Set name="webAppDir">/home/inter12/jetty/webapp/virtual</Set>   
           
           <Set name="parentLoaderPriority">false</Set>
           <Set name="extract">true</Set>
           <Set name="allowDuplicates">false</Set>
           <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>
         </New>
       </Arg>
</Call>
...... 
 

 

cd /home/inter12/jetty/config/contexts 

新建一个 test.xml(这个名字可以是任意的)

 

 

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<Configure class="org.mortbay.jetty.webapp.WebAppContext">


  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Required minimal context configuration :                        -->
  <!--  + contextPath                                                  -->
  <!--  + war OR resourceBase                                          -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <Set name="contextPath">/</Set>
  
  <!-- 这里必须加上war这个结尾 - - - - - - - - - - - - - - - - - - - - -->
  <Set name="war">/home/inter12/jetty/webapp/test.war</Set>   

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Optional context configuration                                  -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <Set name="extractWAR">true</Set>
  <Set name="copyWebDir">false</Set>

</Configure>
 

 

好了,到这里就已经基本结束了。

启动jetty 

 

cd $JETTY_HOME

 

java -jar start.jar /home/inter12/jetty/config/jetty.xml 

 

那么访问 http://localhost:8080/test  等同于  http://localhost:8080

 

TIPS:

  若是通过contexts来配置 / 访问的话,那么war就不要放在webapp目录下,jetty会加载两边,若是配置了log4j的话,就会出现一下错误,切记!!!

 

 Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 


### 配置和使用 Jetty 嵌入式服务器 要在 Spring MVC 项目中集成和使用 Jetty 作为嵌入式服务器,可以通过以下方式实现: #### 添加依赖项 首先,在项目的 `pom.xml` 文件中添加 Jetty 和 Spring 的相关依赖。以下是 Maven 中的配置示例[^1]: ```xml <dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- Embedded Jetty Server --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>${jetty.version}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>${jetty.version}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jsp</artifactId> <version>${jetty.version}</version> </dependency> </dependencies> ``` #### 创建主类启动 Jetty 创建一个 Java 类用于初始化和启动 Jetty 服务器。该类负责加载 Spring 上下文以及设置 Web 应用程序上下文。 ```java import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.webapp.WebAppContext; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; public class JettyLauncher { public static void main(String[] args) throws Exception { // 初始化 Jetty 服务器实例 Server server = new Server(8080); // 设置 Servlet 上下文处理器 ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); // 加载 Spring 配置文件 AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext(); springContext.register(AppConfiguration.class); // 注册您的 Spring 配置类 // 将 Spring 上下文绑定到 Jetty context.addEventListener(new ContextLoaderListener(springContext)); context.addServlet(org.springframework.web.servlet.DispatcherServlet.class, "/").setInitParameter("contextAttribute", "org.springframework.web.context.WebApplicationContext.ROOT"); // 启动 Jetty 并加入主线程等待 server.setHandler(context); try { server.start(); server.join(); } finally { server.destroy(); } } } ``` #### 解决 JSP 资源映射问题 如果遇到无法找到 JSP 文件的问题,则需要确保 Jetty 正确加载了 JSP 支持模块。通过引入 `jetty-jsp` 依赖并调整 Web 应用路径即可解决此问题[^1]。 另外,确认 `web.xml` 或者基于注解的方式已经正确定义视图解析器以支持 `.jsp` 扩展名: ```java @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } ``` 以上方法能够帮助您成功将 Jetty 整合至现有的 Spring MVC 工程之中,并妥善处理静态资源与动态页面之间的路由冲突情况。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值