java.lang.illegalstateexception无法转发响应已提交

z8dt9xmd  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(468)

我正在使用下面的jsp
java.lang.IllegalStateException cannot forward. response already committed jsp. 我不明白发生了什么事?为什么会有例外呢?
我试了很多。请帮我解决这个问题。

String contentType = request.getContentType();
    InputStream tmpInStream =null ;
DataInputStream myInput = null;
boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try 
{
    List  fields = upload.parseRequest(request);
    Iterator it = fields.iterator();
    while (it.hasNext()) 
    {
FileItem fileItem = (FileItem)it.next();
boolean isFormField = fileItem.isFormField();
if (isFormField==false) 
{
    fileName=fileItem.getName();
    log.printOut("[Upload] (When form field is false ) FileName: " + fileItem.getName() + " SIZE (BYTES): " + fileItem.getSize() );
    tmpInStream = fileItem.getInputStream();
    myInput =  new DataInputStream(tmpInStream);
    String tmpCSVData = fileItem.getString();
}
else 
{
    log.printOut("[Upload] (When form field is true ) FileName: " + fileItem.getName() + " SIZE (BYTES): " + fileItem.getSize() );
    csv = fileItem.getString();
}   
    }
}
catch (Exception e) 
{
    log.printErr("[Upload] Error while getting file :"+ e);
}

String thisLine; 
XMLParser xmlParser = new XMLParser();
String InputXml="";
String OutputXml="";
String csvType="";

try{

    Properties properties = new Properties();
    properties.load(new FileInputStream(System.getProperty("user.dir")+File.separator+"ips.properties"));
    String Ipsviewname=properties.getProperty("CsvIpsViewName");
    String Tranviewname=properties.getProperty("CsvTransViewName");
    log.printOut("[Upload] TableName (IPS)"+ Ipsviewname );
    log.printOut("[Upload] TableName (Transaction)"+ Tranviewname );
    while ((thisLine = myInput.readLine()) != null)
    {
String strar[] = thisLine.split(",");
log.printOut("[Upload] Line Split length "+ strar.length );
if(strar.length!=11 && strar.length!=5)
{
    RequestDispatcher rd = request.getRequestDispatcher("DownloadDocs.jsp?result=Invalid CSV");
    rd.forward(request, response);
}
if(strar.length==11)
{
    csvType="ips";
}
else
{

    csvType="Transaction";
}
log.writeXML("[Upload] INPUTXML "+ InputXml );
OutputXml = sessionBean.execute(InputXml);
log.writeXML("[Upload] OUTPUTXML "+ OutputXml );
xmlParser.setInputXML(OutputXml);
    }
   request.setAttribute ( "page","upload" );
   request.setAttribute ( "file", fileName );
   RequestDispatcher rd = request.getRequestDispatcher("csvtabl.jsp?csvType="+csvType+"");
   rd.forward(request, response);
}
catch (Exception e) 
{   
    RequestDispatcher rd =request.getRequestDispatcher("DownloadDocs.jsp?result=Invalid CSV");
    rd.forward(request, response);
    log.printErr("[Upload] Error while reading file :"+ e );}
nhhxz33t

nhhxz33t1#

在第一次调用 RequestDispatcher.forward 方法,否则代码将继续执行。在大多数情况下,您希望在每次转发后返回。
在您的例子中,您在while循环中转发,代码继续执行,并在方法末尾再次尝试转发。

if(strar.length!=11 && strar.length!=5) {
    RequestDispatcher rd = request.getRequestDispatcher("DownloadDocs.jsp?result=Invalid CSV");
    rd.forward(request, response);
    return ;
}

(包括其他地方的退货)。

相关问题