-1

次のようなメッセージを出力する単純なサーブレットを作成しました。

@WebServlet("/servletExample")
public class ServletExample extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

        PrintWriter out = response.getWriter();
        out.println("Hello there");

    }

すべてがうまくいきました。次に、次のような2つのjspページを作成しました。

<body>
    <form method="post" action="servletExample">
        <table border="0">
            <tr>
                <td>
                    First name:
                </td>
                <td>
                    <input type="text" name="firstname"/>
                </td>               
            </tr>
            <tr>
                <td>
                    Last name:
                </td>
                <td>
                    <input type="text" name="lastname"/>
                </td>               
            </tr>   
            <tr>
                <td colspan="2">
                    <input type="submit" value="Submit"/>
                </td>               
            </tr>
        </table>
    </form>
</body>

<body>
    <%
        String firstName = (String)request.getAttribute("firstname");
        String lastName = (String)request.getAttribute("lastname");

        out.println(firstName+ " "+lastName);
    %>
</body>

サーブレットは次のようになります。

@WebServlet("/servletExample")
public class ServletExample extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

        String firstName=request.getParameter("firstname");
        String lastName=request.getParameter("lastname");

        if(firstName == null || lastName==null){
            getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
            return;
        }

        request.setAttribute("firstname", firstName);
        request.setAttribute("lastname", lastName);
        getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
    }
}

実行するとそのフォームが表示されますが、送信すると2日前に行った例の「Hellothere」が表示されます。私が何をするにしても、私はそれを見ます。何を掃除する必要がありますか?私は何が欠けていますか?

編集:私はEclipseとTomcat7を使用しています

4

1 に答える 1

3

Eclipseを使用していると仮定して、Tomcatサーバーを停止し、プロジェクトを選択します。Eclipseのメニューで[プロジェクト]を選択してから、を選択しCleanますBuild Project。次に、Tomcatサーバーを再起動して、再試行します。

Eclipseは、実行するたびに、Tomcatで最後にビルドされたプロジェクトバージョンを使用します。変更を更新するには、プロジェクトをクリーンアップし、EclipseとTomcat用に再構築する必要があります。

于 2013-02-20T20:38:42.150 に答える