2014-10-09 54 views
1

我陷入了基本的JSP操作。我想要一條新線路,所以我在最後添加了\ n,但它引發了一個例外。如果我刪除\ n一切正常JSP:在response.setHeader中傳遞特殊字符?

異常

java.lang.IllegalArgumentException: Invalid LF not followed by whitespace 

類文件

StringBuilder junitLog = new StringBuilder(); 
junitLog.append("The class that is being executed : " + description.getClassName() +"\n"); 
junitLog.append("Number of testcases to execute : " + description.testCount()+"\n"); 

    /** 
    * @return the junitLog 
    */ 
    public StringBuilder getJunitLog() { 
     return junitLog; 
    } 

    /** 
    * @param junitLog the junitLog to set 
    */ 
    public void setJunitLog(StringBuilder junitLog) { 
     this.junitLog = junitLog; 
    } 

JSP: -

response.setHeader("finalJUNITReport",""+ junitListener.getJunitLog()); 

回答

0

我howeva在我的要求中成功。 :) 由Serge Ballesta明確指出。

如果您在標頭值中添加了新行,任何隨後的行都將被視爲新標頭。

下面是我根據我的要求應用的邏輯。

下面是我的代碼更改:

junitLog.append("The class that is being executed : " + description.getClassName() +"?"); 
junitLog.append("Number of testcases to execute : " + description.testCount()+"?"); 

我在字符串末尾添加一個問號。字符串變成如下所述。

The class that is being executed : testCreateHaulier? Number of testcases to execute : 39?  

我使用響應從JSP中傳遞字符串。代碼的setHeader如下:

response.setHeader("finalJUNITReport",""+ junitListener.getJunitLog()); 

,並在我的Java類文件我做了這樣的事情:

junitReportString=yc.getHeaderField("finalJUNITReport").toString(); 
System.out.println(junitReportString); 
junitReportString=junitReportString.replaceAll("\\?", "\n"); 
System.out.println("Report Details  ==============>"+junitReportString); 

使用的replaceAll我代替所有問號新線和我的要求做。
希望它也能幫助別人。:)

1

嘗試Base64對它們進行編碼,然後再設置到標頭中,Base64對它們進行解碼並將其讀回。

1

只要你以OO的方式思考它,你可能會想知道爲什麼你不能在你的標題中添加新行。

但是一旦您認爲它將使用HTTP協議傳輸,就會很明顯:​​HTTP消息(請求或響應)不是別的順序系列字節。對於HTTP,標題部分是第一位的,它由線的那樣:

HEADER_NAME: header value 

如果你把一個新的行頭價值,任何將遵循將被視爲一個新的頭。如果您連續放置2個新行,則表示標題部分的結尾。

您所能做的只是使用"\n ",因爲如果應該是延續線,則以空格開頭的行。

這是錯誤消息後面沒有空格LF無效的原因,並希望它在那裏,因爲你必須發送一個不正確的標頭部分,這可能是難以檢測...