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

3 Answers3

1

There are two problems:

  • Your servlet maps to the URL /ValidationServlet and your form has the action set to ServletValidation.

  • Maybe your login.jsp isn't at the same level of your servlet mapping.

The best solution would be setting the action of your form to map the full URL that maps to your servlet. This can be achieved using Request#getContextPath():

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

If you don't use JSTL in your project, then do it. You should avoid scriptlets in your jsp (those <% ... %> tags that hold nasty Java code inside the JSP). But if you don't, then you should try the following:

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

Still, the first way is the best to go.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I have the jsp file inside the jsp folder in WebContent, and then I edited like this
    And now displays the same error but now says the resource (/ValidationServlet) is not available
    – user1751981 Oct 17 '12 at 05:05
  • @user1751981 it looks like you don't have JSTL in your project or that your Web Application Server doesn't handle EL. Visit our JSTL wiki page (link posted in the answer) to add the library in your project. If you don't want (or think you won't need it) you can use the second way, but I'm warning you that using scriptlets is not the way to go. – Luiggi Mendoza Oct 17 '12 at 05:09
  • Yes, that worked, i use the contextPath and move from there and was able to find it. Thanks. – user1751981 Oct 17 '12 at 05:10
  • @user1751981 you're welcome. Please don't forget to mark this post as an answer. – Luiggi Mendoza Oct 17 '12 at 05:10
1

Just change the web.xml file

<url-pattern>/ServletValidation</url-pattern>
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
0

ServletValidation is different from ValidationServlet.

joy
  • 327
  • 4
  • 11
  • Yes, I forgot to modify the jsp in here, but I have the same as the name of the Servlet in the action now. And still doesn't work – user1751981 Oct 17 '12 at 04:57
  • ok, you need to give the relative path to the servlet. something like ACTION="/bt/ValidationServlet" – joy Oct 17 '12 at 05:01
  • @joy_jedi that won't solve the problem if you have pages in deeper level. Check my answer to get the full path and then the relative path for setting the action in the form. – Luiggi Mendoza Oct 17 '12 at 05:03