JSP Servlet显示不存在的数据

时间:2013-02-20 20:35:41

标签: java jsp

我创建了一个简单的servlet,它打印了一些这样的消息:

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

    }

一切都运作良好。 然后我创建了两个像这样的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>

servlet现在看起来像这样:

@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天前我做过的例子中的“你好”。无论我做什么,我都能看到。 我需要清洁什么? 我错过了什么?

编辑:我使用eclipse和tomcat 7

1 个答案:

答案 0 :(得分:3)

假设你在Eclipse中,停止你的Tomcat服务器,选择你的项目,在Eclipse的菜单中选择Project和Clean,然后选择Build Project。然后重新启动Tomcat服务器并再试一次。

Eclipse每次运行时都会使用最后构建的项目版本和Tomcat。您需要清理项目并为Eclipse和Tomcat重建它以刷新更改。