Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7537341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:49:36+00:00 2026-05-30T06:49:36+00:00

I have a java application that opens a browser and browses to a remote

  • 0

I have a java application that opens a browser and browses to a remote servlet that performs a few redirects. Additionally, the java application instantiates a Jetty Server on an open port and waits for the servlet to redirect the browser to it. I’ve verified that the URL that the servlet is instructing the browser to redirect to is correct (i.e. something like http://localhost:54321/callback), but I’m never hitting a breakpoint in the request handler of the concrete AbstractHandler I implemented to receive the redirect. I am running in a windows environment, but I’m not sure if that’s what’s causing my woes…

Below is the LocalServerReceiver implementation I’m using:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Request;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.AbstractHandler;

public class LocalServerReceiver {

    class CallbackHandler extends AbstractHandler {

        @Override
        public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {

            if (!CALLBACK_PATH.equals(target)) {
                return;
            }

            writeLandingHtml(response);
            response.flushBuffer();
            ((Request) request).setHandled(true);
            String error = request.getParameter("error");
            if (error != null) {
                // TODO Do something.
            }
            synchronized (LocalServerReceiver.this) {
                LocalServerReceiver.this.notify();
            }
        }

        private void writeLandingHtml(HttpServletResponse response) throws IOException {
            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType("text/html");

            PrintWriter doc = response.getWriter();
            doc.println("<html>");
            doc.println("<body>");
            doc.println("<script type='text/javascript'>");
            doc.println("window.setTimeout(function() {");
            doc.println("    window.open('', '_self', ''); window.close(); }, 1000);");
            doc.println("if (window.opener) { window.opener.checkToken(); }");
            doc.println("</script>");
            doc.println("</body>");
            doc.println("</html>");
        }
    }

    private static final String CALLBACK_PATH = "/callback";

    private Server _server;

    public String getRedirectUrl() throws Exception {
        int port = getUnusedPort();
        _server = new Server(port);
        for (Connector c : _server.getConnectors()) {
            c.setHost("localhost");
        }
        _server.addHandler(new CallbackHandler());
        _server.start();
        return "http://localhost:" + port + CALLBACK_PATH;
    }

    public void waitForResponse() {
        try {
            wait();
        } catch (InterruptedException e) {
            // Should not happen
        }
    }

    public void stop() throws Exception {
        if (_server != null) {
            _server.stop();
            _server = null;
        }
    }

    private static int getUnusedPort() throws IOException {
        Socket s = new Socket();
        s.bind(null);
        try {
            return s.getLocalPort();
        } finally {
            s.close();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:49:38+00:00Added an answer on May 30, 2026 at 6:49 am

    After digging a little deeper, I realized I needed to make the current thread the owner of the LocalServerReceiver object’s monitor before calling java.lang.Object.wait(), like so

    public void waitForResponse() {
        try {
            synchronized (LocalServerReceiver.this) {
                LocalServerReceiver.this.wait();
            }
        } catch (InterruptedException e) {
            // Should not happen
        }
    }
    

    Once I did this, Jetty started to work as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java application that is connected to a view on a remote
I have a few JAVA application that I monitor using JMX. I would like
I have a very short Java application that just opens a connection to a
Suppose I have a Java application that opens a database connection. Normally I would
I have a Java application that launches another java application. The launcher has a
I have a Java application that monitors a folder for incoming XML files. When
I have a Java application that's very String-heavy - it takes a feed of
I have a java application that has Web Services published using Axis. With the
We have a Java application that needs to be brought to the foreground when
I have a java application that runs out of memory, but I have no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.