2013-05-28 3 views
0

웹 양식을 사용하여 정보를 제출하고 JSP 페이지에 입력 된 양식 정보가 표시됩니다. 그러나 양식의 제출 단추를 클릭하면 올바른 JSP 파일로 이동하지만 모든 양식 값은 "null"로 표시됩니다. Jersey를 사용하여 POST 요청을하고 있습니다.JSP에서 null 값을 얻는 이유는 무엇입니까?

양식은 다음과 같습니다

<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이 표시됩니다.

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

JSP 파일이 제대로 표시되지만 모든 고객 정보 필드는 다음과 같이 표시됩니다 : 나는 정보를 입력하고 클릭 한 후에는 다음 주소로 이동 경로에 의해 지정된 JSP 파일을 표시합니다 "제출" 다음과 같은 "null":

You've entered the following information: 

Customer Name: null 

Customer ID: null 

Customer DOB: null 

그런데 왜 양식을 제출 한 후 JSP에서 null 값을 얻습니까? 내 코드에 무슨 문제가 있습니까?

+1

** 관련 없음 ** [JSP로 스크립틀릿을 사용하는 것이 좋습니다] (http://stackoverflow.com/a/3180202/814702) – informatik01

+1

@ informatik01 : 귀하의 제안에 감사드립니다. 빠른 프로토 타입을 얻기 위해 JSP를 사용하고 있습니다. – tonga

+0

나는 본다. 내가 경우에 그것을 썼습니다)) – informatik01

답변

3

더 이상 존재하지 않는 요청 매개 변수를 표시하려고합니다. 그들은 단지 첫 번째 요청, 양식 제출 기간 동안 존재합니다.

당신이 그들을 다시 당신이 속성 요청으로보기 층에 노출 할 필요가 표시하려면

. 데이브의 제안에 따라

(그들이 건설 클래스의 일환으로 노출 된 경우 즉, 내가 행복 할 거라고 말했다.)

+0

Dave에게 감사드립니다. 양식 매개 변수를 요청 속성으로 표시하는 예를 들어 주시겠습니까? – tonga

0

, 나는 createCustomer() 방법으로 다음과 같은 요청 속성을 추가하고,에 request.getAttribute()을 사용했습니다 confirm.jsp을 입력하여 입력 된 양식 데이터를 검색하십시오. 그것은 마침내 작동합니다. 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); 

그러나, 한쪽 질문은 : 나는 클래스 필드에서 다음 주사를 사용했다 :

@Context HttpServletRequest request; 
@Context HttpServletResponse response; 

내가 대신 다음 사용하는 경우, 나는 예외 오류 얻을 것이다 :

@Context ServletRequest request; 
@Context ServletResponse response; 

왜 그럴까요?

관련 문제