zoukankan      html  css  js  c++  java
  • 获取sevlet response值

    调用:

    PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
    SimpleWrapper wrapper = new SimpleWrapper(response);
    /* サーブレットから出力される HTML を取得 */
    String outHtml = wrapper.toString();

    System.out.println("=" + outHtml);

    try {
    wrapper.finalize();
    } catch (Throwable e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    实现:

    ------------------------------------------------------------------------

    SimpleWrapper.java

    package com.autocredit.web.filter;

    import java.io.CharArrayWriter;
    import java.io.PrintWriter;

    import javax.servlet.ServletOutputStream;
    import javax.servlet.WriteListener;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpServletResponseWrapper;

    public class SimpleWrapper extends HttpServletResponseWrapper {

    private CharArrayWriter caw = null;

    public SimpleWrapper(HttpServletResponse response) {
    super(response);
    caw = new CharArrayWriter();
    }

    public String toString() {
    /* CharArrayWriter の toString を呼び出します */
    return caw.toString();
    }

    public PrintWriter getWriter() {
    /* CharArrayWriter で PrintWriter を返却します。 */
    return new PrintWriter(caw);
    }

    public ServletOutputStream getOutputStream() {
    /* SampleServletOutputStream を返却します。 */
    return new SampleServletOutputStream(caw);
    }

    class SampleServletOutputStream extends ServletOutputStream {

    private CharArrayWriter buffer = null;

    public SampleServletOutputStream(CharArrayWriter charArrayWriter) {
    super();
    buffer = charArrayWriter;
    }

    public void write(int c) {
    buffer.write(c);
    }

    @Override
    public boolean isReady() {
    // TODO Auto-generated method stub
    return false;
    }

    @Override
    public void setWriteListener(WriteListener writeListener) {
    // TODO Auto-generated method stub

    }
    }
    }

  • 相关阅读:
    来自1068
    耻辱的时间戳(笑哭)
    依然排序
    呵呵
    好吧,第二篇
    来自机房的第一篇博客
    Shader-水流效果
    unity中虚拟摇杆的实现
    (转载)Unity3d中的属性(Attributes)整理
    C#冒泡排序法及优化
  • 原文地址:https://www.cnblogs.com/adolfmc/p/5130416.html
Copyright © 2011-2022 走看看