简体   繁体   English

Web.xml导致Tomcat7失败

[英]Web.xml causes Tomcat7 to fail

I have a basic servlet. 我有一个基本的servlet。

I using tomcat 7 to run it. 我用tomcat 7来运行它。

The server contains 3 files: 该服务器包含3个文件:

  1. file.jsp - prints the date file.jsp - 打印日期

  2. WebController - the servlet. WebController - servlet。

  3. web.xml - configuration file. web.xml - 配置文件。

web.xml: web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
    <servlet>
          <servlet-name>file.jsp</servlet-name>
          <jsp-file>file.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
          <servlet-name>file.jsp</servlet-name>
          <url-pattern>/about</url-pattern>
    </servlet-mapping>
</web-app>

When I remove the <servlet> and <servlet-mapping> from the web.xml file, the servlet runs good. 当我从web.xml文件中删除<servlet><servlet-mapping> ,servlet运行良好。

When web.xml is as the above, I'm getting the following error message: web.xml如上所示时,我收到以下错误消息:

'Staring Tomcat v7.0 Server at localhost' has encountered a problem.

Server Tomcat v7.0 Server at localhost failed to start.

I'm using eclipse. 我正在使用eclipse。 what is the problem in my web.xml file? 我的web.xml文件中有什么问题? thanks in advance! 提前致谢!

Edit: 编辑:

This is my project: 这是我的项目:

在此输入图像描述

Update: 更新:

current version of web.xml : 当前版本的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
    <servlet>
          <servlet-name>file.jsp</servlet-name>
          <jsp-file>/file.jsp</jsp-file>
    </servlet>

    <servlet>
        <servlet-name>WebController</servlet-name>
        <servlet-class>WebController</servlet-class>
    </servlet>

    <servlet-mapping>
          <servlet-name>file.jsp</servlet-name>
          <url-pattern>/about</url-pattern>
    </servlet-mapping>
</web-app>

Your jsp file must not be in WEB-INF. 您的jsp文件不得位于WEB-INF中。 Place it in the root of your project 将它放在项目的根目录中

Update the web.xml as follows: 更新web.xml ,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
    <servlet>
      <servlet-name>file.jsp</servlet-name>
      <jsp-file>/file.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
      <servlet-name>file.jsp</servlet-name>
      <url-pattern>/about</url-pattern>
    </servlet-mapping>
</web-app>

here is how your project should look like: 这是你的项目应该是这样的:

/myjspapp
   /file.jsp
   /WEB-INF
      /web.xml

you can now access your jsp at the following url: localhost:8080/myjspapp/about 您现在可以通过以下url访问您的jsp: localhost:8080/myjspapp/about

One possible format for servlet mapping in the web.xml file looks like this: web.xml文件中servlet映射的一种可能格式如下所示:

<servlet>
    <servlet-name>com.example.file_jsp</servlet-name>
    <servlet-class>com.example.file_jsp</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>com.example.file_jsp</servlet-name>
    <url-pattern>/file.jsp</url-pattern>
</servlet-mapping>

The class name comes from the JSP file name and this part of your build.xml: 类名来自JSP文件名和build.xml的这一部分:

<jasper2 
  validateXml="false"
  uriroot="web"
  package="com.example"
  webXmlFragment="build/web/WEB-INF/generated_web.xml"
  outputDir="build/src" />

This is answer is similar to as ph's but as you said that it was giving you status 500 so, worked the problem out for you... 答案类似于ph,但正如你所说它给你的状态500所以,为你解决问题...

Try putting the JSP file in webcontent folder (For eclipse only) and then follow your example. 尝试将JSP文件放在webcontent文件夹中(仅限eclipse),然后按照您的示例操作。

I have worked on your example and it gives me proper output. 我已经研究过你的例子,它给了我正确的输出。

Below is my project structure in eclipse:- 以下是我在eclipse中的项目结构: -

在此输入图像描述

Below is configured web.xml file:- 下面是配置的web.xml文件: - 在此输入图像描述

After running the server, and requesting the URL Pattern "/about" I am getting my output of JSP. 在运行服务器并请求URL模式“/ about”后,我得到了JSP的输出。

Please try and let me know... Hope this helps 请尝试让我知道...希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM