简体   繁体   中英

Return Response (HttpServletResponse) to HttpServletRequest

I always end up with a ClassCastException when trying to cast javax.ws.rs.core.Response into a javax.servlet.http.HttpServletResponse

My Servlet looks like this:

@GET
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Response myResponse = processRequest(request);
        //myResponse -> response
}

The servlet is hosted on aa jetty:

context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.addServlet(new ServletHolder(servlet),"/*");

I want to return the content of myResponse (basically some json body if it matters) if this is possible.

Can someone help?

I don't think you can send the response from servlet to upstream in Response(Custom) format.

You need to set the value in HttpServletrequest and HttpServletResponse while sending the response to upstream.

Below is my suggestion for sending the data in json format in servlet.

Put your JSON data as a string in a String object. Set this object in request.setAttribute(); and use the name of the attribute to fetch the value in your ajax function. use JSON.parse() to convert the string to JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM