Need help creating custom HttpServletResponse

Suddenly stuck on generating a custom servlet response. I want to replace the servlet response with a predefined one:

public class MyCustomResponse extends HttpServletResponseWrapper {
  private String customOutput;
  public MyCustomResponse(String customOutput, HttpServletResponse response) {
    super(response);
    // PrintWriter and Outputstream should stream this variable as output
    this.customOutput = customOutput;
  }

  //
  // Below I need to override something
  //
}

and filter code are reset as follows:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  //
  //
  MyCustomResponse customResponse = new MyCustomResponse("Hello world!", (HttpServletResponse) response);
  chain.doFilter(request, customResponse);
}

Shame on me, but I'm really fixated on this simple task :(

Any help would be appreciated.

UPDATE:

All I want to do is implement a custom response wrapper that, as soon as it fits into the filter chain, will always respond with some predefined text. I know how to write user data from the doFilter () method, but I want to be MyCustomResponseresponsible for this - just create an instance and add a chain. Any reasonable answers "You cannot do this because ..." are also welcome.

+3
4

:

" , getWriter getOutputStream"

getWriter() getOutputStream(), .

//---

    private PrintWriter printWriter = null;
    private ServletOutputStream outputStream = null;

    public PrintWriter getWriter( ) throws IOException {

        if (this.outputStream != null) {
            throw new IllegalStateException(
                    "Cannot call getWriter( ) after getOutputStream( )");
        }

        if (this.printWriter == null) {
            // initialize printWriter
        }
        return this.printWriter;
    }

    public ServletOutputStream getOutputStream( ) throws IOException {

        if (this.printWriter != null) {
            throw new IllegalStateException(
                "Cannot call getOutputStream( ) after getWriter( )");
        }

        if (this.outputStream == null) {
            // initialize outputStream
        }
        return this.outputStream;
    }

//---
+2

,

  • , . , ? ? ?
  • ? "" .
  • , . , . . . / . , (# 2)
+1

, Java, HTTP.

HTTP-, , ( ), ( ) HttpServletResponse ( , .., HttpServletResponse), , response.getWriter(). Print ( "Hello worlds!" ).

, , , .

, , :

private ServletOutputStream fakeOutputStream = 
    new ServletOutputStream() {

        @Override
        public void write(int b) throws IOException {
            // do nothing. Everything written to this stream is ignored
        }
    }

private PrintWriter fakeWriter = new PrintWriter(fakeOutputStream);

public MyCustomResponse(String customOutput, HttpServletResponse response) {
    super(response);
    response.getWriter().print(customOutput);
}

@Override
public ServletOutputStream getOutputStream() {
    return fakeOutputStream;
}

@Override
public PrintWriter getWriter() {
    return fakeWriter;
}
+1

, , , :

, web.xml, :

  • Extend javax.servlet.GenericServletand override the method service(ServletRequest, ServletResponse). Then you create the Template method template to create service(ServletRequest, ServletResponseWrapper). OR
  • Extend javax.servlet.HttpServletand override the method service(HttpServletRequest, HttpServletResponse). Use the template method template to create service(HttpServletRequest, HttpServletResponseWrapper). This will require that you have not used the methods doGet, doPost, doPut, doTracealready provided HttpServlet, but instead, create your own, which uses your wrapper.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1785896/


All Articles