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 8179285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:58:02+00:00 2026-06-06T23:58:02+00:00

I’m trying to embed Jetty in a Processing Sketch. So far I made it

  • 0

I’m trying to embed Jetty in a Processing Sketch. So far I made it working to serve static files (a html directory in the Sketch folder).

I want to react to one POST with a user input from one of the static pages.

As I have no knowledge on Jetty and coming from a PHP & Ruby (RoR) web programing background I am very confused with the way things go in Jetty.

I simply want something similar to routes where everything except e.g.

"localhost:8080/post?string=whatever"

is a static file.

The post?string=whatever should maybe trigger a function (in Processing) where the submitted String is handled.

I have been reading the Jetty docs a lot but couldn’t figure out so far how to do it.

Thank you very much for any help!

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

String poststr;

void setup() {

  Server server = new Server();
  SelectChannelConnector connector = new SelectChannelConnector();
  connector.setPort(8080);
  server.addConnector(connector);

  ResourceHandler resource_handler = new ResourceHandler();
  resource_handler.setDirectoriesListed(true);
  resource_handler.setWelcomeFiles(new String[] { 
    "index.html"
  }
  );

  resource_handler.setResourceBase(sketchPath("")+"pftf");

  HandlerList handlers = new HandlerList();
  handlers.setHandlers(new Handler[] { 
    resource_handler, new DefaultHandler()
  }
  );
  server.setHandler(handlers);




  try {
    server.start();
    server.join();
  } 
  catch(Exception e) {
  };


}
  • 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-06-06T23:58:03+00:00Added an answer on June 6, 2026 at 11:58 pm

    Yes, Jetty can be very confusing in the beginning, especially when you only want to do a couple of simple things (not necessarily full-blown web applications).

    The key to making this work is to use a ContextHandler for each of your other handlers (e.g. ResourceHandler). You can tell the ContextHandler which context (i.e. URL) it should respond to. After making a ContextHandler for the ResourceHandler and your custom Handler (e.g. PostHandler) you have to put both in a ContextHandlerCollection (uff…), so your Server knows what contexts exist.

    Jetty

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.ServletException;
    import java.io.IOException;
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.server.Request;
    import org.eclipse.jetty.server.handler.AbstractHandler;
    
    
    void setup() {
        /* Configure the http server */
        Server server = new Server();
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(8080);
        server.addConnector(connector);
    
        /* Resources */
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(true);
        resourceHandler.setWelcomeFiles(new String[] { 
            "index.html"
        }
        );
    
        resourceHandler.setResourceBase(sketchPath("")+"pftf");
        ContextHandler resourceContext = new ContextHandler();
        resourceContext.setContextPath("/");
        resourceContext.setHandler(resourceHandler);
    
    
        /* Post API */
        PostHandler postHandler = new PostHandler();
        ContextHandler postContext = new ContextHandler();
        postContext.setContextPath("/post");
        postContext.setHandler(postHandler);
    
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(new Handler[] { 
            resourceContext, postContext
        }
        );
    
        server.setHandler(contexts);
    
    
        /* Start the server (finally)  */
    
        try {
            server.start();
            server.join();
        } 
        catch(Exception e) {
            println("Could not start http server. Reason: " + e.toString());
        };
    }
    
    
    void printCard(String mtext) {
        println("Printing card with text: " + mtext);
    }
    

    Your PostHandler could look something like this:

    public class PostHandler extends AbstractHandler
    {
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.setContentType("text/html;charset=utf-8");
    
            String stringParameter = request.getParameter("string");
    
            /* Check if the string parameter is there and not empty */
            if (stringParameter != null && !stringParameter.trim().equals("")) {
    
                response.setStatus(HttpServletResponse.SC_OK);
                baseRequest.setHandled(true);
                response.getWriter().println("<h1>You sent me: " + stringParameter + "</h1>");
    
                println("Received a string via /post: " + stringParameter);
    
                printCard(stringParameter);
            } 
            else {
                // Parameter is missing
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST );
                baseRequest.setHandled(true);
                response.getWriter().println("<h1>Error: Missing string parameter</h1>");
    
                println("Missing string via /post.");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.