Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSP:在response.setHeader中传递特殊字符?_Java_Jsp - Fatal编程技术网

Java JSP:在response.setHeader中传递特殊字符?

Java JSP:在response.setHeader中传递特殊字符?,java,jsp,Java,Jsp,我被困在一个基本的JSP操作中。我想要一个新行,所以我在最后添加了\n,但它引发了一个异常。如果我删除\n一切正常 例外情况 java.lang.IllegalArgumentException: Invalid LF not followed by whitespace 类文件 StringBuilder junitLog = new StringBuilder(); junitLog.append("The class that is being executed : " + descri

我被困在一个基本的JSP操作中。我想要一个新行,所以我在最后添加了\n,但它引发了一个异常。如果我删除\n一切正常

例外情况

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());

在设置标题之前,请尝试使用Base64编码,当您想读回标题时,请尝试使用Base64解码。

只要您以面向对象的方式思考,您就会想为什么不能在标题中添加新行

但一旦您认为它将使用HTTP协议传输,就很明显:HTTP消息(请求或响应)只不过是一系列连续的字节。对于HTTP,标头部分位于第一位,由以下行组成:

HEADER_NAME: header value
如果在标题值中添加新行,则后面的任何内容都将被视为新标题。如果你放两个连续的新行,表示页眉部分的结尾

您所能做的就是使用
“\n”
,因为以空格开头的行应该是一个连续行


这就是错误消息Invalid LF后面没有空格的原因,希望它在那里,因为您可能会发送一个不正确的标题部分,而这可能更难检测到…

我是如何在我的要求中成功的。:) 正如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?    
我使用response.setHeader代码从JSP传递字符串,如下所示:

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,我将所有问号替换为新行,我的要求完成。
希望它也能帮助其他人。:)