Java Web App表单操作提交按钮不起作用

时间:2017-11-17 19:42:52

标签: java jsp

我有一个HTTPServlet如下,它有一个包含4个输入文本和一个提交按钮的表单:

@WebServlet("/addalbum")
public class AddAlbumServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html lang=\"en\">\n" +
"<head>\n" +
"    <meta charset=\"UTF-8\">\n" +
"    <title>Welcome to AAVA Music Studios</title>\n" +
"    <link rel=\"stylesheet\" href=\"https://fonts.googleapis.com/icon?family=Material+Icons\">\n" +
"    <link rel=\"stylesheet\" href=\"https://code.getmdl.io/1.3.0/material.indigo-pink.min.css\">\n" +
"    <script defer src=\"https://code.getmdl.io/1.3.0/material.min.js\"></script>\n" +
"</head>\n" +
"<body>\n" +
"<!-- The drawer is always open in large screens. The header is always\n" +
"   shown, even in small screens. -->\n" +
"<style>\n" +
"    .demo-layout-transparent {\n" +
"        background: #00FFFF center / cover;\n" +
"    }\n" +
"    .demo-layout-transparent .mdl-layout__header,\n" +
"    .demo-layout-transparent .mdl-layout__drawer-button {\n" +
"        /* This background is dark, so we set text to white. Use 87% black instead if\n" +
"           your background is light. */\n" +
"        color: white;\n" +
"    }\n" +
"</style>\n" +
"<div class = \"demo-layout-transparent mdl-layout mdl-js-layout mdl-layout--fixed-header mdl-layout--fixed-tabs\">\n" +
"    <header class = \"mdl-layout__header\">\n" +
"        <!-- Top row, always visible -->\n" +
"        <div class = \"mdl-layout__header-row\">\n" +
"            <!-- Title -->\n" +
"            <span class = \"mdl-layout-title\">Welcome to AAVA Music Studios</span>\n" +
"        </div>"+
"    </header>\n" +
"\n" +
"    <main class = \"mdl-layout__content\">\n" +
"        <section class = \"mdl-layout__tab-panel is-active\" id = \"scroll-tab-4\">\n" +
"            <div class = \"page-content\">");
            out.println("<form action=\"add\" method=\"post\">\n" +
"Album ID:<br>\n" +
"<input type=\"text\" name=\"album_id\">\n" +
"<br>\n" +
"Title:<br>\n" +
"<input type=\"text\" name=\"title\">\n" +
"<br>\n" +
"Copyright Date:<br>\n" +
"<input type=\"text\" name=\"copyright_date\">\n" +
"<br>\n" +
"Format:<br>\n" +
"<input type=\"text\" name=\"format\">\n" +
"<br>\n" +
"<br>\n" +
"<input type=\"submit\" name=\"submit\">\n" +
"</form> ");
            out.println("</div>\n" +
"        </section>\n" +
"    </main>\n" +
"</div>\n" +
"\n" +
"</body>\n" +
"</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        if (request.getParameter("submit") != null) {
            System.out.println("HELLO");
            request.getRequestDispatcher("/WEB-INF/addalbum.jsp").forward(request, response);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

我希望“提交”按钮可以调用addalbum.jsp页面。

这是WEB-INF文件夹中的addalbum.jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="music.Album"%>
<%@page import="connection.DataConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%
            music.MusicHandler.addAlbum(new DataConnection().getConnection(), new Album(Integer.parseInt(request.getParameter("album_id")) , request.getParameter("title") , new SimpleDateFormat("YYYY-MM-dd").parse(request.getParameter("copyright_date")) , request.getParameter("format")));
        %>
    </body>
</html>

问题是我希望Submit按钮调用addalbum.jsp页面,然后在其中执行addAlbum函数。它没有发生,因为即使点击按钮,我也没有得到&#34; HELLO&#34;在控制台输出中。请帮忙

0 个答案:

没有答案