Вопрос

It's my first Java EE "app". I am a little bit confused... After googling I think I'm doing everything correct, but if it was so, I wouldn't come across problems : )

My form under save.jsp:

<body>
    <form method="post" action="${pageContext.request.contextPath}/save">
        <table>
            <tr>
                <td><label for="idTxtName" >Name:</label></td>
                <td><input type="text" id="idTxtName" name="name"/></td>
            </tr>
            <tr>
                <td><label for="idTxtSurname" >Surname:</label></td>
                <td><input type="text" id="idTxtSurname" name="surname"/></td>
            </tr>
                    ... etc...
            <tr>
                <td></td>
                <td><input type="submit" value="Save person" /></td>
            </tr>
        </table>
    </form>
</body>

I've tried also:

form method="post" action="/save"

as well as

form method="post" action="save"

My Servlet:

package servlets;

import java.io.IOException;
import java.sql.SQLException;
import // etc

    public class SaveServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
                       // some actions
        }
    }

And web.xml mapping:

  <servlet>
    <servlet-name>SavingServlet</servlet-name>
    <servlet-class>servlets.SaveServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>SavingServlet</servlet-name>
    <url-pattern>/save</url-pattern>
  </servlet-mapping>

After submitting my save.jsp form I see a classic message:

HTTP Status 404 - /SImpleCRUD/save
type Status report
message /SImpleCRUD/save
description The requested resource is not available.

What am I doing wrong? Is my mapping incorrect?

Это было полезно?

Решение

Some times it's happen.There is nothing wrong in your code. But just restart your server and run again. May be it will work fine.

In JSP action="SavingServlet" and @WebServlet("/SavingServlet") Configure this annotation in class level of your Servlet. Now no need to configure your servlet in web.xml

Другие советы

I am not sure about this is a proper way to solve this error. But Its works for me.

Change your web.xml to like this and try

<servlet>
    <servlet-name>SavingServlet</servlet-name>
    <jsp-file>/save.jsp</jsp-file>
  </servlet>

  <servlet-mapping>
    <servlet-name>SavingServlet</servlet-name>
    <url-pattern>/save</url-pattern>
  </servlet-mapping>

Its Working fine in my machine.

Your form's action should have only save:

<form method="post" action="save">

Without the context ('${pageContext.request.contextPath}/')

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top