Is it possible to forward a Request from a WebServ

2019-07-31 23:04发布

问题:

Problem: I have a WebFilter which forwards valid urls to either a proxy servlet or a servlet which handles a webpage for the admin to monitor recent requests and more.

The admin servlet is suppost to forward ajax requests to a REST service (after login.jsp from the webpage rendered by controlpannel.jsp ) but apparently the rest service has a different context as the WebFilter and WebServlets ?

Question: So is it at all possible to forward from my WebServlet to the rest helper servlet (and its resource classes) ?

More specific Information:

This is how I use forwarding:

ServletContext sc = request.getServletContext();
RequestDispatcher dispatcher = sc.getRequestDispatcher(forwardURI);
dispatcher.forward(request, response);

I tried to forward to this uri:

forwardURI = /REST/proxy_client/newer_than

My rest helper servlet:

@Stateless
@ApplicationPath("/REST")
public class RestService extends Application {
@Override
public Set<Class<?>> getClasses() {
    final Set<Class<?>> restResourceClasses = new HashSet<Class<?>>();
    restResourceClasses.add(ProxyClientResource.class);
    return restResourceClasses;
}

}

And this resource class:

@Path("/proxy_client")
@Stateless
public class ProxyClientResource {
@EJB
private ProxyClientBean proxyClientBean;

@GET
@Produces("application/json")
@Path("/newer_than")
public String getNumberOfEntriesNewerThanTimestamp(@QueryParam("timestamp") Expression<Timestamp> indexTimestamp,
        @QueryParam("numberOfclients") Integer numberOfclients) {
    List<ProxyClient> pageData = proxyClientBean.getElementsNewerThan(numberOfclients, indexTimestamp);
    return convertToJSONstring(pageData);
}

Solution attempt:

I found this question about how to call a rest web service from a servlet, but they use a client and no forwarding.

EDIT:

I had a configuration problem (might still have one), so now when I try to forward to my rest helper servlet (the one extending javax.ws.rs.core.Application) I get this error:

RestServlet is currently unavailable 

(in the web.xml I call the Servlet RestServlet)

when accessing the REST api directly I get:

HTTP Status 500 - Authenticator.invoke() failed

but I can't find out what this means.

Edit2:

I will try repacing the subclass of Applicaton with a config in web.xml subclassing and @ApplicationPath dont seem to work for me. Also when I try to get the rest ServletsContext I get an error that no class has been specified, which is something you do when using the web.xml config.

Edit3:

I'm deploying my application on HCP and with the underlying problem beeing that I cant even access my REST service I found this SAP discussion. When I get my REST service working without forwarding I will report back here.

Edit4: This actually answers the question from Edit3 I had to add jersey 1.19.1 (not 2.x because im using Java EE6 which only supports up to DWP 3.0 not 3.1 as required) to by projects libraries otherwise It would say that I didn't specify a servlet class (but when I tried to add javax.ws.rs.core.Application it would tell me this is no Servlet class even though I have seen this configuration).

回答1:

My real problem was that the javax.ws.rs.core.Application from the Java ee6 container on SAP Hana Cloud Platform did not work for a unkown reason.

The solution was to download and add the jersey-bundle-1.19.1.jar to WEB-INF/lib and the projects libraries.

There is no problem at all to forward a request from a vanilla servlet to the rest service! If it does not work in your case its most likely your setup or some unexpected reason like it was in my case.