Why can't I redirect correctly my Java Servlet?

Viewed 69

it is probably a dumb question, but I am really stuck and need your help. I have made a Java Servlet which is handling requests for a CRUD demo app. This is my servlet:

public class AuthorController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private AuthorDAO authorDAO;

    public void init() {
        String jdbcURL      = getServletContext().getInitParameter("jdbcURL");
        String jdbcUsername = getServletContext().getInitParameter("jdbcUsername");
        String jdbcPassword = getServletContext().getInitParameter("jdbcPassword");

        authorDAO = new AuthorDAO(jdbcURL, jdbcUsername, jdbcPassword);

    }

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

        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getServletPath();
        System.out.println(action);
        try {
            switch (action) {
            case "/newAuthor":
                showNewForm(request, response);
                break;
            case "/insertAuthor":
                insertAuthor(request, response);
                break;
            case "/deleteAuthor":
                deleteAuthor(request, response);
                break;
            case "/editAuthor":
                showEditForm(request, response);
                break;
            case "/updateAuthor":
                updateAuthor(request, response);
                break;
            default:
                listAuthor(request, response);
                break;
            }
        } catch (SQLException ex) {
            throw new ServletException(ex);
        }
    }

    
    /* List all authors */
    private void listAuthor(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException, ServletException {
        List<Author> listAuthor = authorDAO.listAllAuthors();
        request.setAttribute("listAuthors", listAuthor);
        RequestDispatcher dispatcher = request.getRequestDispatcher("AuthorList.jsp");
                
        dispatcher.forward(request, response);
    }
    
    
    /* Show authors form */
    private void showNewForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("AuthorForm.jsp");
        
        dispatcher.forward(request, response);
    }
    
    
    /* Show authors edit form */
    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, ServletException, IOException {
        int id               = Integer.parseInt(request.getParameter("id"));
        Author existingAuthor        = authorDAO.getAuthor(id);
        RequestDispatcher dispatcher = request.getRequestDispatcher("AuthorForm.jsp");
        
        request.setAttribute("author", existingAuthor);
        
        dispatcher.forward(request, response);
    }
    

    /* Inserting a new author */
    private void insertAuthor(HttpServletRequest request, HttpServletResponse response) 
            throws SQLException, IOException {
        String author_name    = request.getParameter("author_name");
        int author_age        = Integer.parseInt(request.getParameter("author_age"));
        String author_country = request.getParameter("author_country");

        Author newAuthor = new Author(author_name, author_age, author_country);
        authorDAO.insertAuthor(newAuthor);
        
        response.sendRedirect("listAuthor");
    }

    
    /* Updating an existing author */
    private void updateAuthor(HttpServletRequest request, HttpServletResponse response) 
            throws SQLException, IOException {
        int id        = Integer.parseInt(request.getParameter("id"));
        String author_name    = request.getParameter("author_name");
        int author_age        = Integer.parseInt(request.getParameter("author_age"));
        String author_country = request.getParameter("author_country");

        Author author = new Author(id, author_name, author_age, author_country);
        authorDAO.updateAuthor(author);
        
        response.sendRedirect("listAuthor");
    }

    
    /* Deleting an existing author */
    private void deleteAuthor(HttpServletRequest request, HttpServletResponse response) 
            throws SQLException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));

        Author author = new Author(id);
        authorDAO.deleteAuthor(author);
        
        response.sendRedirect("listAuthor");
    }
}

And this is my web.xml file:

<servlet>
    <servlet-name>AuthorController</servlet-name>
    <servlet-class>org.demo.AuthorController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>AuthorController</servlet-name>
    <url-pattern>/listAuthor</url-pattern>
</servlet-mapping>

However, when I try to access http://localhost:8080/listAuthor/newAuthor Postman gives me a 404 error. What am I doing wrong? I am totally lost with this mapping and can't make it work. I am able to access: http://localhost:8080/listAuthor and list my table with authors, but can't add a new one.

0 Answers
Related