Java HTTP 状态 404 - 调用 Servlet 时未找到

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

HTTP Status 404 - Not Found on call of Servlet

javajspservletshttp-status-code-404

提问by Malwinder Singh

I am getting HTTP Status 404 when i call Servlet from JSP page.

当我从 JSP 页面调用 Servlet 时,我收到 HTTP 状态 404。

Here is web.xml :

这是 web.xml :

<servlet>
    <servlet-name>Register</servlet-name>
    <servlet-class>servlets.dataio.registration.Register</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Register</servlet-name>
    <url-pattern>/Register</url-pattern>
</servlet-mapping>

Here is effective code of JSP:

下面是 JSP 的有效代码:

<form name="admin-form" action="Register" method="POST"> 

Error message:

错误信息:

HTTP Status 404 - Not Found

type Status report

message: Not Found

description: The requested resource is not available.

Directory structure is:

目录结构为:

Web Pages --> Pages --> Registration --> registerpage.jsp

I have tried Clean and Building project. I am using NetBeans IDE and GlassFish server

我尝试过清洁和建筑项目。我正在使用 NetBeans IDE 和 GlassFish 服务器

采纳答案by lxcky

Try:

尝试:

action="../../Register"

To make your page relative to where the Servlet is.

使您的页面相对于 Servlet 所在的位置。

../means one directory up. And based on the directory structure you've posted, it's two folders down under the Web Pagesso you'll need two ../to make it relative to where your Servlet is, which is, based <url-pattern>/Register</url-pattern>meaning just under Web Pages

../表示向上一个目录。并且根据您发布的目录结构,它在 下有两个文件夹,Web Pages因此您需要两个文件夹../才能使其相对于您的 Servlet 所在的位置,也就是,基于的<url-pattern>/Register</url-pattern>意思就在Web Pages

回答by p_xr

try action="/Register"

尝试操作=“/注册”

and this line is 'cause message must be at least 30 chars

这一行是因为消息必须至少有 30 个字符

回答by Gas

The actionattribute is using by default relative path, so if your jsp is available at http://yourHost/yourApp/pathToJsp/registerpage.jspthen your relative servlet path is http://yourHost/yourApp/pathToJsp/Registerwhich is not correct.

action属性默认使用相对路径,因此如果您的 jsp 可用,http://yourHost/yourApp/pathToJsp/registerpage.jsp那么您的相对 servlet 路径http://yourHost/yourApp/pathToJsp/Register是不正确的。

You have two choices:

你有两个选择:

  • use relative path like J.Lucky suggested - but its error prone, as you have to fix it in every jsp, or if you move jsp in you directory structure
  • use absoulute path - start with / - but you need to include your context-root name, like this - /yourContext/Register. If you want to avoid hard coding context root you can use request.getContextPath() method, like this action='<%=request.getContextPath()%>/Register'
  • 使用 J.Lucky 建议的相对路径 - 但它容易出错,因为您必须在每个 jsp 中修复它,或者如果您在目录结构中移动 jsp
  • 使用绝对路径 - 以 / 开头 - 但您需要包含您的上下文根名称,例如 - /yourContext/Register。如果你想避免硬编码上下文根,你可以使用 request.getContextPath() 方法,像这样action='<%=request.getContextPath()%>/Register'