Débora Débora - 20 days ago 7
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();
ctx.setResourceBase("src/main/webapp");
ctx.setContextPath("/jetty-jsp-example");

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

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

ctx.setAttribute("o:Prg.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",".*/[^/]*jstl.*\\.jar$");

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");
server.setHandler(ctx);
server.start();
server.join();


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

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-proxy</artifactId>
<version>9.3.8.v20160314</version>




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

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
jerseyServlet.setName("proxy");
// 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.

Comments