0

I've a method that uses HttpServletRequest, HttpServletResponse and ServletContext. Here's my code:

public String processarModelo(MyObject ou, Map<String, Object> attrs,
            Map<String, Object> params) throws Exception {


        ServletContext sc = com.opensymphony.webwork.ServletActionContext.getServletContext();
        HttpServletResponse r = com.opensymphony.webwork.ServletActionContext.getResponse();
        MyHttpRequest rw = new MyHttpRequest(com.opensymphony.webwork.ServletActionContext.getRequest());
        rw.clearAttributes();

        for (String s : attrs.keySet()) {
            rw.setAttribute(s, attrs.get(s));
        }

        Map<String, String> p = rw.getParameterMap();
        p.clear();
        for (String s : params.keySet()) {
            p.put(s, (String) params.get(s));
        }

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        Writer w = new OutputStreamWriter(bout);
        SwallowingHttpServletResponse r2 = new SwallowingHttpServletResponse(r, w, "iso-8859-1");

        javax.servlet.RequestDispatcher dispatcher = sc.getRequestDispatcher("/paginas/expediente/processa_modelo.jsp");

        dispatcher.include(rw, r2);
        w.flush();
        String s = bout.toString();


        return s;
    }

It's working when I call it from my browser. This week I've built a webservice that must call the same method. Inside the webservice method I have however no ServletContext, HttpServletResponse and HttpServletRequest at hands.

How can I emulate them to render my JSP and get the generated HTML?

2
  • These are interfaces whose concrete implementations are provided by your servlet container. It seems your problem is that you are in a web service class that does not provide direct access to these objects. You probably should not reference actual implementation classes as that ties you to a specific servlet container.
    – EJK
    Jan 21, 2013 at 17:30
  • 1
    If this were a REST web service, running with a JAX-RS provider, you could use the @Context annotation to have the provider inject instances of request and response objects.
    – EJK
    Jan 21, 2013 at 17:32

2 Answers 2

1

Well, searching around about @Context annotation, i found this one for webservice:

@Resource
private WebServiceContext context;

With this, i can get everything i need with this:

HttpServletRequest request = (HttpServletRequest)context.getMessageContext().get(MessageContext.SERVLET_REQUEST);
        ServletContext servletContext = (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
        HttpServletResponse response = (HttpServletResponse) context.getMessageContext().get(MessageContext.SERVLET_RESPONSE);

Thanks everybody for helping!!

0

Are you sure you want your web service to call this? It will get redirected to some other page....

It looks like a design issue to me. You should extract the actual functionality from here and place it in some common code. Then call it in such a way that the web service request and servlet request will have enough information to call the functionality.

Also one letter variable names are discouraged ;)

If you want to make a call to the web page to store the HTML, you should make an HTTP request in the webservice method itself and store the data. You can use something like HTTPClient (http://hc.apache.org/httpcomponents-client-ga/) to do this.

Trying to build the HTTPRequest yourself isnt a good way to go.

1
  • Well, the real functionality e render a jsp, and get the html result and store it in a database. Before we used it only from the browser. Now we need a new funcionality using webservice. So, for this reason, the ServletContext, HttpServletRequest and HttpServletResponde are requested here. I need them to render my jsp. Before i had it created from browser accessing. Now, accessing it from a webservice, i dont have them anymore... So i need to emulate.
    – Igor
    Jan 21, 2013 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.