EPractice Lab Online Skill Evaluation Lab uses descriptive type questions to support image and text answer input. Examinee can also draw and upload their images from local system. To support this requirement a developer decided to use Java Servlet.
Which of the following statements about the client request and the servlet are true, considering request is of type HttpServletRequest?
A. The client request can be either "GET" or "Post"
B. The method request.getReader() should be used to retrieve the file data.
C. The client request must be Post
D. The method request.getInputStream() should be used to retrieve the file data.
E. The method request.getParameterValues(String reqParameter) should be used to retrieve the file data.
Answers : C, D
"Get" uses the URL to transfer the parameters and hence can be used only for textual data. Also the amount of data that can be sent by "Get" is limited. On the other hand "POST" uses the body of the request to send the data and there is no limit in the size of the data that can be transferred. So, "POST" is ideal for file uploads.
The servletRequest has the following two methods, which can be used to read the body of the request:
BufferedReader getReader() : Retrieves the body of the request as character data using a BufferedReader
ServletInputStream getInputStream(): Retrieves the body of the request as binary data using a ServletInputStream.
For file uploads, the encoding type of the request should be specified as "multipart/form-data". The container will populate the parameter map only when the encoding type is "application/x-www-form-urlencoded" (which is the default when encoding type is unspecified.)