1

我在eclipse中运行tomcat 7 ...当我运行我的html时出现以下错误,它调用servlet来执行简单的计算..错误是

HTTP Status 404 - /TestTomcat/  
--------------------------------------------------------------------------------

type Status report  
message /TestTomcat/
description The requested resource is not available.

--------------------------------------------------------------------------------
Apache Tomcat/7.0.37

我的 html 文件是

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form method="post" action="http://localhost:8080/TestTomcat/">
        What's your name <br />
        <input name="yourname" value="Test"/><br />

        First number <br />
        <input name="firstnum" value="3" /><br />

        Second number<br />
        <input name="secondnum" value="5"/><br />

        <input type="hidden" name="Language" value="English" />

        <input type="submit" />

    </form>
</body>
</html>

小服务程序是

package helloServlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/TestTomcat")
public class TestTomcat extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

         String usersName = request.getParameter("yourname");

         String theLang = request.getParameter("Language");

         int firstNum = Integer.parseInt(request.getParameter("firstnum"));
         int secondNum = Integer.parseInt(request.getParameter("secondnum"));
         int sumONum = firstNum + secondNum;
         response.setContentType("text/html");

         PrintWriter output = response.getWriter();

         output.println("<html><body><h3>Hello " + usersName);

         output.println("</h3><br />" + firstNum + " + " + secondNum);
         output.println(" = " + sumONum + "<br />Speaks " + theLang + "</body></html>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

web.xml 文件代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
   <display-name>Student_database</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


  <servlet>
        <servlet-name>TestTomcat</servlet-name>
        <servlet-class>helloServlets.TestTomcat</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestTomcat</servlet-name>
        <url-pattern>/TestTomcat</url-pattern>
    </servlet-mapping>
</web-app>

请帮助我,我是一个绝对的初学者....谢谢

..

是的,如果我使用“Raunak Agarwal”提供的以下解决方案并启动我的服务器,那么我会收到以下错误

我以前这样做过,但是当我启动服务器时,出现以下错误

Mar 03, 2013 9:56:29 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files (x86)/Java/jre7/bin/client;C:/Program Files (x86)/Java/jre7/bin;C:/Program Files (x86)/Java/jre7/lib/i386;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\Java\jdk1.7.0_15\bin;C:\Program Files\eclipse;;.
Mar 03, 2013 9:56:29 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TomcatTest' did not find a matching property.
Mar 03, 2013 9:56:29 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 03, 2013 9:56:29 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 03, 2013 9:56:29 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 502 ms
Mar 03, 2013 9:56:29 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 03, 2013 9:56:29 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Mar 03, 2013 9:56:30 AM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/TomcatTest]]
    at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/TomcatTest]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 7 more
Caused by: java.lang.IllegalArgumentException: The servlets named [TestTomcat] and [helloServlets.TestTomcat] are both mapped to the url-pattern [/TestTomcat] which is not permitted
    at org.apache.catalina.deploy.WebXml.addServletMapping(WebXml.java:335)
    at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2412)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2125)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2080)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2073)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1300)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5179)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more

Mar 03, 2013 9:56:30 AM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
    at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:684)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more

Mar 03, 2013 9:56:30 AM org.apache.catalina.startup.Catalina start
SEVERE: Catalina.start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:684)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 9 more
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 11 more

Mar 03, 2013 9:56:30 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 490 ms
4

3 回答 3

2

尝试将注释更改为@WebServlet(name = "TestTomcat", urlPatterns = {"/TestTomcat"})并从中删除任何 servlet 映射web.xml。我希望它应该工作。

于 2013-03-03T10:05:43.060 回答
1

首先,尝试将您的表单 action 更改为 just TestTomcat

由于您使用的是注释,因此您的 web.xml 中不需要以下代码。所以,删除它

<servlet>
        <servlet-name>TestTomcat</servlet-name>
        <servlet-class>helloServlets.TestTomcat</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestTomcat</servlet-name>
        <url-pattern>/TestTomcat</url-pattern>
    </servlet-mapping>
于 2013-03-03T04:40:11.853 回答
1

您的上下文似乎是/TomcatTest,并且您正在将 servlet 映射到/TestTomcat以便访问它,您应该提供以下 url

http://localhost:8080/TestTomcat/TestTomcat
于 2013-03-03T10:27:23.990 回答