Element [servlet]不是有效的根元素

时间:2012-10-15 11:24:04

标签: xml web web.xml

这是错误的日志,有人可以帮我解决一下这个问题吗?

In-place deployment at E:\Pavan\Java Web Apps\WebApplication2\build\web
Initializing...
deploy?DEFAULT=E:\Pavan\Java Web Apps\WebApplication2\build\web&name=WebApplication2&contextroot=/WebApplication2&force=true failed on GlassFish Server 3+ 
 Error occurred during deployment: Exception while deploying the app [WebApplication2] : Deployment descriptor file WEB-INF/web.xml in archive [web]. Element [servlet] is not a valid root element. Please see server.log for more details.
E:\Pavan\Java Web Apps\WebApplication2\nbproject\build-impl.xml:1022: The module has not been deployed.
See the server log for details.
BUILD FAILED (total time: 2 seconds)

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<servlet>
<servlet-name>
Example
</servlet-name>
<servlet-class>Exmple Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example</servlet-name>
<url-pattern>/Example/*</url-pattern>
</servlet-mapping>
<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">
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
</web-app>

我的主要目的是创建一个简单的文件来接受用户ID和密码,然后将其显示在另一个页面上。

2 个答案:

答案 0 :(得分:2)

web.xml中的元素顺序错误。您似乎已将<servlet>元素插入顶部而不是<web-app>元素内。

它需要看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<servlet>
<servlet-name>
Example
</servlet-name>
<servlet-class>Exmple Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example</servlet-name>
<url-pattern>/Example/*</url-pattern>
</servlet-mapping>
</web-app>

另外,我很确定,你的servlet类名不是Exmple Servlet。 :-)如果你尝试像这样启动它,应用程序仍会抛出错误。

答案 1 :(得分:0)

您在开始时缺少<webapp>