You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java EE 7应用上下文根配置疑问:GlassFish如何识别及修改路径

解答你的GlassFish上下文根相关问题

Hey there! Let's break down your questions about the hello1 app's context root and the missing sun-web.xml file step by step.

1. 为什么找不到sun-web.xml?

In Java EE 7 and later versions, GlassFish replaced the old sun-web.xml with glassfish-web.xml. The sun-web.xml was a holdover from older Sun Application Server/early GlassFish versions, so it's totally normal you don't see it in your Java EE 7 project—this is just a branding and version update for the server-specific configuration file.

2. GlassFish怎么自动识别/hello1作为上下文根?

GlassFish determines the default context root for your web app through a few common, automatic rules:

  • Maven/Gradle builds: If you're using a build tool like Maven, the finalName in your pom.xml sets the default context root. For your hello1 app, this value is almost certainly set to hello1, so GlassFish uses /hello1 without extra config.
  • War file deployment: When you deploy a .war file directly to GlassFish, the server uses the war filename (stripping the .war extension) as the context root. So hello1.war automatically maps to /hello1.
  • IDE deployment: If you're deploying from an IDE like NetBeans or IntelliJ, the IDE typically uses your project name as the default context root unless you explicitly change it in the deployment settings.

Also, a quick note: Starting with Servlet 3.0 (Java EE 6+), the web.xml doesn't handle context root configuration—that's a server-specific setting, not part of the standard web app descriptor, which is why you don't see it there.

3. 怎么把上下文根改成/abc?

You have three straightforward ways to switch to /abc:

Option 1: Modify your build file (Maven example)

Open your pom.xml and update the finalName element:

<build>
    <finalName>abc</finalName>
    <!-- rest of your build configuration -->
</build>

Rebuild and deploy your app, and the resulting abc.war will map to the /abc context root.

Option 2: Use the GlassFish Admin Console

  1. Open the GlassFish admin console (usually accessible at http://localhost:4848)
  2. Log in with your admin credentials
  3. Navigate to Applications → find your hello1 app → click its name
  4. Locate the Context Root field, change its value to /abc
  5. Click Save and restart the app for changes to take effect

Option 3: Add a glassfish-web.xml file

Create a glassfish-web.xml file in your WEB-INF directory with this content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
    <context-root>/abc</context-root>
</glassfish-web-app>

This file will override the default context root setting, and GlassFish will use /abc as soon as you redeploy the app.

内容的提问来源于stack exchange,提问作者Rockstar5645

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:40:59