Html Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11731377/
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
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
提问by pongahead
I have an HTML form in a JSP file in my WebContent/jsps
folder. I have a servlet class servlet.java
in my default package in src
folder. In my web.xml
it is mapped as /servlet
.
我的文件WebContent/jsps
夹中的 JSP 文件中有一个 HTML 表单。我servlet.java
在src
文件夹中的默认包中有一个 servlet 类。在我web.xml
它被映射为/servlet
.
I have tried several URLs in action
attribute of the HTML form:
我action
在 HTML 表单的属性中尝试了几个 URL :
<form action="/servlet">
<form action="/servlet.java">
<form action="/src/servlet.java">
<form action="../servlet.java">
But none of those work. They all keep returning a HTTP 404 error like below in Tomcat 6/7/8:
但这些都不起作用。它们都在 Tomcat 6/7/8 中不断返回 HTTP 404 错误,如下所示:
HTTP Status 404 — /servlet
Description: The requested resource (/servlet) is not available.
HTTP 状态 404 — /servlet
描述:请求的资源 (/servlet) 不可用。
Or as below in Tomcat 8.5/9:
或者在 Tomcat 8.5/9 中如下:
HTTP Status 404 — Not Found
Message: /servlet
Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists
HTTP 状态 404 — 未找到
消息:/servlet
描述:源服务器没有找到目标资源的当前表示或不愿意透露一个存在
Why is it not working?
为什么它不起作用?
回答by BalusC
Put servlet class in a package
将 servlet 类放在一个 package
First of all, put the servlet class in a Java package
. You should alwaysput publicly reuseable Java classes in a package, otherwise they are invisible to classes which are in a package, such as the server itself. This way you eliminiate potential environment-specific problems. Packageless servlets work only in specific Tomcat+JDK combinations and this should never be relied upon.
首先,将 servlet 类放在 Java 中package
。您应该始终将可公开重用的 Java 类放在包中,否则它们对于包中的类(例如服务器本身)是不可见的。通过这种方式,您可以消除潜在的特定于环境的问题。无包 servlet 仅在特定的 Tomcat + JDK 组合中工作,并且永远不应依赖于此。
In case of a "plain" IDE project, the class needs to be placed in its package structure inside "Java Resources" folder and thus not"WebContent", this is for web files such as JSP. Below is an example of the folder structure of a default Eclipse Dynamic Web Projectas seen in Navigatorview:
在“普通”IDE 项目的情况下,类需要放置在“Java 资源”文件夹内的包结构中,因此不是“WebContent”,这是用于 Web 文件,如 JSP。以下是在导航器视图中看到的默认 Eclipse动态 Web 项目的文件夹结构示例:
EclipseProjectName
|-- src
| `-- com
| `-- example
| `-- YourServlet.java
|-- WebContent
| |-- WEB-INF
| | `-- web.xml
| `-- jsps
| `-- page.jsp
:
In case of a Maven project, the class needs to be placed in its package structure inside main/java
and thus note.g. main/resources
, this is for non-class files. Below is an example of the folder structure of a default Maven webapp project as seen in Eclipse's Navigatorview:
在Maven项目的情况下,类需要放在它的包结构里面main/java
,因此不是例如main/resources
,这是用于非类文件。下面是在 Eclipse 的导航器视图中看到的默认 Maven webapp 项目的文件夹结构示例:
MavenProjectName
|-- src
| `-- main
| |-- java
| | `-- com
| | `-- example
| | `-- YourServlet.java
| |-- resources
| `-- webapp
| |-- WEB-INF
| | `-- web.xml
| `-- jsps
| `-- page.jsp
:
Note that the /jsps
subfolder is not strictly necessary. You can even do without it and put the JSP file directly in webcontent/webapp root, but I'm just taking over this from your question.
请注意,/jsps
子文件夹不是绝对必要的。您甚至可以不使用它并将 JSP 文件直接放在 webcontent/webapp 根目录中,但我只是从您的问题中接管了这个。
Set servlet URL in url-pattern
设置 servlet URL url-pattern
The servlet URL is specified as the "URL pattern" of the servlet mapping. It's absolutely not per definition the classname/filename of the servlet class. The URL pattern is to be specified as value of @WebServlet
annotation.
servlet URL 被指定为 servlet 映射的“URL 模式”。servlet 类的类名/文件名绝对不是每个定义。URL 模式将被指定为@WebServlet
注释的值。
package com.example; // Use a package!
@WebServlet("/servlet") // This is the URL of the servlet.
public class YourServlet extends HttpServlet { // Must be public and extend HttpServlet.
// ...
}
In case you want to support path parameters like /servlet/foo/bar
, then use an URL pattern of /servlet/*
instead. See also Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?
如果您想支持类似 的路径参数/servlet/foo/bar
,请改用 的 URL 模式/servlet/*
。另见Servlet和路径参数如/xyz/{value}/test,如何在web.xml中映射?
@WebServlet
works only on Servlet 3.0 or newer
@WebServlet
仅适用于 Servlet 3.0 或更新版本
In order to use @WebServlet
, you only need to make sure that your web.xml
file, if any (it's optional since Servlet 3.0), is declared conform Servlet 3.0+ version and thus notconform e.g. 2.5 version or lower. Below is a Servlet 4.0 compatible one (which matches Tomcat 9+, WildFly 11+, Payara 5+, etc).
为了使用@WebServlet
,您只需要确保您的web.xml
文件(如果有)(从 Servlet 3.0 起它是可选的)被声明为符合 Servlet 3.0+ 版本,因此不符合例如 2.5 版本或更低版本。下面是一个兼容 Servlet 4.0 的(匹配 Tomcat 9+、WildFly 11+、Payara 5+ 等)。
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
>
<!-- Config here. -->
</web-app>
Or, in case you're not on Servlet 3.0+ yet (e.g. Tomcat 6 or older), then remove the @WebServlet
annotation.
或者,如果您还没有使用 Servlet 3.0+(例如 Tomcat 6 或更早版本),则删除@WebServlet
注释。
package com.example;
public class YourServlet extends HttpServlet {
// ...
}
And register the servlet instead in web.xml
like this:
并以如下方式注册 servlet web.xml
:
<servlet>
<servlet-name>yourServlet</servlet-name>
<servlet-class>com.example.YourServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>yourServlet</servlet-name>
<url-pattern>/servlet</url-pattern> <!-- This is the URL of the servlet. -->
</servlet-mapping>
Note thus that you should not use both ways. Use either annotation based configuarion or XML based configuration. When you have both, then XML based configuration will override annotation based configuration.
请注意,您不应同时使用这两种方式。使用基于注解的配置或基于 XML 的配置。当您同时拥有两者时,基于 XML 的配置将覆盖基于注解的配置。
Verifying the build/deployment
验证构建/部署
In case you're using a build tool such as Eclipse and/or Maven, then you need to make absolutely sure that the compiled servlet class file resides in its package structure in /WEB-INF/classes
folder of the produced WAR file. In case of package com.example; public class YourServlet
, it must be located in /WEB-INF/classes/com/example/YourServlet.class
. Otherwise you will face in case of @WebServlet
also a 404 error, or in case of <servlet>
a HTTP 500 error like below:
如果您使用诸如 Eclipse 和/或 Maven 之类的构建工具,那么您需要绝对确保已编译的 servlet 类文件位于/WEB-INF/classes
生成的 WAR 文件文件夹中的包结构中。在 的情况下package com.example; public class YourServlet
,它必须位于/WEB-INF/classes/com/example/YourServlet.class
。否则,您将面临@WebServlet
404 错误或<servlet>
HTTP 500 错误,如下所示:
HTTP Status 500
Error instantiating servlet class com.example.YourServlet
HTTP 状态 500
实例化 servlet 类 com.example.YourServlet 时出错
And find in the server log a java.lang.ClassNotFoundException: com.example.YourServlet
, followed by a java.lang.NoClassDefFoundError: com.example.YourServlet
, in turn followed by javax.servlet.ServletException: Error instantiating servlet class com.example.YourServlet
.
并在服务器日志中找到a java.lang.ClassNotFoundException: com.example.YourServlet
,后面跟着a java.lang.NoClassDefFoundError: com.example.YourServlet
,依次后面跟着javax.servlet.ServletException: Error instantiating servlet class com.example.YourServlet
。
An easy way to verify if the servlet is correctly compiled and placed in classpath is to let the build tool produce a WAR file (e.g. rightclick project, Export > WAR filein Eclipse) and then inspect its contents with a ZIP tool. If the servlet class is missing in /WEB-INF/classes
, or if the export causes an error, then the project is badly configured or some IDE/project configuration defaults have been mistakenly reverted (e.g. Project > Build Automaticallyhas been disabled in Eclipse).
验证 servlet 是否正确编译并放置在类路径中的一种简单方法是让构建工具生成 WAR 文件(例如,右键单击项目,Eclipse 中的导出 > WAR 文件),然后使用 ZIP 工具检查其内容。如果 servlet 类在 中丢失/WEB-INF/classes
,或者如果导出导致错误,则项目配置不当或某些 IDE/项目配置默认值被错误地恢复(例如,项目 > 自动构建已在 Eclipse 中禁用)。
You also need to make sure that the project icon has no red cross indicating a build error. You can find the exact error in Problemsview (Window > Show View > Other...). Usually the error message is fine Googlable. In case you have no clue, best is to restart from scratch and do not touch any IDE/project configuration defaults. In case you're using Eclipse, you can find instructions in How do I import the javax.servlet API in my Eclipse project?
您还需要确保项目图标没有表示构建错误的红叉。您可以在问题视图(Window > Show View > Other...)中找到确切的错误。通常错误信息是好的 Googlable。如果您不知道,最好从头开始,不要触及任何 IDE/项目配置默认值。如果您使用的是 Eclipse,您可以在如何在我的 Eclipse 项目中导入 javax.servlet API 中找到说明?
Testing the servlet individually
单独测试 servlet
Provided that the server runs on localhost:8080
, and that the WAR is successfully deployed on a context path of /contextname
(which defaults to the IDE project name, case sensitive!), and the servlet hasn't failed its initialization (read server logs for any deploy/servlet success/fail messages and the actual context path and servlet mapping), then a servlet with URL pattern of /servlet
is available at http://localhost:8080/contextname/servlet
.
假设服务器在 上运行localhost:8080
,并且 WAR 成功部署在 的上下文路径上/contextname
(默认为 IDE 项目名称,区分大小写!),并且 servlet 没有失败初始化(读取任何部署的服务器日志/ servlet 成功/失败消息以及实际上下文路径和 servlet 映射),那么 URL 模式为 的 servlet/servlet
可在http://localhost:8080/contextname/servlet
.
You can just enter it straight in browser's address bar to test it invidivually. If its doGet()
is properly overriden and implemented, then you will see its output in browser. Or if you don't have any doGet()
or if it incorrectly calls super.doGet()
, then a "HTTP 405: HTTP method GET is not supported by this URL" error will be shown (which is still better than a 404 as a 405 is evidence that the servlet itself is actually found).
您可以直接在浏览器的地址栏中输入它以进行单独测试。如果它doGet()
被正确覆盖和实现,那么您将在浏览器中看到它的输出。或者,如果您没有任何doGet()
或错误调用super.doGet()
,则会显示“ HTTP 405:此 URL 不支持 HTTP 方法 GET”错误(这仍然比 404 好,因为 405 是 servlet 的证据本身实际上被发现)。
Overriding service()
is a bad practice, unless you're reinventing a MVC framework — which is very unlikely if you're just starting out with servlets and are clueless as to the problem described in the current question ;) See also Design Patterns web based applications.
覆盖service()
是一种不好的做法,除非您正在重新发明 MVC 框架 - 如果您刚开始使用 servlet 并且对当前问题中描述的问题一无所知,则这是不太可能的;) 另请参阅设计模式基于 web 的应用程序。
Regardless, if the servlet already returns 404 when tested invidivually, then it's entirely pointless to try with a HTML form instead. Logically, it's therefore also entirely pointless to include any HTML form in questions about 404 errors from a servlet.
无论如何,如果 servlet 在单独测试时已经返回 404,那么尝试使用 HTML 表单是完全没有意义的。因此,从逻辑上讲,在有关 servlet 的 404 错误的问题中包含任何 HTML 表单也是完全没有意义的。
Referencing the servlet URL from HTML
从 HTML 引用 servlet URL
Once you've verified that the servlet works fine when invoked individually, then you can advance to HTML. As to your concrete problem with the HTML form, the <form action>
value needs to be a valid URL. The same applies to <a href>
. You need to understand how absolute/relative URLs work. You know, an URL is a web address as you can enter/see in the webbrowser's address bar. If you're specifying a relative URL as form action, i.e. without the http://
scheme, then it becomes relative to the currentURL as you see in your webbrowser's address bar. It's thus absolutely not relative to the JSP/HTML file location in server's WAR folder structure as many starters seem to think.
一旦您确认 servlet 在单独调用时工作正常,您就可以前进到 HTML。至于您对 HTML 表单的具体问题,该<form action>
值需要是一个有效的 URL。这同样适用于<a href>
. 您需要了解绝对/相对 URL 的工作原理。您知道,URL 是一个网址,您可以在网络浏览器的地址栏中输入/查看。如果您将相对 URL 指定为表单操作,即没有http://
方案,那么它就相对于您在网络浏览器地址栏中看到的当前URL。因此,它绝对与许多初学者似乎认为的服务器 WAR 文件夹结构中的 JSP/HTML 文件位置无关。
So, assuming that the JSP page with the HTML form is opened by http://localhost:8080/contextname/jsps/page.jsp
, and you need to submit to a servlet located in http://localhost:8080/contextname/servlet
, here are several cases (note that you can safely substitute <form action>
with <a href>
here):
因此,假设带有 HTML 表单的 JSP 页面由 开启http://localhost:8080/contextname/jsps/page.jsp
,并且您需要提交到位于 中的 servlet http://localhost:8080/contextname/servlet
,这里有几种情况(注意,您可以安全地替换<form action>
为<a href>
这里):
Form action submits to an URL with a leading slash.
<form action="/servlet">
The leading slash
/
makes the URL relative to the domain, thus the form will submit tohttp://localhost:8080/servlet
But this will likely result in a 404 as it's in the wrong context.
Form action submits to an URL without a leading slash.
<form action="servlet">
This makes the URL relative to the current folder of the current URL, thus the form will submit to
http://localhost:8080/contextname/jsps/servlet
But this will likely result in a 404 as it's in the wrong folder.
Form action submits to an URL which goes one folder up.
<form action="../servlet">
This will go one folder up (exactly like as in local disk file system paths!), thus the form will submit to
http://localhost:8080/contextname/servlet
This one must work!
The canonical approach, however, is to make the URL domain-relative so that you don't need to fix the URLs once again when you happen to move the JSP files around into another folder.
<form action="${pageContext.request.contextPath}/servlet">
This will generate
<form action="/contextname/servlet">
Which will thus always submit to the right URL.
表单操作提交到带有前导斜杠的 URL。
<form action="/servlet">
前导斜杠
/
使 URL 相对于域,因此表单将提交到http://localhost:8080/servlet
但这可能会导致 404,因为它在错误的上下文中。
表单操作提交到没有前导斜杠的 URL。
<form action="servlet">
这使得 URL 相对于当前 URL 的当前文件夹,因此表单将提交到
http://localhost:8080/contextname/jsps/servlet
但这可能会导致 404,因为它位于错误的文件夹中。
表单操作提交到一个向上一个文件夹的 URL。
<form action="../servlet">
这将向上移动一个文件夹(就像在本地磁盘文件系统路径中一样!),因此表单将提交到
http://localhost:8080/contextname/servlet
这个必须管用!
然而,规范的方法是使 URL 与域相关,这样当您碰巧将 JSP 文件移动到另一个文件夹时,您不需要再次修复 URL。
<form action="${pageContext.request.contextPath}/servlet">
这将产生
<form action="/contextname/servlet">
因此,这将始终提交到正确的 URL。
Use straight quotes in HTML
在 HTML 中使用直引号
You need to make absolutely sure you're using straight quotes in HTML attributes like action="..."
or action='...'
and thus notcurly quotes like action=”...”
or action='...'
. Curly quotes are not supported in HTML and they will simply become part of the value.
您需要确保在 HTML 属性中使用直引号,例如action="..."
or action='...'
,因此不要使用弯引号,例如action=”...”
or action='...'
。HTML 中不支持卷曲引号,它们将简单地成为值的一部分。
See also:
也可以看看:
- Our servlets wiki page- Contains some hello world examples
- How to call servlet class from HTML form
- doGet and doPost in Servlets
- How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
- 我们的 servlets wiki 页面- 包含一些 hello world 示例
- 如何从 HTML 表单调用 servlet 类
- Servlet 中的 doGet 和 doPost
- 如何通过单击 JSP 页面中的超链接或按钮将当前项目传递给 Java 方法?
Other cases of HTTP Status 404 error:
HTTP 状态 404 错误的其他情况:
- HTTP Status 404 - Servlet [ServletName] is not available
- HTTP Status 404 - The requested resource (/ProjectName/) is not available
- HTTP Status 404 - The requested resource (/) is not available
- JSP in /WEB-INF returns "HTTP Status 404 The requested resource is not available"
- Referencing a resource placed in WEB-INF folder in JSP file returns HTTP 404 on resource
- Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
回答by J.M.I. MADISON
Scenario #1:You accidentially re-deployedfrom the command line while tomcat was already running.
场景 #1:当 tomcat已经在运行时,您不小心从命令行重新部署了.
Short Answer:Stop Tomcat, delete target folder, mvn package, then re-deploy
简答:停止Tomcat,删除目标文件夹,mvn包,然后重新部署
Scenario #2:request.getRequestDispatcher("MIS_SPELLED_FILE_NAME.jsp")
场景#2:request.getRequestDispatcher(" MIS_SPELLED_FILE_NAME.jsp")
Short Answer:Check file name spelling, make sure caseis correct.
简答:检查文件名拼写,确保大小写正确。
Scenario #3: Class Not Found Exceptions(Answer put here because: Question# 17982240 ) (java.lang.ClassNotFoundException for servlet in tomcat with eclipse) (was marked as duplicate and directed me here )
场景#3:找不到类异常(答案放在这里是因为:问题#17982240)(带有eclipse的tomcat中的servlet的java.lang.ClassNotFoundException)(被标记为重复并指向我这里)
Short Answer #3.1: web.xml has wrong package path in servlet-class tag.
简短回答 #3.1:web.xml 在 servlet-class 标签中有错误的包路径。
Short Answer #3.2: java file has wrong import statement.
简答#3.2:java 文件有错误的导入语句。
Below is further details for Scenario #1:
以下是场景 #1 的更多详细信息:
1: Stop Tomcat
1:停止Tomcat
- Option 1: Via CTRL+C in terminal.
- Option 2: (terminal closed while tomcat still running)
- ------------ 2.1: press:Windows+R--> type:"services.msc"
- ------------ 2.2: Find "Apache Tomcat #.# Tomcat#" in Name column of list.
- ------------ 2.3: Right Click --> "stop"
- 选项 1:通过终端中的 CTRL+C。
- 选项 2: (终端关闭,而 tomcat 仍在运行)
- ------------ 2.1:按:Windows+R--> 输入:“ services.msc”
- ------------ 2.2:在列表的名称栏中找到“Apache Tomcat #.# Tomcat#”。
- ------------ 2.3:右键单击-->“停止”
2: Delete the "target" folder. (mvn clean will not help you here)
2:删除“目标”文件夹。 (mvn clean 在这里帮不了你)
3: mvn package
3:mvn包
4: YOUR_DEPLOYMENT_COMMAND_HERE
4:YOUR_DEPLOYMENT_COMMAND_HERE
(Mine: java -jar target/dependency/webapp-runner.jar --port 5190 target/*.war )
(我的: java -jar target/dependency/webapp-runner.jar --port 5190 target/*.war )
Full Back Story:
完整的背景故事:
Accidentially opened a new git-bash window and tried to deploy a .war file for my heroku project via:
不小心打开了一个新的 git-bash 窗口,并尝试通过以下方式为我的 heroku 项目部署一个 .war 文件:
java -jar target/dependency/webapp-runner.jar --port 5190 target/*.war
java -jar target/dependency/webapp-runner.jar --port 5190 target/*.war
After a failure to deploy, I realized I had two git-bash windows open, and had not used CTLR+C to stop the previous deployment.
部署失败后,我意识到我打开了两个 git-bash 窗口,并且没有使用 CTLR+C 停止之前的部署。
I was met with:
我遇到了:
HTTP Status 404 – Not Found Type Status Report
Message /if-student-test.jsp
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/8.5.31
HTTP 状态 404 – 未找到类型状态报告
消息/if-student-test.jsp
描述 源服务器没有找到目标资源的当前表示,或者不愿意透露一个存在。
Apache Tomcat/8.5.31
Below is further details for Scenario #3:
以下是场景 #3 的更多详细信息:
SCENARIO 3.1: The servlet-class package path is wrong in your web.xml file.
场景 3.1:web.xml 文件中的 servlet 类包路径错误。
It should MATCH the package statement at top of your java servlet class.
它应该匹配 java servlet 类顶部的包语句。
File: my_stuff/MyClass.java:
文件:my_stuff/ MyClass.java:
package my_stuff;
File: PRJ_ROOT/src/main/webapp/WEB-INF/web.xml
文件:PRJ_ROOT/src/main/webapp/WEB-INF/ web.xml
<servlet-class>
my_stuff.MyClass
</servlet-class>
SCENARIO 3.2:
场景 3.2:
You put the wrong "package" statement at top of your myClass.java file.
您在 myClass.java 文件的顶部放置了错误的“ package”语句。
For example:
例如:
File is in: "/my_stuff" folder
文件在:“ /my_stuff”文件夹中
You mistakenly write:
你写错了:
package com.my_stuff
This is tricky because:
这很棘手,因为:
1: The maven build (mvn package) will not report any errors here.
1:maven build(mvn包)这里不会报错。
2: servlet-class line in web.xml can have CORRECT package path. E.g:
2:web.xml 中的servlet-class 行可以有正确的包路径。例如:
<servlet-class>
my_stuff.MyClass
</servlet-class>
Stack Used: Notepad+++ GitBash+ Maven+ Heroku Web App Runner+ Tomcat9+ Windows10:
使用的堆栈: Notepad+++ GitBash+ Maven+ Heroku Web App Runner+ Tomcat9+ Windows10:
回答by THE_DOM
My issue was that my method was missing the @RequestBody annotation. After adding the annotation I no longer received the 404 exception.
我的问题是我的方法缺少 @RequestBody 注释。添加注释后,我不再收到 404 异常。
回答by Sajal Saha
Do the following two steps. I hope, it will solve the "404 not found" issue in tomcat server during the development of java servlet application.
执行以下两个步骤。我希望它能解决java servlet应用程序开发过程中tomcat服务器中“404 not found”的问题。
Step 1: Right click on the server(in the server explorer tab)->Properties->Switch Location from workspace metadata to tomcat server
第1步: Right click on the server(in the server explorer tab)->Properties->Switch Location from workspace metadata to tomcat server
Step 2: Double Click on the server(in the server explorer tab)->Select Use tomcat installation option inside server location menu
第2步: Double Click on the server(in the server explorer tab)->Select Use tomcat installation option inside server location menu
回答by pajarnas
I removed the old web library such that are spring framework libraries. And build a new path of the libraries. Then it works.
我删除了旧的 web 库,比如 spring 框架库。并建立一个新的图书馆路径。然后它起作用了。
回答by Chaitanya
An old thread, but since I didn't find it elsewhere, here is one more possibility:
一个旧线程,但由于我没有在其他地方找到它,这是另一种可能性:
If you're using servlet-api 3.0+, then your web.xml must NOTinclude metadata-complete="true"
attribute
如果您使用的servlet-API 3.0+,那么你的web.xml文件必须不包括metadata-complete="true"
属性
This tells tomcat to map the servlets using data given in web.xml
instead of using the @WebServlet
annotation.
这告诉 tomcat 使用 中给出的数据web.xml
而不是使用@WebServlet
注释来映射 servlet 。
回答by Selby Khuzwayo
First of all, run your IDE as Admin. After that, right click the project folder -> Project Facets and make sure that the Java Version is set correct. On my PC. (For Example 1.8) Now it should work.
首先,以管理员身份运行 IDE。之后,右键单击项目文件夹 -> Project Facets 并确保 Java 版本设置正确。在我的电脑上。(对于示例 1.8)现在它应该可以工作了。
Don't just start your server, for example Wildfly, using the cmd. It has to be launched within the IDE and now visit your localhost URL. Example: http://localhost:8080/HelloWorldServlet/HelloWorld
不要只是使用 cmd 启动您的服务器,例如 Wildfly。它必须在 IDE 中启动,然后访问您的本地主机 URL。示例:http://localhost:8080/HelloWorldServlet/HelloWorld
回答by joseph zhao
The fix that worked for me is(if you are using Maven): Rightclick your project, Maven -> Update project. This might give you some other error with the JDK and other Libraries(in my case, MySQL connector), but once you fix them, your original problem should be fixed!
对我有用的修复是(如果您使用的是 Maven):右键单击您的项目,Maven -> 更新项目。这可能会给您带来 JDK 和其他库(在我的情况下为 MySQL 连接器)的其他错误,但是一旦您修复它们,您的原始问题就应该被修复!
回答by matak8s
If you would like to open a servlet with javascript without using 'form' and 'submit' button, here is the following code:
如果您想在不使用“表单”和“提交”按钮的情况下使用 javascript 打开 servlet,这里是以下代码:
var button = document.getElementById("<<button-id>>");
button.addEventListener("click", function() {
window.location.href= "<<full-servlet-path>>" (eg. http://localhost:8086/xyz/servlet)
});
Key:
钥匙:
1) button-id : The 'id' tag you give to your button in your html/jsp file.
1) button-id :您在 html/jsp 文件中为按钮提供的“id”标签。
2) full-servlet-path: The path that shows in the browser when you run the servlet alone
2)full-servlet-path:单独运行servlet时在浏览器中显示的路径
回答by O R Imon
Solution for HTTP Status 404
in NetBeans IDE:
Right click on your project and go to your project properties, then click on run, then input your project relative URL like index.jsp
.
HTTP Status 404
NetBeans IDE 中的解决方案:右键单击您的项目并转到您的项目属性,然后单击运行,然后输入您的项目相对 URL,如index.jsp
.
- Project->Properties
- Click on Run
- Relative URL:/index.jsp (Select your project root URL)
- 项目->属性
- 点击运行
- 相对 URL:/index.jsp(选择您的项目根 URL)