WebLogic 12c中以编程方式包括JSP的RequestDispatcher的问题 [英] Issue with RequestDispatcher including JSP programmatically in Weblogic 12c

查看:100
本文介绍了WebLogic 12c中以编程方式包括JSP的RequestDispatcher的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力应对以下情况:

I'm struggling with the following situation:

在当前运行于Tomcat 7.0.64上的Web应用程序中,我们设法通过Java借助自己的类CharArrayWriterResponse implementing HttpServletResponseWrapper来包含JSP页面.

In our current web application running on Tomcat 7.0.64, we manage to include a JSP page via Java with the help of an own class CharArrayWriterResponse implementing HttpServletResponseWrapper.

这样做的原因是我们将结果HTML封装为AJAX响应所需的JSON.

The reason for doing so is that we wrap the resulting HTML into JSON needed for an AJAX Response.

依赖项:

<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
    }
}

提示:在上述代码示例中,我没有考虑异常处理.

Hint: I didn't consider exception handling in above code samples.

我必须将此应用程序迁移到WebLogic(12.2.1),但此解决方案不再起作用.

I have to migrate this application to WebLogic (12.2.1) but this solution is not working anymore.

到目前为止我发现了什么

What I found out so far:

在Tomcat中,调用了CharArrayWriterResponse类的getWriter()上例中的request.getRequestDispatcher(jsp).include()之后.

In Tomcat after the call to request.getRequestDispatcher(jsp).include() of the example above getWriter() of my CharArrayWriterResponse class is called.

在WebLogic中,getWriter()不再被调用,这就是它不再起作用的原因.

In WebLogic getWriter() is not called anymore and that's the reason why it doesn't work anymore.

经过一些调试后,我发现在WebLogic中,如果我重写了getOutputStream(),则仅调用getOutputStream()而不是getWriter(). getWriter()在Weblogic上不会被调用一次,因此Tomcat和WebLogic的基础实现必须有所不同.

After some debugging, I found out that in WebLogic instead of getWriter() only getOutputStream() is called if I override it. getWriter() is not called once on Weblogic so there have to be differences in the underlying implementation of Tomcat and WebLogic.

问题在于,使用getOutputStream()时,我看不到在单独的流或其他内容中获得include()调用的响应并将其转换为String以用于构建包含HTML的最终JSON的可能性.

Problem is that with getOutputStream() I see no possibility to get the response of the include() call in a separate stream or something else and to convert it to String for usage to build the final JSON containing the HTML.

有人已经解决了这个问题,并且可以提供一种可行的解决方案,以编程方式将JSP与WebLogic结合使用吗?

Has someone solved this problem already and can provide a working solution for including a JSP in a programmatic way in combination with WebLogic?

有人知道实现我目标的另一种方法吗?

Does anyone know another solution to achieve my goal?

感谢您的建议.

请参见工作示例此处

我发现Tomcat和新的Weblogic解决方案之间的区别: 对于后者,不可能再将Tomcat和getWriter()直接包含在JSPF中.

A difference I found out between Tomcat and new Weblogic solution: With latter one it's not possible to include JSPF's directly anymore wheras with Tomcat getWriter() it is.

解决方案将JSPF包装在JSP文件中.

Solution is wrapping the JSPF inside a JSP file.

推荐答案

我做到了:

@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中:

I used WriterOutputStream from apache commons-io, so I had to include in my pom.xml:

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.5</version>
</dependency>

我不知道您的jsp文件中包含什么,但是我已经用一个简单的文件进行了测试,并且我相信它可以工作. 我的jsp文件:

I don't know what's in your jsp file, but I've tested with a simple one and I believe it worked. My jsp file:

<b>Hello world</b>

<p>testing</p>

<ul>test
<li>item</li>
<li>item2</li>
</ul>

输出(在浏览器中访问servlet时):

Output (when accessing the servlet in a browser):

{"results":"<b>Hello world</b>\n\n<p>testing</p>\n\n<ul>test\n<li>item</li>\n<li>item2</li>\n</ul>"}

这篇关于WebLogic 12c中以编程方式包括JSP的RequestDispatcher的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆