DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Easily Update and Reload SSL for a Server and an HTTP Client

Trending

  • Code Reviews: Building an AI-Powered GitHub Integration
  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  • The End of “Good Enough Agile”
  • Agile’s Quarter-Century Crisis

Configuring a Jetty Servlet Proxy

Want to deploy some apps? See how to properly configure the Jetty servlet proxy.

By 
Mohammad Nadeem user avatar
Mohammad Nadeem
·
Sep. 30, 15 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
19.4K Views

Join the DZone community and get the full member experience.

Join For Free

Sample application can be downloaded from here

Lets say you deploy app1, app2, someotherapp to devhost:3000 and testhost:3000 then those apps can be accessed as follows

DEV instances
http://devhost:3000/app1
http://devhost:3000/app2
http://devhost:3000/someotherapp

TEST instances
http://testhost:3000/app1
http://testhost:3000/app2
http://testhost:3000/someotherapp

Many a times one application depends upon other applications with in the same server and hence, your application may contain code snippets as follows:

<link title="Stylesheet" href="/someotherapp/style.css" rel="stylesheet" media="screen" type="text/css" />

Further, those can be directly accessed as well

http://devhost:3000/someotherapp/style.css
http://testhost:3000/someotherapp/style.css

Further Jetty is the best choice for local development either embedded or using jetty-maven-plugin.

In this case it does not make sense to replicate the entire environment locally, instead those urls can be proxied in local environment such that /someotherapp/style.css would be proxied to http://devhost:3000/someotherapp/style.css

Here is how you can achieve it.

1) Map ProxyServlet.Transparent to /someotherapp/*
2) Set the Servlet init parameters as follows
ProxyTo ==> http://devhost:3000/someotherapp
Prefix ==> /someotherapp

The Above Two Steps Can be Done in Two Ways

Configuring Using Java

package com.nadeem.app;

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;

import org.apache.wicket.util.time.Duration;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.ssl.SslSocketConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.webapp.WebAppContext;

public class Start {

    public static void main(String[] args) throws Exception {
        int timeout = (int) Duration.ONE_HOUR.getMilliseconds();

        SocketConnector connector = newConnector(timeout);
        Server server = newServer(timeout, connector);

        addSecureSupport(timeout, connector, server);

        addJmxSupport(server);

        try {
            System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
            server.start();
            System.in.read();
            System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
            server.stop();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    private static void addJmxSupport(Server server) throws Exception
    {
        // START JMX SERVER
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer);
        server.getContainer().addEventListener(mBeanContainer);
        mBeanContainer.start();
    }

    private static void addSecureSupport(int timeout, SocketConnector connector, Server server)
    {
        Resource keystore = Resource.newClassPathResource("/keystore");
        if (keystore != null && keystore.exists()) {
            // if a keystore for a SSL certificate is available, start a SSL
            // connector on port 8443.
            // By default, the quickstart comes with a Apache Wicket Quickstart
            // Certificate that expires about half way september 2021. Do not
            // use this certificate anywhere important as the passwords are
            // available in the source.

            connector.setConfidentialPort(8443);

            SslContextFactory factory = new SslContextFactory();
            factory.setKeyStoreResource(keystore);
            factory.setKeyStorePassword("wicket");
            factory.setTrustStoreResource(keystore);
            factory.setKeyManagerPassword("wicket");
            SslSocketConnector sslConnector = new SslSocketConnector(factory);
            sslConnector.setMaxIdleTime(timeout);
            sslConnector.setPort(8443);
            sslConnector.setAcceptors(4);
            server.addConnector(sslConnector);

            System.out.println("SSL access to the quickstart has been enabled on port 8443");
            System.out.println("You can access the application using SSL on https://localhost:8443");
            System.out.println();
        }
    }

    private static Server newServer(int timeout, SocketConnector connector) {
        Server server = new Server(); 
        server.setHandler(newHandlers(server));
        server.addConnector(connector);
        return server;
    }

    private static HandlerList newHandlers(Server server) {
        WebAppContext appContext = newWebAppContext(server);

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] {appContext, proxyHandler(), new DefaultHandler()});
        return handlers;
    }

    private static SocketConnector newConnector(int timeout) {
        SocketConnector connector = new SocketConnector();

        // Set some timeout options to make debugging easier.
        connector.setMaxIdleTime(timeout);
        connector.setSoLingerTime(-1);
        connector.setPort(8080);

        return connector;

    }

    private static WebAppContext newWebAppContext(Server server) {
        WebAppContext bb = new WebAppContext();
        bb.setServer(server);
        bb.setContextPath("/webApp");
        bb.setWar("src/main/webapp");
        return bb;
    }

    private static ServletContextHandler proxyHandler() {
        ServletContextHandler contextHandler = new ServletContextHandler();
        contextHandler.setServletHandler(newServletHandler());

        return contextHandler;
    }

    private static ServletHandler newServletHandler() {


        ServletHandler handler = new ServletHandler();

        ServletHolder holder = handler.addServletWithMapping(org.eclipse.jetty.servlets.ProxyServlet.Transparent.class, "/proxy/*");
        holder.setInitParameter("ProxyTo", "http://localhost:8080/webApp");
        holder.setInitParameter("Prefix", "/proxy");        

        return handler;
    }
}

Configuring Using Maven Jetty Plugin

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
    <configuration>
        <webAppConfig>
            <contextPath>/webApp</contextPath>
        </webAppConfig>
        <jettyConfig>src/main/etc/jetty.xml</jettyConfig>
        <connectors>
            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <port>8080</port>
                <maxIdleTime>3600000</maxIdleTime>
            </connector>                      
        </connectors>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>jetty-all-server</artifactId>
            <version>${jetty.version}</version>
        </dependency>
    </dependencies>
</plugin>

src/main/etc/jetty.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure id="server" class="org.eclipse.jetty.server.Server">

<New id="ServletContextHandler" class="org.eclipse.jetty.servlet.ServletContextHandler">
 <Set name="servletHandler">
<New id="servletHandler" class="org.eclipse.jetty.servlet.ServletHandler">
<Call id="proxyHolder" name="addServletWithMapping">
<Arg>org.eclipse.jetty.servlets.ProxyServlet$Transparent</Arg>
<Arg>/proxy/*</Arg>
<Call name="setInitParameter">
<Arg>maxThreads</Arg>
<Arg>
<Property name="jetty.proxy.maxThreads" default="128" />
</Arg>
</Call>
<Call name="setInitParameter">
<Arg>maxConnections</Arg>
<Arg>
<Property name="jetty.proxy.maxConnections" default="256" />
</Arg>
</Call>
<Call name="setInitParameter">
<Arg>idleTimeout</Arg>
<Arg>
<Property name="jetty.proxy.idleTimeout" default="30000" />
</Arg>
</Call>
<Call name="setInitParameter">
<Arg>timeout</Arg>
<Arg>
<Property name="jetty.proxy.timeout" default="60000" />
</Arg>
</Call>
<Call name="setInitParameter">
<Arg>ProxyTo</Arg>
<Arg>http://localhost:8080/wicket</Arg>
</Call>
<Call name="setInitParameter">
<Arg>Prefix</Arg>
<Arg>/proxy</Arg>
</Call>
</Call>

</New>
</Set>
</New>

<Set name="handler">
<New class="org.eclipse.jetty.server.handler.HandlerList">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<Ref id="ServletContextHandler"></Ref>
</Item>
</Array>
</Set>
</New>
</Set>


</Configure>
Jetty (web server)

Published at DZone with permission of Mohammad Nadeem, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Easily Update and Reload SSL for a Server and an HTTP Client

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!