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"
– rray
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.
– Norton Dos Santos