2

それはおそらくばかげた質問ですが、私は本当に行き詰まっており、あなたの助けが必要です. CRUD デモ アプリのリクエストを処理する Java サーブレットを作成しました。これは私のサーブレットです:

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");
    }
}

そして、これは私の web.xml ファイルです:

<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>

しかし、http://localhost:8080/listAuthor/newAuthorにアクセスしようとすると、 Postman から 404 エラーが返されます。私は何を間違っていますか?私はこのマッピングで完全に迷っており、機能させることができません。http://localhost:8080/listAuthorにアクセスして、作成者のテーブルを一覧表示できますが、新しいテーブルを追加できません。

4

0 に答える 0