Java EE 7应用上下文根配置疑问: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
finalNamein yourpom.xmlsets the default context root. For your hello1 app, this value is almost certainly set tohello1, so GlassFish uses/hello1without extra config. - War file deployment: When you deploy a
.warfile directly to GlassFish, the server uses the war filename (stripping the.warextension) as the context root. Sohello1.warautomatically 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
- Open the GlassFish admin console (usually accessible at
http://localhost:4848) - Log in with your admin credentials
- Navigate to Applications → find your hello1 app → click its name
- Locate the Context Root field, change its value to
/abc - 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

