0
@WebServlet("/")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private UserDAO userDAO;

    public void init() {
        userDAO = new UserDAO();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getServletPath();

        try {
            switch (action) {
                case "/new":
                    showNewForm(request, response);
                    break;
                case "/insert":
                    insertUser(request, response);
                    break;
                case "/delete":
                    deleteUser(request, response);
                    break;
                case "/edit":
                    showEditForm(request, response);
                    break;
                case "/update":
                    updateUser(request, response);
                    break;
                default:
                    listUser(request, response);
                    break;
            }
        } catch (SQLException ex) {
            throw new ServletException(ex);
        }
    }

    private void listUser(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException, ServletException {
        List<User> listUser = userDAO.selectAllUsers();
        request.setAttribute("listUser", listUser);
        RequestDispatcher dispatcher = request.getRequestDispatcher("user-list.jsp");
        dispatcher.forward(request, response);
    }

    private void showNewForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("user-form.jsp");
        dispatcher.forward(request, response);
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        User existingUser = userDAO.selectUser(id);
        RequestDispatcher dispatcher = request.getRequestDispatcher("user-form.jsp");
        request.setAttribute("user", existingUser);
        dispatcher.forward(request, response);
    }

    private void insertUser(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String country = request.getParameter("country");
        User newUser = new User(name, email, country);
        userDAO.insertUser(newUser);
        response.sendRedirect("list");
    }

    private void updateUser(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String country = request.getParameter("country");

        User book = new User(id, name, email, country);
        userDAO.updateUser(book);
        response.sendRedirect("list");
    }

    private void deleteUser(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        userDAO.deleteUser(id);
        response.sendRedirect("list");
    }
}

This is my servlet and I believe I am having a problem with my goPost & my doGet.

Currently, my index page is the user-list.JSP, and when I click on any of the buttons on the page to turn it into "/new" etc.

I am not able to switch to the "user-form.JSP".

I have been staring at this thing for a while and am unsure what to move on from here. I will continue to look into this problem, hopefully, I can find something wrong with the Request Dispatcher, but I am not sure.

I am getting: HTML status report 404 - Not Found

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
HeyGilly
  • 1
  • 1
  • Where exactly did you read/learn that you should be mapping the servlet on an URL pattern of `"/"`? – BalusC Nov 28 '22 at 08:45
  • @BalusC Is that frowned upon? I have learned that from other developers and some walkthroughs. [Java Guides](https://www.javaguides.net/2019/03/jsp-servlet-jdbc-mysql-crud-example-tutorial.html) is one of the places I saw this as well. – HeyGilly Nov 28 '22 at 15:59
  • 1
    Wow. Add that site to your blacklist. Explanation: https://stackoverflow.com/a/4140659. Better you get started here https://stackoverflow.com/tags/servlets/info and then advance through the "Front controller pattern" link over there. – BalusC Nov 28 '22 at 17:52

0 Answers0