1

I have the following problem when trying to use the login.jsp I have the following code in the login

<%@ 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>

And the following text in the 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>

But once I click the button for submit it returns:

State HTTP 404 - /bt/jsp/ServletValidation

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

The folder structure is the following:

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

The problem I'm finding is why is it sending to that URL

4

3 回答 3

1

有两个问题:

  • 您的 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>

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

更多信息:

于 2012-10-17T04:50:07.433 回答
1

只需更改 web.xml 文件

<url-pattern>/ServletValidation</url-pattern>
于 2012-10-17T04:52:48.447 回答
0

ServletValidation 与 ValidationServlet 不同。

于 2012-10-17T04:49:33.327 回答