2

私はJSPの初心者で、次のサーブレットを持っています:

@SuppressWarnings("serial")
public class HelloAppIgorServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        RequestDispatcher disp = req.getRequestDispatcher("/mainpage.jsp");
        disp.forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter pw = resp.getWriter();
        pw.print("Test");
        pw.close();
    }
}

そして、mainpage.jsp という名前の 1 つの JSP ファイル:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <p>Hello there!<p>
    <form action="." method="post">
        <input type="text" name="name1" />
        <input type="submit" value="name2" />
    </form>
</body>
</html>

問題は、doPost() メソッドが機能しないことです。ボタンをクリックすると index.html ページにリダイレクトされます。問題はサーブレットにはないと確信していますが、どこにあるのでしょうか?

4

1 に答える 1

1

で適切なアクションを設定する必要があります<form action="." method="post">。アクションは、 で定義したサーブレットの (相対) URL<servlet-mapping>ですweb.xml

于 2013-07-10T11:33:39.913 に答える