Configuring Liberty web applications to reread POST data
Your web application can reread the POST data of the request in scope by using the input stream, a reader, or parsed parameters. Set the web container property to use this reread behavior.
Before you begin
The Java™ servlet specification allows a web application to read POST data of a request by using an input stream, by using a reader, or by using parsed parameters. However, the web application can use only one of these methods in the scope of a request to read the POST data.
When you set the web container property, your web application can reread the POST data multiple times. However, a reader or an input stream must be closed to complete the read operation before a new read operation can be started. The data does not need to be fully read before a reader or input stream is closed. Partial reading of the data can be completed. For parsed parameters, the web container reads the entire POST data and then closes the read operation after all of the parameters are parsed.
Your application can do a non-blocking reread of POST data. For example, the application can do a non-blocking read by using the asynchronous read listener (AsyncReadListener), doing a close, and then doing a blocking read. The application can also do, for example, a non-blocking read by using the asynchronous read listener, doing a close, and then doing a second non-blocking read by using a new asynchronous read listener.
When your application does a subsequent read of the POST data by using a non-blocking read, the server first provides the data that was previously read. If the previous completed read did not read all of the post data that was sent in the request, the new read does a block when the application reads the remaining POST data.
About this task
You set the web container property to true. Then, you run a web application that can reread POST data. Examples are provided.
Procedure
Examples
Blocking read example
- Get the input stream by using the getInputStream () method.
- Read the data.
- Close the input stream.
- Get the new input stream by using the getInputStream () method.
- Read the data.
- Close the input stream.
- Get a parameter by using the getParameter () method.
- Get the reader by using the getReader () method.
- Read the data.
- Close the reader.
- Get the parameter again by using the getParameter () method.
java.io.InputStream in = request.getInputStream();
byte[] inBytes = new byte[5];
int readLen = in.read(inBytes);
if (readLen > 0){
}
if (close)
in.close();
java.io.InputStream in2 = request.getInputStream();
byte[] inBytes = new byte[2048];
for (int n; (n = in2.read(inBytes, 0, 2048)) != -1;) {
readLen += n;
}
if (close)
in2.close();
String param = request.getParameter("Test");
java.io.BufferedReader rdr = request.getReader();
StringBuffer inBuffer = new StringBuffer();
char[] inChars = new char[1024];
for (int n; (n = rdr.read(inChars,0,1024)) != -1;) {
inBuffer.append(new String(inChars, 0, n));
}
rdr.close();
String param2 = request.getParameter("Test");
Non-blocking read example
- Get the input stream by using the getInputStream () method.
- Get the listener from the servlet input stream by using the setReadListener () method.
- Read the data in the onDataAvailable () method.
- Close the input stream.
After the input stream closes, the onAllDataRead () method and the onError () method are not called for this listener.
- Get a parameter by using the getParameter () method.
- Get the new input stream by using the getInputStream () method.
- Get a new listener from the servlet input stream by using the setReadListener () method.
- Read the data in the onDataAvailable () method.
- If all the data is read, then the onAllDataRead () method is called for this listener.
java.io.InputStream input = request.getInputStream();
RL1 = ((ServletInputStream) input).setReadListener(readListener);
RL1
onDataAvailable(){
if (input.isReady()) {
Len = input.read(inBytes);
}
input.close();
String param = request.getParameter("Test");
java.io.InputStream input = request.getInputStream();
RL2 = ((ServletInputStream) input).setReadListener(readListener);
}
RL1 // onAllDataRead() or onError() will not be called for RL1, as the stream has been closed for RL1
RL2
onDataAvailable(){
if (input.isReady()) {
Len = input.read(inBytes);
}
}
RL2 // If all data read
onAllDataRead(){
AsncContext.complete();
}