Sie HttpServletResponseWrapper verwenden müssen einzuführen und seine getOutputStream Methode außer Kraft setzen.
CustomHttpServletResponseWrapper
und CustomServletOutputStream
sind Implementierung für diese.
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class CustomHttpServletResponseWrapper extends HttpServletResponseWrapper {
private ServletOutputStream outputStream;
private PrintWriter writer;
private CustomServletOutputStream os;
public CustomHttpServletResponseWrapper(HttpServletResponse response) throws IOException {
super(response);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
if (writer != null) {
throw new IllegalStateException("getWriter() has already been called on this response.");
}
if (outputStream == null) {
outputStream = getResponse().getOutputStream();
os = new CustomServletOutputStream(outputStream);
}
return os;
}
@Override
public PrintWriter getWriter() throws IOException {
if (outputStream != null) {
throw new IllegalStateException("getOutputStream() has already been called on this response.");
}
if (writer == null) {
os = new CustomServletOutputStream(getResponse().getOutputStream());
writer = new PrintWriter(new OutputStreamWriter(os, getResponse().getCharacterEncoding()), true);
}
return writer;
}
@Override
public void flushBuffer() throws IOException {
if (writer != null) {
writer.flush();
} else if (outputStream != null) {
os.flush();
}
}
public byte[] getContent() {
if (os != null) {
return os.getContent();
} else {
return new byte[0];
}
}
}
customServletOutputStream.java
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
public class CustomServletOutputStream extends ServletOutputStream {
private OutputStream outputStream;
private ByteArrayOutputStream content;
public CustomServletOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
this.content = new ByteArrayOutputStream(1024);
}
@Override
public void write(int b) throws IOException {
outputStream.write(b);
content.write(b);
}
public byte[] getContent() {
return content.toByteArray();
}
@Override
public boolean isReady() {
// TODO Auto-generated method stub
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
// TODO Auto-generated method stub
}
}
Ihre Hauptklasse: Verwendung RequestDespetcher.include()
Anfrage s2 zu umleiten und CustomHttpServletResponseWrapper
passieren.
@ResponseBody
@RequestMapping("/s1")
public String s1(HttpServletRequest req, HttpServletResponse res) throws Exception{
RequestDispatcher rd = req.getRequestDispatcher("/oauth2/s2");
CustomHttpServletResponseWrapper wrappedResponse = new CustomHttpServletResponseWrapper(res);
rd.include(req, wrappedResponse);
wrappedResponse.flushBuffer();
byte[] result = wrappedResponse.getContent();
System.out.println("result of servlet 2 "+new String(result));
// here you got the result of servlet 2. manipulate it as you want.
return new String(result);
}
@ResponseBody
@RequestMapping("/s2")
public String s2(HttpServletRequest req, HttpServletResponse res){
return "hello from s2";
}
Was ist mit 'response.sendRedirect();'. – Kartic
Ja, Sie können entweder erreichen, indem Anfrage Dispatcher nach vorne mit oder Include Methoden (erhältlich mit 'request' Objekt) oder senden Redirect-Methode (erhältlich mit' response' Objekt). 'ServletContext' ist auch eine der Möglichkeiten,' Servlet'-Collaboration zu erreichen, da es die Informationen aller Servlets bereitstellt. – Mihir