@WebServlet annotation web.xml welcome-file [英] @WebServlet annotation web.xml welcome-file

查看:157
本文介绍了@WebServlet annotation web.xml welcome-file的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置JSP / JavaBeans项目的welcome-file。我有一个名为'Controller.java'的servlet,带有以下@WebServlet注释:

I would like to set the welcome-file of my JSP/JavaBeans project. I have a servlet named 'Controller.java' with the following @WebServlet annotation:

@WebServlet(name="Controller", urlPatterns={"/login", "/show_dbs"})

我有一个web.xml包含以下内容的文件:

and I hava a web.xml file with the following content:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

    <welcome-file-list>
        <welcome-file>Controller</welcome-file>
    </welcome-file-list>
</web-app>

几乎所有事情都进展顺利,我可以打开 http:// localhost :8080 / PROJECT / login http:// localhost:8080 / PROJECT / show_dbs 我来到Controller.java。但当我打开 http:// localhost:8080 / PROJECT / 时,我收到404错误。

Almost all things are going well, I can open http://localhost:8080/PROJECT/login and http://localhost:8080/PROJECT/show_dbs and I come to Controller.java. But when I open http://localhost:8080/PROJECT/ I get a 404 error.

我'使用带有动态Web项目的Eclipse,Controller.java文件位于/ src(默认包)下,web.xml文件位于/ WebContent / WEB-INF下。

I'm using Eclipse with a 'Dynamic Web Project', the Controller.java file is located under /src (default package) and the web.xml file is under /WebContent/WEB-INF.

我希望你有一个小费。

推荐答案

在欢迎文件列表中,您必须指定URI 。但是你已经指定了servlet的名称

In the welcome file list you must specify the URIs. But you have specified the name of the servlet.

来自 Java™Servlet规范3.0版 (强调我的):

Quote from the Java™ Servlet Specification version 3.0 (emphasis mine):


10.10欢迎文件



Web应用程序开发人员可以定义
有序列表部分URI 在Web
应用程序部署描述符中称为欢迎文件。 Web应用程序部署描述符
架构中描述了列表的部署描述符语法

10.10 Welcome Files

Web Application developers can define an ordered list of partial URIs called welcome files in the Web application deployment descriptor. The deployment descriptor syntax for the list is described in the Web application deployment descriptor schema.

此机制的目的是允许部署者
指定容器的部分URI的有序列表,用于附加到URI的
当有一个URI对应于
的URI请求时,WAR中的目录条目未映射到Web组件。这种
类型的请求称为有效的部分请求。

The purpose of this mechanism is to allow the deployer to specify an ordered list of partial URIs for the container to use for appending to URIs when there is a request for a URI that corresponds to a directory entry in the WAR not mapped to a Web component. This kind of request is known as a valid partial request.

通过以下常见示例明确了此工具的使用:欢迎文件
'index.html',以便对URL的请求,如
host:port / webapp / directory / ,其中'目录'是WAR
中的一个未映射到servlet或JSP页面的条目,作为'host:port返回给客户端
/webapp/directory/index.html'

The use for this facility is made clear by the following common example: A welcome file of 'index.html' can be defined so that a request to a URL like host:port/webapp/directory/, where 'directory' is an entry in the WAR that is not mapped to a servlet or JSP page, is returned to the client as 'host:port/webapp/directory/index.html'.

如果Web容器收到有效的部分请求,则Web容器必须检查部署描述符中定义的
welcome文件列表。欢迎的
文件列表是一个部分URL的有序列表,没有尾随或
领先/。
Web服务器必须按照
中指定的顺序附加每个欢迎文件部署请求的部署描述符和
检查WAR中的静态资源是否映射到该请求
URI。如果找不到匹配项, Web服务器必须再次按部署描述符中指定的顺序将每个
欢迎文件附加到
部分请求,并检查servlet是否映射到该请求
URI。
Web容器必须将请求发送到
中匹配的WAR的第一个资源。容器可以使用转发,重定向或特定于容器的
机制将请求发送到
welcome资源,该机制与直接请求无法区分。

If a Web container receives a valid partial request, the Web container must examine the welcome file list defined in the deployment descriptor. The welcome file list is an ordered list of partial URLs with no trailing or leading /. The Web server must append each welcome file in the order specified in the deployment descriptor to the partial request and check whether a static resource in the WAR is mapped to that request URI. If no match is found, the Web server MUST again append each welcome file in the order specified in the deployment descriptor to the partial request and check if a servlet is mapped to that request URI. The Web container must send the request to the first resource in the WAR that matches. The container may send the request to the welcome resource with a forward, a redirect, or a container specific mechanism that is indistinguishable from a direct request.

如果没有按照描述的方式找到匹配的欢迎文件,容器
可以以它认为合适的方式处理请求。对于某些配置,这可能是
意味着返回目录列表或其他人返回404响应。

If no matching welcome file is found in the manner described, the container may handle the request in a manner it finds suitable. For some configurations this may mean returning a directory listing or for others returning a 404 response.






PS


P.S.

另请参阅第10.10章中规范中的示例

Also see the examples in the specification in the chapter 10.10

这篇关于@WebServlet annotation web.xml welcome-file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆