Débora Débora - 1 year ago 221
Java Question

Jetty ProxyServlet does not redirect the request

I want to redirect the request to another server which handles the request and does business logic. I tried with ProxyServlet but it does not work.

Here is my implementation of main server which gets all requests:runs on localhost:8081

Server server = new Server(8081);

// 2. Creaing the WebAppContext for the created content
WebAppContext ctx = new WebAppContext();

ServletHolder jerseyServlet = ctx.addServlet(ProxyServlet.class,"/*");

jerseyServlet.setInitParameter("maxThreads", "1");
jerseyServlet.setInitParameter("proxyTo", "localhost:8080");


org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");

I want to forward this request a server running on port 8080. My target service url path is: localhost:8080/jetty-jsp-example/sv/endpoint .

I have imported jetty proxy as following


Moreover, I read that our custom proxy servlet should be required to extend the proxyservlet class and override the rewriteURL() method.But when I tried that it gave me compile error.

Anyone let me how can I solve this please. I use jetty 9.3

Answer Source

Heh, I was wondering how to get this working the other day too. I had to use the subtype BalancerServlet with the following init parameters:

// give your servlet a name
// parameter is 'balancerMember.NAME.proxyTo'
jerseyServlet.setInitParameter("balancerMember.proxy.proxyTo", "http://yourdownstreamserver.com/");

Docs around configuration are a bit sparse; I ended up having to look at the source code. Also note that Jetty proxy doesn't support proxying to downstream services over SSL.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download