我想使用 web 表单提交一些信息,然后一个 JSP 页面将显示输入的表单信息。但是,当我单击表单中的提交按钮时,它进入了正确的 JSP 文件,但所有表单值都显示为“null”。我正在使用 Jersey 来执行 POST 请求。
表格是:
<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 中会得到空值呢?我的代码有什么问题?