简体   繁体   English

为什么在JSP中获得空值?

[英]Why do I get null values in JSP?

I want to use a web form to submit some information and then a JSP page will display the entered form information. 我想使用Web表单提交一些信息,然后JSP页面将显示输入的表单信息。 However when I clicked the submit button in the form, it goes the correct JSP file but all form values are displayed as "null". 但是,当我单击表单中的提交按钮时,它将进入正确的JSP文件,但是所有表单值都显示为“空”。 I'm using Jersey to do the POST request. 我正在使用Jersey来执行POST请求。

The form is: 形式是:

<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>

The code to do the requests is: 进行请求的代码是:

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

The JSP file displayed is confirm.jsp and its content is: 显示的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>

If I type the following address in a browser: 如果我在浏览器中键入以下地址:

http://localhost:8080/MyRestWS/rest/customer

It will show me the form.html with the form. 它将显示带有表单的form.html After I fill out information and click "Submit", it will go to the following address and display the JSP file as specified by the path: 填写信息并单击“提交”后,它将转到以下地址并显示路径指定的JSP文件:

http://localhost:8080/MyRestWS/rest/customer/created

The JSP file shows up correctly but all customer information fields are displayed as "null" like the following: JSP文件正确显示,但是所有客户信息字段都显示为“ null”,如下所示:

You've entered the following information:

Customer Name: null

Customer ID: null

Customer DOB: null

So why do I get null values in JSP after submitting the form? 那么,为什么在提交表单后在JSP中获得空值? What's wrong with my code? 我的代码有什么问题?

You're trying to display request parameters that no longer exist; 您正在尝试显示不再存在的请求参数。 they exist only for the duration of the first request, the form submission. 它们仅在第一个请求(表单提交)期间存在。

If you want to display them again you need to expose them to the view layer as request attributes . 如果要再次显示它们,则需要将它们作为请求属性公开给视图层。

(That said, I'd be happier if they were exposed as part of a constructed class.) (也就是说,如果将它们作为构造类的一部分公开,我会更高兴。)

Following Dave's suggestions, I've added the following request attribute in the createCustomer() method and used request.getAttribute() in the confirm.jsp to retrieve the form data entered. 遵循Dave的建议,我在createCustomer()方法中添加了以下请求属性,并在confirm.jsp使用了request.getAttribute()来检索输入的表单数据。 It finally works. 终于可以了。 The confirm.jsp file can display all information correctly after I submit the form. 提交表单后,confirm.jsp文件可以正确显示所有信息。

    request.setAttribute("name", name);
    request.setAttribute("dob", dob);
    request.setAttribute("id", Integer.valueOf(id));
    RequestDispatcher dispatcher = request.getRequestDispatcher("/confirm.jsp");
    dispatcher.forward(request, response);

However, one side question is: I had to use the following injection in the class fields: 但是,另一个问题是:我必须在类字段中使用以下注入:

@Context HttpServletRequest request;
@Context HttpServletResponse response;

If I use the following instead, I will get exception error: 如果我改用以下命令,则会出现异常错误:

@Context ServletRequest request;
@Context ServletResponse response;

Why is that? 这是为什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM