Servlet is giving 404 error

1

I am trying to create a post application, I started to study Java Web and every time I run the servlet in Apache 8, a 404 error page appears.

My servlet

 package app.web4.servlets;

 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;

 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 import app.web4.model.Post;

 @WebServlet(name="post", urlPatterns="/user/post")
 public class postServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public postServlet() 
{

}


protected void doGet(HttpServletRequest request, HttpServletResponse response)

        throws ServletException, IOException {

    createPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    createPost(request, response);
}

private void createPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String postText = request.getParameter("postText");
    String userName = "";

    if (request.getUserPrincipal() != null) {
        userName = request.getUserPrincipal().getName();
    }

    try {
        Post post = new Post(postText, userName);

        List<Post> posts = (List<Post>) getServletContext().getAttribute("posts");

        if (posts == null) {
            posts = new ArrayList<>();
            posts.add(post);
            getServletContext().setAttribute("posts", posts);
        }else {
            posts.add(post);
            getServletContext().setAttribute("posts", posts);
        }

        request.setAttribute("posts", posts);


        request.getRequestDispatcher("/WEB-INF/view/home.jsp").forward(request, response);

    } catch (Exception e) {
        throw new NumberFormatException("Houve algum erro com o seu post");
    }
}
 }

The call I make to it through the NewPost.jsp file

    <div class="container ">
    <form action="post" method="POST" class="form-inline">
        <input name="postText" type="text"
            class="form-control mb-2 mr-sm-2 mb-sm-0"
            placeholder="What's on your mind?">
        <button type="submit" class="btn btn-primary">Post</button>
    </form>
</div>

I tested with simpler servlets, which would only lead to a page with a "hi", but with the same 404 error.

Any ideas?

    
asked by anonymous 25.10.2017 / 14:05

1 answer

0

The url for your Servlet is /user/post .

According to javadoc of @WebServlet , the properties value and urlPatterns that serve to map the URL of the Servlet. The name is just the name of the Servlet.

<div class="container ">
    <form action="/user/post" method="POST" class="form-inline">
        <input name="postText" type="text"
            class="form-control mb-2 mr-sm-2 mb-sm-0"
            placeholder="What's on your mind?">
        <button type="submit" class="btn btn-primary">Post</button>
    </form>
</div>

If you are using JSP in the view:

<form action="${pageContext.request.contextPath}/user/post" method="POST" class="form-inline">
    ...
</form>
    
25.10.2017 / 14:16