Wicket 6 - Capturing HttpServletRequest parameters

2019-03-04 13:38发布

问题:

USing Wicket 6.17 and servlet 2.5, I have a form that allows file upload, and also has ReCaptcha (using Recaptcha4j). When the form has ReCaptcha without file upload, it works properly using the code:

    final HttpServletRequest servletRequest = (HttpServletRequest ) ((WebRequest) getRequest()).getContainerRequest();
    final String remoteAddress = servletRequest.getRemoteAddr();
    final String challengeField = servletRequest.getParameter("recaptcha_challenge_field");
    final String responseField = servletRequest.getParameter("recaptcha_response_field");

to get the challenge and response fields so that they can be validated.

This doesn't work when the form has the file upload because the form must be multipart for the upload to work, and so when I try to get the parameters in that fashion, it fails.

I have pursued trying to get the parameters differently using ServletFileUpload:

    ServletFileUpload fileUpload = new ServletFileUpload(new DiskFileItemFactory(new FileCleaner()) );
    String response = IOUtils.toString(servletRequest.getInputStream());

and

    ServletFileUpload fileUpload = new ServletFileUpload(new DiskFileItemFactory(new FileCleaner()) );
    List<FileItem> requests = fileUpload.parseRequest(servletRequest);

both of which always return empty.

Using Chrome's network console, I see the values that I'm looking for in the Request Payload, so I know that they are there somewhere.

Any advice on why the requests are coming back empty and how to find them would be greatly appreciated.

Update: I have also tried making the ReCaptcha component multipart and left out the file upload. The result is still the same that the response is empty, leaving me with the original conclusion about multipart form submission being the problem.

回答1:

Thanks to the Wicket In Action book, I have found the solution:

MultipartServletWebRequest multiPartRequest = webRequest.newMultipartWebRequest(getMaxSize(), "ignored");
// multiPartRequest.parseFileParts(); // this is needed since Wicket 6.19.0+
IRequestParameters params = multiPartRequest.getRequestParameters();

allows me to read the values now using the getParameterValue() method.