-1
我創建了一個簡單的servlet,這是打印一些消息是這樣的:JSP Servlet的顯示inexisting數據
@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>
該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天前的例子中看到「Hello there」。無論我做什麼,我都會看到。 我需要清潔什麼? 我錯過了什麼?
編輯:我使用的Eclipse和Tomcat 7
謝謝。有效。 – 2013-02-20 20:41:06
是的,Eclipse有時會很煩人,總是不得不刷新。祝你好運! – 2013-02-20 20:41:40