Servlet giving error 404

Asked

Viewed 249 times

1

I’m trying to create a posting application, I started studying the Java Web now and every time I run Servlet on Apache 8 it appears a 404 error page.

my life

 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 him 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 of the same error 404.

Any idea?

  • The action should point to Servlet and not action="post"

  • When I use the action="post" it sends to the correct url of Servlet, but presents 404. When I go into the eclipse and have Servlet run directly, give the same error.

1 answer

0


The url for your Servlet is /user/post.

According to the javadoc of @WebServlet, the properties value and urlPatterns which are used to map the Servlet URL. The name is just the name of 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>
  • When I do this it sends directly to localhost:8080/user/post and not localhost:8080/Twitterweb4/user/post. also told me to try "user/post" in the.da flame, but then call me a localhost:8080/user/user/post.

  • I edited my answer, see if it solves your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.