Java 未找到 HTTP 404(未找到 Servlet)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22317434/
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-08-13 14:54:26  来源:igfitidea点击:

HTTP 404 not found(Servlet not found)

javajsphttptomcatservlets

提问by Saumyaraj

I am a student learning to build Web Application using jsp and servlets. My Web Application project was working fine since a month but today suddenly its behaving weird. When I am submitting my jsp page, it is not able to find my servlet. I have used servlet annotationto map the request.

我是一名学生,正在学习使用 jsp 和 servlet 构建 Web 应用程序。一个月以来,我的 Web 应用程序项目运行良好,但今天突然变得很奇怪。当我提交我的 jsp 页面时,它无法找到我的 servlet。我使用servlet 注释来映射请求。

Following is my jsp:-

以下是我的jsp:-

<form name=registration_form action="<%=application.getContextPath() %>/Registration" method="post">  
    First Name:</td><td><input type="text" name="firstName" required/></td></tr> 
    </form>

Following is my Servlet:-

以下是我的 Servlet:-

package servlets;
    @WebServlet("/Registration")
    public class Registration extends HttpServlet {
        private static final long serialVersionUID = 1L;
        public Registration() {
            super();
        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String firstName=request.getParameter("firstName"); 
            System.out.println(firstName);
        }
    }

Following is my web.xml:-

以下是我的 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>Login</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Following is my Project Hierarchy:-

以下是我的项目层次结构:-

enter image description here

在此处输入图片说明

Following is the error:-

以下是错误:-

HTTP Status 404 - /Login/Registration
type Status report

message /Login/Registration

description The requested resource is not available.

Apache Tomcat/7.0.47 

Following is my console log:-

以下是我的控制台日志:-

Mar 11, 2014 11:30:33 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/jdk1.6.0_17/bin/../jre/bin/client;C:/Program Files (x86)/Java/jdk1.6.0_17/bin/../jre/bin;C:/Program Files (x86)/Java/jdk1.6.0_17/bin/../jre/lib/i386;C:\Program Files (x86)\PC Connectivity Solution\;C:\Program Files (x86)\Java\jdk1.6.0_17\bin;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\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;c:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;c:\Program Files (x86)\Common Files\Roxio Shared\OEM\DLLShared\;c:\Program Files (x86)\Common Files\Roxio Shared\OEM\DLLShared\;c:\Program Files (x86)\Common Files\Roxio Shared\OEM.0\DLLShared\;c:\Program Files (x86)\Roxio\OEM\AudioCore\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Microsoft SQL Server\Tools\binn\;C:\Program Files (x86)\Android\android-sdk;C:\Program Files (x86)\Java\jdk1.6.0_17\bin;C:\Program Files (x86)\MySQL\MySQL Utilities 1.3.4\;C:\Program Files (x86)\Intel\OpenCL SDK.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK.0\bin\x64;D:\mongodb\bin;D:\development tools\apache-maven-3.1.1-bin\apache-maven-3.1.1\bin;C:\Program Files (x86)\Google\google_appengine\;D:\development tools\eclipse;;.
Mar 11, 2014 11:30:33 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Login' did not find a matching property.
Mar 11, 2014 11:30:34 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 11, 2014 11:30:34 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 11, 2014 11:30:34 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1071 ms
Mar 11, 2014 11:30:34 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 11, 2014 11:30:34 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Mar 11, 2014 11:30:35 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 11, 2014 11:30:35 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 11, 2014 11:30:35 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1463 ms

采纳答案by Hardik Barot

I had suffered this problem too. Whenever we delete a tomcat server and run our project in new one without build classpaths and clean and build that project, we got this type of exceptions.

我也遇到过这个问题。每当我们删除一个 tomcat 服务器并在没有构建类路径的情况下在新的服务器中运行我们的项目并清理和构建该项目时,我们都会遇到这种类型的异常。

So, Build your projects classpath. Goto property of your Project then Select =>Java Build Path => Order and Export. Select All classpaths.

因此,构建您的项目类路径。转到项目的属性,然后选择 =>Java 构建路径 => 订购和导出。选择所有类路径。

回答by shazin

Your Servlet path is /Registrationbut your request points to /Login/Registration.

您的 Servlet 路径是/Registration但您的请求指向/Login/Registration.

You need to your form's action attribute to follow.

您需要遵循表单的 action 属性。

action="${pageContext.request.contextPath}/Registration"

or

或者

action="/Registration"

回答by jmail

use this format in your jsp action page:

在您的 jsp 操作页面中使用此格式:

action="/Registration"

and the webpage:

和网页:

<servlet>
<servlet-name>Registration</servlet-name>
 <servlet-class>packagename.Registration</servlet-class>
</servlet> 
<servlet-mapping>
<servlet-name>Registration</servlet-name>
 <url-pattern>/Registration</url-pattern>
</servlet-mapping>

follow this

按照这个