gpt4 book ai didi

java - 所需的资源(servlet)不可用

转载 作者:行者123 更新时间:2023-11-30 04:38:13 24 4
gpt4 key购买 nike

尝试使用login.jsp时遇到以下问题我在登录中有以下代码

<%@ include file="/jsp/include.jsp"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
function sendForm() {
document.formLogin.submit();
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Example :: Spring Application</title>
</head>
<body>

<div class="container">
<form class="bs-docs-example form-horizontal"
action="ServletValidation" name="formLogin" id="formLogin"
method="post">

<legend>Login</legend>
<div class="control-group">
<label for="inputUsername" class="control-label">Email</label>

<div class="controls">
<input type="text" id="inputUsername">
</div>
</div>
<div class="control-group">
<label for="inputPassword" class="control-label">Password</label>
<div class="controls">
<input type="password" id="inputPassword">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox"> <input type="checkbox">
Remember me
</label>
<button class="btn" type="submit" action="sendForm();">Sign
in</button>
</div>
</div>
</form>
</div>

</body>
</html>

以及 web.xml 中的以下文本

  <servlet>
<description></description>
<display-name>ValidationServlet</display-name>
<servlet-name>ValidationServlet</servlet-name>
<servlet-class>bt.servlet.ValidationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidationServlet</servlet-name>
<url-pattern>/ValidationServlet</url-pattern>
</servlet-mapping>

但是一旦我单击提交按钮,它就会返回:

State HTTP 404 - /bt/jsp/ServletValidation

Description The resource required (/bt/jsp/ServletValidation) is not available.

文件夹结构如下:

+bt
-src
-WebContent
-jsp
-resources
-WEB-INF
-classes
*web.xml
*index.jsp

我发现的问题是为什么它发送到该 URL

最佳答案

有两个问题:

  • 您的 servlet 映射到 URL /ValidationServlet并且您的表单的操作设置为 ServletValidation .

  • 也许你的login.jsp与您的 servlet 映射不在同一级别。

最好的解决方案是设置表单的操作来映射映射到 servlet 的完整 URL。这可以使用 Request#getContextPath() 来实现:

<form action="${request.contextPath}/ValidationServlet" ...>
<!-- content... -->
</form>

如果您在项目中没有使用 JSTL,那就使用吧。您应该避免在 jsp 中使用 scriptlet(那些 <% ... %> 标签,它们在 JSP 中保存讨厌的 Java 代码)。但如果您不这样做,那么您应该尝试以下操作:

<form action="<%=request.getContextPath()%>/ValidationServlet" ...>
<!-- content... -->
</form>

不过,第一种方法是最好的选择。

更多信息:

关于java - 所需的资源(servlet)不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12927264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com