Issue
I always end up with a ClassCastException
when trying to cast javax.ws.rs.core.Response
into ajavax.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 a a 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?
Solution
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.
Answered By - Vaibs
Answer Checked By - Mary Flores (JavaFixing Volunteer)