我想使用Web表單來提交一些信息,然後JSP頁面將顯示輸入的表單信息。但是,當我單擊表單中的提交按鈕時,它將轉到正確的JSP文件,但所有表單值都顯示爲「null」。我使用Jersey來執行POST請求。爲什麼我會在JSP中獲得空值?
形式爲:
<form action="/MyRestWS/rest/customer/created" method="POST">
<table border="1">
<tr>
<td>Customer name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Customer ID:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>Customer DOB:</td>
<td><input type="text" name="dob"></td>
</tr>
</table>
<br/>
<input type="submit" value="Submit">
</form>
做請求的代碼是:
@Path("/customer")
public class CustomerService {
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("created")
public Response createCustomer(@FormParam("id") int id, @FormParam("name") String name, @FormParam("dob") Date dob) {
Response r;
r = Response.ok().entity(new Viewable("/confirm.jsp")).build();
return r;
}
@GET
@Produces(MediaType.TEXT_HTML)
public Viewable displayForm() {
return new Viewable("/form.html");
}
}
顯示的JSP文件是confirm.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>Your entered information</title>
</head>
<body>
<h2>
<%
out.println("You've entered the following information:");
%>
</h2>
<p>
Customer Name:
<%=request.getParameter("name")%></p>
<p>
Customer ID:
<%=request.getParameter("id")%></p>
<p>
Customer DOB:
<%=request.getParameter("dob")%></p>
</body>
</html>
如果我在瀏覽器中鍵入以下地址:
http://localhost:8080/MyRestWS/rest/customer
它會告訴我form.html
的形式。我填寫信息,然後單擊後「提交」,它會去以下地址,並通過指定的路徑顯示JSP文件:
http://localhost:8080/MyRestWS/rest/customer/created
的JSP文件顯示正確,但所有的客戶信息字段顯示爲「null」如下:
You've entered the following information:
Customer Name: null
Customer ID: null
Customer DOB: null
那麼爲什麼我在提交表單後在JSP中得到空值?我的代碼有什麼問題?
**無關:** [在JSP中使用scriptlets非常令人沮喪](http://stackoverflow.com/a/3180202/814702) – informatik01
@ informatik01:感謝您的建議。我只是使用JSP來獲得快速原型。 – tonga
我明白了。我寫了它以防萬一)) – informatik01