Why do I get null values in JSP?

若如初见. 提交于 2019-12-13 21:39:17

问题


I want to use a web form to submit some information and then a JSP page will display the entered form information. However when I clicked the submit button in the form, it goes the correct JSP file but all form values are displayed as "null". I'm using Jersey to do the POST request.

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:

<%@ 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. 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:

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:

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? What's wrong with my code?


回答1:


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




回答2:


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. It finally works. The confirm.jsp file can display all information correctly after I submit the form.

    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?



来源:https://stackoverflow.com/questions/16798398/why-do-i-get-null-values-in-jsp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!