Announcement Announcement Module
Collapse
No announcement yet.
How to use JSP results inside a controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use JSP results inside a controller

    Hi,

    I am having problems invoking a JSP from inside a controller and then using the resulting markup of that JSP for another operation inside the same controller. Here are the details and my current approach. Any help would be greatly appreciated.

    I would like to do the following:

    1. FooController is invoked
    2. FooController parses some information from the request, creates some data objects and invokes another JSP. The JSP returns an XML document
    3. FooController uses this XML to create a proprietary message format, invokes some service layer API, and then returns a resulting SUCCESS/FAIL page to the user.
    4. I need to use the ViewResolving framework since I am integrating with OpenCms so I can't simply use the RequestDispatcher to forward to the page.

    I have implemented this by the following but am not getting the results I desire. After invoking the first JSP successfully, the system hangs for 10-20 seconds and does not invoke my final SUCCESS/FAIL JSP.

    Any ideas?

    Code:
    public class GatewayDispatcherServlet extends DispatcherServlet {
    	
    	public GatewayDispatcherServlet() {
    		super();
    		_singleton = this;
    		System.out.println("************************** "+getSingleton());
    	}
    	
    	public void renderMyModel(ModelAndView modelView, final HttpServletRequest request, HttpServletResponse response) throws Exception {
    		//return render(modelView, request, response);
    		render(modelView, request, response);
    	}
    	
    	public static GatewayDispatcherServlet getSingleton() {
    		return _singleton;
    	}
    	
    	private static GatewayDispatcherServlet _singleton;
    }

    Code:
    public class MyHttpServletResponseWrapper extends HttpServletResponseWrapper {
    
    	public MyHttpServletResponseWrapper(HttpServletResponse arg0) {
    		super(arg0);
    		_outputStream = new MyServletOutputStream();
    	}
    
    	public ServletOutputStream getOutputStream() throws IOException {		
    		return _outputStream;
    	}
    	
    	public String getContent() {
    		return _outputStream.toString();
    	}
    
    	private MyServletOutputStream _outputStream;
    }
    Code:
    public class MyServletOutputStream extends ServletOutputStream {
    
    	public MyServletOutputStream() {
    		_outputStream = new ByteArrayOutputStream();
    	}
    	
    	public void write(int b) throws IOException {
    		_outputStream.write(b);
    	}
    	
    	public String toString() {
    		return _outputStream.toString();
    	}
    	
    	private final ByteArrayOutputStream _outputStream;
    }
    Code:
    	public MyController(MobileObjectDAO mobileObjectDAO, MyEngine engine) {
    		_mobileObjectDAO = mobileObjectDAO;
    		_engine = engine;
    		_gatewayDispatcherServlet = GatewayDispatcherServlet.getSingleton();
    	}
    
    	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    
    		...
    		...
    
    		//BUILD A MAP FOR THE VIEW
    		Map model = new HashMap();
    		model.put("amo", amo);
    		model.put("device", device);
    		
    		//INVOKE A JSP TO RENDER THE MODEL
    		_gatewayDispatcherServlet.getClass();
    		MyHttpServletResponseWrapper responseWrapper = new MyHttpServletResponseWrapper(response);
    		_gatewayDispatcherServlet.renderMyModel(new ModelAndView("renderMe", model), request, responseWrapper);
    
    		//Send this rendering to a messaging service
    		String rendering = responseWrapper.getContent();
    		_messageService.sendMessage(rendering);
    
    		//success/failure page
    		return new ModelAndView("resultPage", model);
    	}

  • #2
    found the problem

    I found the problem. I wasn't implementing a writer on my servlet response wrapper.. doh!

    Code:
    public class MyHttpServletResponseWrapper extends HttpServletResponseWrapper {
    
    	public MyHttpServletResponseWrapper(HttpServletResponse arg0) {
    		super(arg0);
    		_outputStream = new MyServletOutputStream();
    		_writer = new PrintWriter(_outputStream);
    	}
    
    	public ServletOutputStream getOutputStream() throws IOException {		
    		return _outputStream;
    	}
    	
    	public PrintWriter getWriter() {
    		return _writer;
    	}
    	
    	public String getContent() {
    		return _outputStream.toString();
    	}
    
    	private MyServletOutputStream _outputStream;
    	private PrintWriter _writer;
    }

    Comment

    Working...
    X