Eclipse + Glassfish v3 中没有 web.xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5099731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:51:33  来源:igfitidea点击:

No web.xml in Eclipse + Glassfish v3?

eclipsedeploymentglassfishdeployment-descriptor

提问by PhD

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:

我在 Eclipse (helios) + Glassfish v3 中创建了一个简单的“hello world”servlet。我正在为 Eclipse 使用 Glassfish 的插件似乎在 WEB-INF/ 文件夹中没有 web.xml 但有一个 sun-web.xml。这是我第一次使用 glassfish,但对没有 web.xml 感到有点惊讶 - 所以这里有一些问题:

  1. 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.
  2. 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?
  3. 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.
  1. 我在哪里检查 servlet 的 url 映射?在 Eclipse 中创建新的 Servlet 时,它要求我提供 URL 映射,但我无法在任何可以调整设置的 .xml 文件中找到它。
  2. 如果没有任何 web.xml,从头开始创建它会很容易出错。你有什么建议?谷歌搜索样本并玩玩?不应该是自动创建的吗?
  3. 有没有人遇到过这个?我尝试查找 web.xml 和 sun-web.xml 之间的区别,但结果一点也不具有启发性。我不想为了配置目的而学习另一个 xml,而且那太特定于 glassfish。

We have to configure servlet contexts, mappings etc especially during development/testing but the sheer absence of web.xml has me stumped.

我们必须配置 servlet 上下文、映射等,尤其是在开发/测试期间,但是 web.xml 的完全缺失让我很难过。

回答by vkraemer

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.

Eclipse 允许您在为 Java EE 6 创建动态 Web 项目时不创建 web.xml 文件,因为 Java EE 6 规范(一般)和 Servlet 3.0 规范(特别是)试图不强调部署描述符。

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 annotationsis pretty obtuse. You should read through the Servlet 3.0 spec from the jcp.org siteto get a bit more explanatory text.

您可以使用注释来提供已包含在 web.xml 文件中的所有数据。Servlet 3.0 注释Javadoc非常晦涩。您应该通读jcp.org 站点上的Servlet 3.0 规范以获取更多解释性文本。

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.

要更改 Servlet 3.0 servlet 的 url 映射,首先要查看 servlet 的源代码。查找(并更改) urlPatterns 元素的值。

If you are trying to create a web app based on Servlet 3.0, try to avoid creating a web.xml file.

如果您尝试创建基于 Servlet 3.0 的 Web 应用程序,请尽量避免创建 web.xml 文件。

The sun-web.xml/glassfish-web.xml file is used to 'finish' the description of a war file for deployment into a GlassFish container.

sun-web.xml/glassfish-web.xml 文件用于“完成”war 文件的描述以部署到 GlassFish 容器中。

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

关于像 WebServlet 这样的注释的另一个注意事项......它们没有将您的注释类集成到类层次结构中,因此正确使用 @WebServlet 看起来像

@WebServlet(
    name = "MyServlet", 
    urlPatterns = {"/path_to_servlet"}
)
public class MyServlet extends HttpServlet {}

回答by TMN

If you find you do need a web.xmlfile, 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 a web.xmlfor you with the display-nameand welcome-file-listelements.

如果您发现确实需要一个web.xml文件,您可以在 Project Explorer 视图中右键单击部署描述符,并且应该有一个选项“生成部署描述符存根”。这将web.xml使用display-namewelcome-file-list元素为您创建一个。

回答by PhD

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.

创建“新建 > 动态 Web 项目”时单击“完成”似乎是一个坏习惯 - 您应该继续单击“下一步”并转到最后一个选择“生成 web.xml 部署描述符”的窗口窗格 -似乎默认情况下未选中。

Well that says I've been a bit rusted with creating web-apps. And here I thought it was a glassfish specific issue.

嗯,这说明我对创建网络应用程序有点生疏了。在这里,我认为这是一个 glassfish 的特定问题。

回答by Lev Khomich

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.

由于 Glassfish 3.x 是完全认证的 Java EE 6 服务器,因此它支持 Servlets 3.0。从 Servlets 3.0 开始,可以通过注解指定 web.xml 设置。

For example

例如

@WebServlet(
    name = "MyServlet", 
    urlPatterns = {"/path_to_servlet"}
)
public class MyServlet {}

回答by Clete2

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.

为了补充 TMN 所说的,我注意到项目浏览器不会显示部署描述符,直到我出于某种原因执行 SVN 更新。如果您遇到该问题,请尝试更新您的代码。我已经在 HEAD 修订版上,但由于某种原因,更新显示了该视图。