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/ JSP文件夹中有一个JSP文件中的HTML表单。我有一个servlet类servlet。在src文件夹中的默认包中的java。在我的网站。它被映射为/servlet。
I have tried several URLs in action
attribute of the HTML form:
我在HTML表单的action属性中尝试了几个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:
但这些都不奏效。它们都不断返回HTTP 404错误,如下面Tomcat 6/7/8中的错误:
HTTP Status 404 — /servlet
Description: The requested resource (/servlet) is not available.
说明:请求的资源(/servlet)不可用。
Or as below in Tomcat 8.5/9:
或如以下Tomcat 8.5/9:
HTTP Status 404 — Not Found
Message: /servlet
消息:/ servlet
Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists
描述:源服务器没有找到目标资源的当前表示,或者不愿意透露该资源的存在。
Why is it not working?
为什么它不能工作?
2 个解决方案
#1
93
Put servlet class in a package
First of all, put the servlet class in a Java package
. You should always put 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包中。您应该始终将可重用的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 Project as seen in Navigator view:
在“plain”IDE项目中,类需要放在“Java资源”文件夹内的包结构中,而不是“WebContent”,这是用于JSP等web文件的。下面是默认的Eclipse动态Web项目的文件夹结构示例,如Navigator视图所示:
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 not e.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 Navigator view:
对于Maven项目,需要将类放在main/java内部的包结构中,而不是main/resources,这是针对非类文件的。下面是默认Maven webapp项目的文件夹结构示例,如Eclipse的Navigator视图所示:
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.
注意,/jsp子文件夹并不是必须的。你甚至可以不使用它,直接将JSP文件放在webcontent/webapp root中,但我只是从你的问题中接手这个问题。
Set servlet URL in 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等路径参数,那么使用/servlet/*的URL模式。还可以查看Servlet和路径参数,如/xyz/{值}/测试,如何在web.xml中映射?
@WebServlet
works only on Servlet 3.0 or newer
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 not conform e.g. 2.5 version or lower. Below is a Servlet 3.1 compatible one (which matches Tomcat 8+, WildFly 8+, GlassFish 4+, etc).
为了使用@WebServlet,您只需确保您的web。如果有xml文件(因为Servlet 3.0是可选的),则声明为符合Servlet 3.0+版本,因此不符合如2.5版本或更低版本。下面是一个Servlet 3.1兼容的(匹配Tomcat 8+、WildFly 8+、GlassFish 4+等)。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1"
>
<!-- Config here. -->
</web-app>
Or, in case you're not on Servlet 3.0+ yet (not Tomcat 7 or newer, but Tomcat 6 or older), then remove the @WebServlet
annotation.
或者,如果您还没有使用Servlet 3.0+(不是Tomcat 7或更新的,而是Tomcat 6或更老的),那么删除@WebServlet注释。
package com.example;
public class YourServlet extends HttpServlet {
// ...
}
And register the servlet instead in web.xml
like this:
并在web中注册servlet。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类文件位于所生成WAR文件的/WEB-INF/classes文件夹中的包结构中。如果是软件包com.示例;公共类YourServlet,它必须位于/WEB-INF/classes/ com/example/yourservlet.class中。否则,@WebServlet也会出现404错误,或者
HTTP Status 500
HTTP状态500
Error instantiating servlet class com.example.YourServlet
实例化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
.
并在服务器日志中找到java.lang。com . example。ClassNotFoundException:然后是java.lang。com . example。NoClassDefFoundError:然后是javax.servlet。ServletException:实例化servlet类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 file in Eclipse) and then inspect its contents with a ZIP tool. If the servlet class is missing in /WEB-INF/classes
, then the project is badly configured or some IDE/project configuration defaults have been mistakenly reverted (e.g. Project > Build Automatically has been disabled in Eclipse). In case you have no clue, best is to restart from scratch and do not touch any IDE/project configuration defaults.
验证servlet是否被正确编译并放置在类路径中的一个简单方法是让构建工具生成WAR文件(例如,rightclick项目,在Eclipse中导出> WAR文件),然后使用ZIP工具检查其内容。如果/WEB-INF/类中缺少servlet类,那么项目配置糟糕,或者某些IDE/项目配置默认值被错误地恢复(例如,在Eclipse中自动禁用项目>构建)。如果您没有任何线索,最好是从头开始,不要触及任何IDE/项目配置默认值。
Testing the servlet individually
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年,战争是成功部署的上下文路径/ contextname(默认为IDE项目名称,区分大小写!),及其初始化servlet没有失败(读任何部署服务器日志/ servlet成功/失败消息和实际的上下文路径和servlet映射),然后一个servlet的URL模式/ 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
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 current URL 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表单的具体问题,
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打开的。您需要提交到位于http://localhost:8080/contextname/servlet中的servlet,这里有几种情况(注意,您可以安全地将
-
Form action submits to an URL with a leading slash.
表单动作提交到一个具有前导斜杠的URL。
<form action="/servlet">
The leading slash
/
makes the URL relative to the domain, thus the form will submit to前导斜杠/使URL相对于域,因此表单将提交给
http://localhost:8080/servlet
But this will likely result in a 404 as it's in the wrong context.
但这可能会导致404错误,因为它在错误的环境中。
-
Form action submits to an URL without a leading slash.
表单动作提交到一个URL,而没有一个主斜杠。
<form action="servlet">
This makes the URL relative to the current folder of the current URL, thus the form will submit to
这使得URL相对于当前URL的当前文件夹,因此表单将提交给。
http://localhost:8080/contextname/jsps/servlet
But this will likely result in a 404 as it's in the wrong folder.
但这可能会导致404页面出现在错误的文件夹中。
-
Form action submits to an URL which goes one folder up.
表单动作提交到一个指向一个文件夹的URL。
<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.
不过,规范的方法是使URL域相对,这样当您碰巧将JSP文件移动到另一个文件夹时,就不需要再次修复URL。
<form action="${pageContext.request.contextPath}/servlet">
This will generate
这将生成
<form action="/contextname/servlet">
Which will thus always submit to the right URL.
因此,它将始终提交到正确的URL。
Use straight quotes in HTML
You need to make absolutely sure you're using straight quotes in HTML attributes like action="..."
or action='...'
and thus not curly quotes like action=”...”
or action=’...’
. Curly quotes are not supported in HTML and they will simply become part of the value.
您需要确保在HTML属性中使用直引号,比如action="…"或action='…因此不像action= "……"”或者action =“…”。在HTML中不支持花括号,它们只会成为值的一部分。
See also:
- Our servlets wiki page - Contains some hello world examples
- 我们的servlet wiki页面—包含一些hello world示例
- How to call servlet class from HTML form
- 如何从HTML表单调用servlet类
- doGet and doPost in Servlets
- 在Servlets中doGet和doPost。
- How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
- 如何通过单击JSP页面中的超链接或按钮将当前项传递给Java方法?
Other cases of HTTP Status 404 error:
- HTTP Status 404 - Servlet [ServletName] is not available
- HTTP状态404 - Servlet [ServletName]不可用
- HTTP Status 404 - The requested resource (/ProjectName/) is not available
- HTTP状态404 -请求的资源(/ProjectName/)不可用
- HTTP Status 404 - The requested resource (/) is not available
- HTTP状态404 -请求的资源(/)不可用
- JSP in /WEB-INF returns "HTTP Status 404 The requested resource is not available"
- /WEB-INF中的JSP返回“HTTP状态404请求的资源不可用”
- Referencing a resource placed in WEB-INF folder in JSP file returns HTTP 404 on resource
- 在JSP文件中引用放置在WEB-INF文件夹中的资源将返回资源上的HTTP 404
- Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
- 当调用一个转发到JSP的Servlet时,浏览器无法访问/查找CSS、图像和链接等相关资源
#2
0
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
.
NetBeans IDE中HTTP状态404的解决方案:右键单击项目,进入项目属性,然后单击run,然后输入项目相关URL,如index.jsp。
- Project->Properties
- 项目- >属性
- Click on Run
- 点击运行
- Relative URL:/index.jsp (Select your project root URL)
- 相对URL:/索引。jsp(选择项目根URL)
#1
93
Put servlet class in a package
First of all, put the servlet class in a Java package
. You should always put 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包中。您应该始终将可重用的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 Project as seen in Navigator view:
在“plain”IDE项目中,类需要放在“Java资源”文件夹内的包结构中,而不是“WebContent”,这是用于JSP等web文件的。下面是默认的Eclipse动态Web项目的文件夹结构示例,如Navigator视图所示:
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 not e.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 Navigator view:
对于Maven项目,需要将类放在main/java内部的包结构中,而不是main/resources,这是针对非类文件的。下面是默认Maven webapp项目的文件夹结构示例,如Eclipse的Navigator视图所示:
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.
注意,/jsp子文件夹并不是必须的。你甚至可以不使用它,直接将JSP文件放在webcontent/webapp root中,但我只是从你的问题中接手这个问题。
Set servlet URL in 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等路径参数,那么使用/servlet/*的URL模式。还可以查看Servlet和路径参数,如/xyz/{值}/测试,如何在web.xml中映射?
@WebServlet
works only on Servlet 3.0 or newer
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 not conform e.g. 2.5 version or lower. Below is a Servlet 3.1 compatible one (which matches Tomcat 8+, WildFly 8+, GlassFish 4+, etc).
为了使用@WebServlet,您只需确保您的web。如果有xml文件(因为Servlet 3.0是可选的),则声明为符合Servlet 3.0+版本,因此不符合如2.5版本或更低版本。下面是一个Servlet 3.1兼容的(匹配Tomcat 8+、WildFly 8+、GlassFish 4+等)。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1"
>
<!-- Config here. -->
</web-app>
Or, in case you're not on Servlet 3.0+ yet (not Tomcat 7 or newer, but Tomcat 6 or older), then remove the @WebServlet
annotation.
或者,如果您还没有使用Servlet 3.0+(不是Tomcat 7或更新的,而是Tomcat 6或更老的),那么删除@WebServlet注释。
package com.example;
public class YourServlet extends HttpServlet {
// ...
}
And register the servlet instead in web.xml
like this:
并在web中注册servlet。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类文件位于所生成WAR文件的/WEB-INF/classes文件夹中的包结构中。如果是软件包com.示例;公共类YourServlet,它必须位于/WEB-INF/classes/ com/example/yourservlet.class中。否则,@WebServlet也会出现404错误,或者
HTTP Status 500
HTTP状态500
Error instantiating servlet class com.example.YourServlet
实例化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
.
并在服务器日志中找到java.lang。com . example。ClassNotFoundException:然后是java.lang。com . example。NoClassDefFoundError:然后是javax.servlet。ServletException:实例化servlet类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 file in Eclipse) and then inspect its contents with a ZIP tool. If the servlet class is missing in /WEB-INF/classes
, then the project is badly configured or some IDE/project configuration defaults have been mistakenly reverted (e.g. Project > Build Automatically has been disabled in Eclipse). In case you have no clue, best is to restart from scratch and do not touch any IDE/project configuration defaults.
验证servlet是否被正确编译并放置在类路径中的一个简单方法是让构建工具生成WAR文件(例如,rightclick项目,在Eclipse中导出> WAR文件),然后使用ZIP工具检查其内容。如果/WEB-INF/类中缺少servlet类,那么项目配置糟糕,或者某些IDE/项目配置默认值被错误地恢复(例如,在Eclipse中自动禁用项目>构建)。如果您没有任何线索,最好是从头开始,不要触及任何IDE/项目配置默认值。
Testing the servlet individually
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年,战争是成功部署的上下文路径/ contextname(默认为IDE项目名称,区分大小写!),及其初始化servlet没有失败(读任何部署服务器日志/ servlet成功/失败消息和实际的上下文路径和servlet映射),然后一个servlet的URL模式/ 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
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 current URL 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表单的具体问题,
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打开的。您需要提交到位于http://localhost:8080/contextname/servlet中的servlet,这里有几种情况(注意,您可以安全地将
-
Form action submits to an URL with a leading slash.
表单动作提交到一个具有前导斜杠的URL。
<form action="/servlet">
The leading slash
/
makes the URL relative to the domain, thus the form will submit to前导斜杠/使URL相对于域,因此表单将提交给
http://localhost:8080/servlet
But this will likely result in a 404 as it's in the wrong context.
但这可能会导致404错误,因为它在错误的环境中。
-
Form action submits to an URL without a leading slash.
表单动作提交到一个URL,而没有一个主斜杠。
<form action="servlet">
This makes the URL relative to the current folder of the current URL, thus the form will submit to
这使得URL相对于当前URL的当前文件夹,因此表单将提交给。
http://localhost:8080/contextname/jsps/servlet
But this will likely result in a 404 as it's in the wrong folder.
但这可能会导致404页面出现在错误的文件夹中。
-
Form action submits to an URL which goes one folder up.
表单动作提交到一个指向一个文件夹的URL。
<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.
不过,规范的方法是使URL域相对,这样当您碰巧将JSP文件移动到另一个文件夹时,就不需要再次修复URL。
<form action="${pageContext.request.contextPath}/servlet">
This will generate
这将生成
<form action="/contextname/servlet">
Which will thus always submit to the right URL.
因此,它将始终提交到正确的URL。
Use straight quotes in HTML
You need to make absolutely sure you're using straight quotes in HTML attributes like action="..."
or action='...'
and thus not curly quotes like action=”...”
or action=’...’
. Curly quotes are not supported in HTML and they will simply become part of the value.
您需要确保在HTML属性中使用直引号,比如action="…"或action='…因此不像action= "……"”或者action =“…”。在HTML中不支持花括号,它们只会成为值的一部分。
See also:
- Our servlets wiki page - Contains some hello world examples
- 我们的servlet wiki页面—包含一些hello world示例
- How to call servlet class from HTML form
- 如何从HTML表单调用servlet类
- doGet and doPost in Servlets
- 在Servlets中doGet和doPost。
- How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
- 如何通过单击JSP页面中的超链接或按钮将当前项传递给Java方法?
Other cases of HTTP Status 404 error:
- HTTP Status 404 - Servlet [ServletName] is not available
- HTTP状态404 - Servlet [ServletName]不可用
- HTTP Status 404 - The requested resource (/ProjectName/) is not available
- HTTP状态404 -请求的资源(/ProjectName/)不可用
- HTTP Status 404 - The requested resource (/) is not available
- HTTP状态404 -请求的资源(/)不可用
- JSP in /WEB-INF returns "HTTP Status 404 The requested resource is not available"
- /WEB-INF中的JSP返回“HTTP状态404请求的资源不可用”
- Referencing a resource placed in WEB-INF folder in JSP file returns HTTP 404 on resource
- 在JSP文件中引用放置在WEB-INF文件夹中的资源将返回资源上的HTTP 404
- Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
- 当调用一个转发到JSP的Servlet时,浏览器无法访问/查找CSS、图像和链接等相关资源
#2
0
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
.
NetBeans IDE中HTTP状态404的解决方案:右键单击项目,进入项目属性,然后单击run,然后输入项目相关URL,如index.jsp。
- Project->Properties
- 项目- >属性
- Click on Run
- 点击运行
- Relative URL:/index.jsp (Select your project root URL)
- 相对URL:/索引。jsp(选择项目根URL)