I have the following code and get an exception. Can you help me what I am doing wrong?
package com.java400;
import com.ibm.as400.access.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyVetoException;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONArray;
public class CallLocalProgramController extends HttpServlet {
ArrayList<String> listdata;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String message = "";
// String pno = "816-3999637";
// String entity = "IB";
AS400 as400System = new AS400(server, user, pass);
ProgramCall program = new ProgramCall(as400System);
try {
// Initialize the name of the program to run.
String programName = "/QSYS.LIB/ILMARTINUS.LIB/RRFCFJAVA.PGM";
// Set up the 3 parameters.
ProgramParameter[] parameterList = new ProgramParameter[8];
// Parameter
AS400Text textData = new AS400Text(10, as400System);
parameterList[0] = new ProgramParameter(textData.toBytes("RF320R"));
textData = new AS400Text(10, as400System);
parameterList[1] = new ProgramParameter(textData.toBytes("ILMARTINUS"));
parameterList[2] = new ProgramParameter(30);
parameterList[3] = new ProgramParameter(30);
parameterList[4] = new ProgramParameter(30);
parameterList[5] = new ProgramParameter(30);
parameterList[6] = new ProgramParameter(30);
parameterList[7] = new ProgramParameter(50);
program.setProgram(programName, parameterList);
// Run the program.
if (program.run() != true) {
// Report failure.
System.out.println("Program failed!");
// Show the messages.
AS400Message[] messageList = program.getMessageList();
for (int i = 0; i < messageList.length; ++i) {
// Show each message.
System.out.println(messageList[i].getText());
// Load additional message information.
messageList[i].load();
//Show help text.
System.out.println(messageList[i].getHelp());
}
} // Else no error, get output data.
else {
textData = new AS400Text(50, as400System);
message = (String) textData.toObject(parameterList[7].getOutputData());
@SuppressWarnings("UnusedAssignment")
String[] fields = null;
String member = "";
try {
if (member.equals("")) {
member = "*FIRST";
}
} catch (Exception e) {
System.out.println("Error obtaining user input.");
System.exit(0);
}
try {
as400System.connectService(AS400.RECORDACCESS);
} catch (AS400SecurityException | IOException e) {
System.out.println("Unable to connect for record level access.");
System.out.println("Check the programmer's guide setup file for special instructions regarding record level access");
System.exit(0);
}
QSYSObjectPathName filePathName = new QSYSObjectPathName("ILMARTINUS", "RFP320R", member, "MBR"); //Note 2
SequentialFile theFile = new SequentialFile(as400System, filePathName.getPath()); //Note 3
AS400FileRecordDescription recordDescription = new AS400FileRecordDescription(as400System, filePathName.getPath());
try {
RecordFormat[] format = recordDescription.retrieveRecordFormat();
//Note 4
fields = format[0].getFieldNames();
theFile.setRecordFormat(format[0]); //Note 5
theFile.open(AS400File.READ_ONLY, 100, AS400File.COMMIT_LOCK_LEVEL_NONE); //Note 6
int count = 0;
@SuppressWarnings("UnusedAssignment")
int countData = 0;
listdata = new ArrayList<>();
JSONArray listHeader = new JSONArray(Arrays.asList(fields));
listdata.add(listHeader.toString());
Record record = theFile.readNext(); //Note 7
while (record != null) {
JSONArray dataArray = new JSONArray();
countData = record.getFields().length;
for (int i = 0; i < countData; i++) {
dataArray.put(record.getField(i).toString());
}
listdata.add(dataArray.toString());
count++;
record = theFile.readNext();
}
//System.out.println();
//JSONArray mJSONArray = new JSONArray(listdata);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
pw.write(listdata.toString());
pw.flush();
pw.close();
theFile.close(); //Note 8
as400System.disconnectService(AS400.RECORDACCESS); //Note 9
} catch (AS400Exception | AS400SecurityException | PropertyVetoException | IOException | InterruptedException e) {
System.out.println("Error occurred attempting to display the file.");
try {
theFile.close();
} catch (AS400Exception | AS400SecurityException | IOException | InterruptedException x) {
}
as400System.disconnectService(AS400.RECORDACCESS);
}
}
} catch (AS400SecurityException | ErrorCompletingRequestException | ObjectDoesNotExistException | PropertyVetoException | IOException | InterruptedException e) {
System.out.println("Program " + program.getProgram() + " issued an exception!");
}
// Done with the server.
as400System.disconnectAllServices();
//Print the output from the RPGLE called program
System.out.println("Message is: " + message);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equals("export")) {
exportToExcel(request, response);
request.getRequestDispatcher("/CallLocalProgram?action=getData").forward(request, response);
} else {
doGet(request, response);
}
}
public void exportToExcel(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=RFP320R.xlsx");
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("RFP320R");
int rowNo = 0;
int cellnum = 0;
for (String rowData : listdata) {
String[] arrRowData = rowData.replace("\"", "")
.replace("[", "").replace("]", "").split(",");
Row row = sheet.createRow(rowNo++);
cellnum = 0;
for (String rData : arrRowData) {
Cell cell = row.createCell(cellnum++);
cell.setCellValue(rData);
}
}
wb.write(response.getOutputStream());
wb.close();
}
}
Here is the stacktrace
Warning: StandardWrapperValve[CallLocalProgramController]: Servlet.service() for servlet CallLocalProgramController threw exception java.lang.IllegalStateException: Cannot forward after response has been committed at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:448) at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378) at com.java400.CallLocalProgramController.doPost(CallLocalProgramController.java:178)