我正在努力应对以下情况:
在我们当前运行在Tomcat 7.0.64上的Web应用程序中,我们设法通过Java在自己的类CharArrayWriterResponse implementing HttpServletResponseWrapper
的帮助下包含一个JSP页面。
这样做的原因是我们将生成的HTML包装成AJAX响应所需的JSON。
依赖关系:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
代码示例:
// somewhere in servlet doPost()/doGet()
try (PrintWriter out = response.getWriter()) {
out.println(getJspAsJson(request, response));
}
private static String getJspAsJson(HttpServletRequest request, HttpServletResponse response) {
String html = getHtmlByJSP(request, response, "WEB-INF/path/to/existing.jsp");
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
return "{\"results\":" + gson.toJson(html) + "}";
}
public static String getHtmlByJSP(HttpServletRequest request, HttpServletResponse response, String jsp) {
CharArrayWriterResponse customResponse = new CharArrayWriterResponse(response);
request.getRequestDispatcher(jsp).include(request, customResponse);
return customResponse.getOutput();
}
public class CharArrayWriterResponse extends HttpServletResponseWrapper {
private final CharArrayWriter charArray = new CharArrayWriter();
public CharArrayWriterResponse(HttpServletResponse response) {
super(response);
}
@Override
public PrintWriter getWriter() throws IOException {
// this is called ONLY in tomcat
return new PrintWriter(charArray);
}
public String getOutput() {
return charArray.toString();
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
// this is called ONLY in WebLogic
return null; // don't know how to handle it
}
}
提示:我没有考虑上面代码示例中的异常处理。
我必须将此应用程序迁移到WebLogic(12.2.1),但此解决方案不再有效。
到目前为止我发现了什么:
在调用request.getRequestDispatcher(jsp).include()
之后的getWriter()
CharArrayWriterResponse
我的getWriter()
类的getWriter()
之后,在Tomcat中调用。
在WebLogic中getOutputStream()
不再被调用,这就是它不再起作用的原因。
经过一些调试后,我发现在WebLogic而不是getWriter()
中,如果我覆盖它,则只调用getOutputStream()
。
在Weblogic上不会调用include()
一次,因此必须在Tomcat和WebLogic的底层实现方面存在差异。
问题在于getWriter()
我认为不可能在单独的流或其他内容中获取{{1}}调用的响应,并将其转换为String以用于构建包含最终JSON的最终JSON HTML。
是否有人已经解决了这个问题,并且可以提供一个有效的解决方案,以便以编程方式将JSP与WebLogic结合使用?
有没有人知道另一种解决方案来实现我的目标?
感谢您的建议。
参见工作示例here
我发现Tomcat和新的Weblogic解决方案之间存在差异: 对于后者,不可能直接包含JSPF,而Tomcat {{1}}就是这样。
解决方案是将JSPF包装在JSP文件中。
答案 0 :(得分:2)
我这样做了:
@Override
public ServletOutputStream getOutputStream() throws IOException {
// this is called ONLY in WebLogic
// created a custom outputstream that wraps your charArray
return new CustomOutputStream(this.charArray);
}
// custom outputstream to wrap charArray writer
class CustomOutputStream extends ServletOutputStream {
private WriterOutputStream out;
public CustomOutputStream(CharArrayWriter writer) {
// WriterOutputStream has a constructor without charset but it's deprecated, so change the UTF-8 charset to the one you use, if needed
this.out = new WriterOutputStream(writer, "UTF-8");
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
@Override
public void write(int b) throws IOException {
this.out.write(b);
this.out.flush(); // it doesn't work without flushing
}
}
我使用了apache commons-io中的WriterOutputStream
,所以我必须包含在我的pom.xml中:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
我不知道你的jsp文件中有什么,但我用一个简单的测试,我相信它有效。 我的jsp文件:
<b>Hello world</b>
<p>testing</p>
<ul>test
<li>item</li>
<li>item2</li>
</ul>
输出(在浏览器中访问servlet时):
{"results":"<b>Hello world</b>\n\n<p>testing</p>\n\n<ul>test\n<li>item</li>\n<li>item2</li>\n</ul>"}
答案 1 :(得分:2)
以下是我更新且工作的示例如何在实现getOutputStream()
时调用getWriter()
而不是HttpServletResponseWrapper
时以编程方式包含JSP文件:
public class MyServletOutputStream extends ServletOutputStream {
private final BufferedOutputStream bufferedOut;
public MyServletOutputStream(CharArrayWriter charArray) {
this.bufferedOut = new BufferedOutputStream(new WriterOutputStream(charArray, "UTF-8"), 16384);
}
@Override
public void write(int b) throws IOException {
this.bufferedOut.write(b);
}
/**
* This is needed to get correct full content without anything missing
*/
@Override
public void flush() throws IOException {
if (this.bufferedOut != null) {
this.bufferedOut.flush();
}
super.flush();
}
@Override
public void close() throws IOException {
this.bufferedOut.close();
super.close();
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
}
public class CharArrayWriterResponse extends HttpServletResponseWrapper {
private final CharArrayWriter charArray = new CharArrayWriter();
private ServletOutputStream servletOutputStream;
public CharArrayWriterResponse(HttpServletResponse response) {
super(response);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
if (servletOutputStream == null) {
servletOutputStream = new MyServletOutputStream(this.charArray);
}
return servletOutputStream;
}
public String getOutputAndClose() {
if (this.servletOutputStream != null) {
try {
// flush() is important to get complete content and not last "buffered" part missing
this.servletOutputStream.flush()
return this.charArray.toString();
} finally {
this.servletOutputStream.close()
}
}
throw new IllegalStateException("Empty (null) servletOutputStream not allowed");
}
// not necessary to override getWriter() if getOutputStream() is used by the "application server".
}
// ...somewhere in servlet process chain e.g. doGet()/doPost()
// request/response The original servlet request/response object e.g. from doGet/doPost(HttpServletRequest request, HttpServletResponse response)
CharArrayWriterResponse customResponse = new CharArrayWriterResponse(response);
request.getRequestDispatcher("/WEB-INF/path/to/existing.jsp").include(request, customResponse);
String jspOutput = customResponse.getOutputAndClose();
// do some processing with jspOut e.g. wrap inside JSON
// customResponse.getOutputStream() is already closed by calling getOutputAndClose()
答案 2 :(得分:2)
由于我无法弄清楚如何在评论中添加多行代码,我只是把它放在这里。
我可以通过覆盖flush()
类中的MyServletOutputStream
方法修复issue:
// inside MyServletOutputStream class
@Override
public void flush() throws IOException {
if (this.bufferedOut != null) {
this.bufferedOut.flush();
}
super.flush();
}