21
Tôi đang cố truy xuất một số tham số được chuyển tới jax-rs từ một biểu mẫu đã đăng với HttpServletRequest. Tuy nhiên, đối tượng yêu cầu của tôi luôn trả về giá trị null cho các tham số của tôi. Tôi sẽ không nói về điều này đúng không? Tôi đã đăng mã bên dưới, cùng với một yêu cầu mẫu được gửi đi.jax-rs lấy các tham số biểu mẫu
Đây là dịch vụ của tôi:
@Path("/")
@Stateless
public class HomeController {
@Context
private HttpServletRequest request;
@Context
private HttpServletResponse response;
@EJB
private LoginServiceLocal loginService;
@POST
@Path("/authenticate")
@Consumes("application/x-www-form-urlencoded")
public void authenticate() throws Exception {
String email = request.getParameter("email");
String password = request.getParameter("password");
if (loginService.authenticate(email, password)) {
response.sendRedirect("/app");
} else {
request.setAttribute("authenticationError", "Invalid email/password.");
}
}
}
Ví dụ yêu cầu:
POST http://localhost:8081/cheetah-web/authenticate HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://localhost:8081/cheetah-web/login
Cookie: JSESSIONID=a4e7aec0624206ad33754e35cce4
Content-Type: application/x-www-form-urlencoded
Content-Length: 39
email=unit%40test.com&password=testpass
Làm thế nào để lấy tất cả các params hình thức cùng một lúc như chúng ta có thể làm cho truy vấn params sử dụng UriInfo.getQueryParameters() –
Bạn sẽ sử dụng MultivaluedMap làm tham số. Ví dụ, xác thực void công khai (MultivaluedMap form) {...} –