我在 Eclipse (helios) + Glassfish v3 中创建了一个简单的“hello world”servlet。我正在使用 Glassfish 的 eclipse 插件 WEB-INF/ 文件夹中似乎没有 web.xml,但有一个 sun-web.xml。这是我第一次使用 glassfish,但对缺少 web.xml 感到有点惊讶 - 所以这里有一些问题:
- 我在哪里检查 servlet 的 url 映射?在 Eclipse 中创建新的 Servlet 时,它要求我提供 URL 映射,但我无法在任何可以调整设置的 .xml 文件中找到它。
- 如果没有任何 web.xml,从头开始创建它会很容易出错。你有什么建议?谷歌获取示例并玩一玩?不应该是自动创建的吗?
- 有人遇到过这种情况吗?我尝试查找 web.xml 和 sun-web.xml 之间的区别,但结果根本没有启发性。我不想为了配置目的而学习另一个 xml,而且那也是 glassfish 特定的。
我们必须配置 servlet 上下文、映射等,尤其是在开发/测试期间,但 web.xml 的完全缺失让我感到困惑。
I created a simple "hello world" servlet in Eclipse (helios) + Glassfish v3. I am using Glassfish's plugin for eclipse It seems there is no web.xml but a sun-web.xml in the WEB-INF/ folder. This is my first time with glassfish but was a bit surprised at the absence of web.xml - so here are some of the problems:
- Where do I check for url-mappings for the servlet? On creating a new Servlet in Eclipse it asks me for a URL-mapping but I'm unable to find it anywhere in any .xml file where I can tweak the settings.
- If there isn't any web.xml, creating it from scratch will be quite error prone. What do you suggest? Google for a sample and play around? Shouldn't one be auto-created?
- Has anyone encountered this? I tried looking up the difference between web.xml and sun-web.xml but the results weren't at all enlightening. I wouldn't want to learn another xml for configuration purposes and that too glassfish specific.
We have to configure servlet contexts, mappings etc especially during development/testing but the sheer absence of web.xml has me stumped.
发布评论
评论(5)
Eclipse 允许您在为 Java EE 6 创建动态 Web 项目时不创建 web.xml 文件,因为 Java EE 6 规范(一般情况)和 Servlet 3.0 规范(特别)尝试不强调部署描述符。
您可以使用注释来提供 web.xml 文件中包含的所有数据。 Servlet 3.0 注释的 Javadoc 相当迟钝。您应该通读 来自 jcp.org 站点的 Servlet 3.0 规范以获得更多解释文本。
要更改 Servlet 3.0 servlet 的 url 映射,首先要查看的地方是 servlet 的源代码。查找(并更改) urlPatterns 元素的值。
如果您尝试创建基于 Servlet 3.0 的 Web 应用程序,请尽量避免创建 web.xml 文件。
sun-web.xml/glassfish-web.xml 文件用于“完成”war 文件的描述,以便部署到 GlassFish 容器中。
关于像 WebServlet 这样的注释的另一注释...它们不会将带注释的类集成到类层次结构中,因此 @WebServlet 的正确使用看起来像
Eclipse allows you to not create a web.xml file when you create Dynamic Web Project for Java EE 6, since the Java EE 6 spec (in general) and Servlet 3.0 spec (in particular) attempt to de-emphasize deployment descriptors.
You can use annotation to provide all the data that had been included in the web.xml file. The Javadoc for the Servlet 3.0 annotations is pretty obtuse. You should read through the Servlet 3.0 spec from the jcp.org site to get a bit more explanatory text.
To change the url-mapping for a Servlet 3.0 servlet, the first place to look is in the source code for the servlet. Look for (and change) the value of the urlPatterns element.
If you are trying to create a web app based on Servlet 3.0, try to avoid creating a web.xml file.
The sun-web.xml/glassfish-web.xml file is used to 'finish' the description of a war file for deployment into a GlassFish container.
One other note about the annotations like WebServlet... they do not integrate your annotated class into the class hierarchy, so the correct use of @WebServlet would look like
如果您发现确实需要
web.xml
文件,则可以在 Project Explorer 视图中右键单击部署描述符,并且应该有一个选项“生成部署描述符存根”。这将为您创建一个包含display-name
和welcome-file-list
元素的web.xml
。If you find you do need a
web.xml
file, you can context-click on the deployment descriptor in the Project Explorer view and there should be an option "Generate Deployment Descriptor Stub". That will create aweb.xml
for you with thedisplay-name
andwelcome-file-list
elements.创建“新建>”时单击“完成”似乎是一个坏习惯。动态 Web 项目” - 您应该继续单击“下一步”,然后转到选择“生成 web.xml 部署描述符”的最后一个窗口窗格 - 似乎默认情况下未选中它。
好吧,这说明我对创建网络应用程序有点生疏了。在这里,我认为这是玻璃鱼特有的问题。
It seems that it's a bad habit to click "Finish" when you create a 'New > Dynamic Web Project' - You should keep clicking 'next' and go the last window-pane where you select "generate web.xml deployment descriptor" - seems it's unchecked by default.
Well that says I've been a bit rusted with creating web-apps. And here I thought it was a glassfish specific issue.
由于 Glassfish 3.x 是经过完全认证的 Java EE 6 服务器,因此它支持 Servlets 3.0。从 Servlets 3.0 开始,可以通过注释指定 web.xml 设置。
例如
Because of Glassfish 3.x is fully certified Java EE 6 server, it supports Servlets 3.0. Starting from Servlets 3.0, it is possible to specify web.xml settings through annotations.
For example
补充一下 TMN 所说的,我注意到项目资源管理器不会显示部署描述符,直到我出于某种原因执行了 SVN 更新。如果您遇到该问题,请尝试更新您的代码。我已经在 HEAD 修订版上,但由于某种原因,更新显示了该视图。
To add to what TMN said, I noticed that the project explorer would not show the Deployment Descriptor until I performed an SVN update for some reason. If you have that problem, try updating your code. I was on the HEAD revision already but for some reason the update showed that view.