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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:34:49+00:00 2026-05-31T15:34:49+00:00

Suppose I have the following example of an embedded Java servlet in Jetty libraries

  • 0

Suppose I have the following example of an embedded Java servlet in Jetty libraries – is it possible to do an equivalent using the Tomcat API libraries?

import java.io.IOException;

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

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloHandler extends AbstractHandler
{
    public void handle(String target,Request baseRequest,HttpServletRequest     request,HttpServletResponse response) 
        throws IOException, ServletException
    {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }

    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);
        server.setHandler(new HelloHandler());

        server.start();
        server.join();
    }
}
  • 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-31T15:34:50+00:00Added an answer on May 31, 2026 at 3:34 pm

    A Tomcat7 embedded equivalent would look like this:

    public class TinyTomcat7Embedded {
    
        public static void main(String[] args) {
            Tomcat tomcat = new Tomcat();
            tomcat.setPort(8080);
    
            File baseDir = new File("tomcat");
            tomcat.setBaseDir(baseDir.getAbsolutePath());
    
            File applicationDir = new File(baseDir + "/webapps", "/ROOT");
            if (!applicationDir.exists()) {
                applicationDir.mkdirs();
            }
    
            try {
                Context appContext = tomcat.addWebapp("/", "ROOT");
    
                // A Jetty AbstractHandler is an HttpServlet here:
                Tomcat.addServlet(appContext, "helloWorldServlet", new HttpServlet() {
    
                    @Override
                    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                        response.setContentType("text/html;charset=utf-8");
                        response.setStatus(HttpServletResponse.SC_OK);
                        response.getWriter().println("<h1>Hello World</h1>");
                    }
                });
                appContext.addServletMapping("/helloworld", "helloWorldServlet");
    
                tomcat.start();
                System.out.println("Tomcat server: http://" + tomcat.getHost().getName() + ":" + 8080 + "/");
                tomcat.getServer().await();
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (LifecycleException e) {
                e.printStackTrace();
            }
        }
    }
    

    Add these dependencies to your pom.xml:

        <!-- Eclipse Java Compiler -->
        <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>3.7.1</version>
        </dependency>
    
        <!-- Tomcat Embedded -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>7.0.26</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-logging-juli</artifactId>
            <version>7.0.26</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-logging-log4j</artifactId>
            <version>7.0.26</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>7.0.26</version>
        </dependency>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say suppose I have the following Java code. public class Example { public static
Suppose an example. I have following interface: public interface DataSource<T> { Future<T> fetch(); }
Suppose I have following code: def foo(s): A dummy function foo. For example: >>>
For example suppose I have the following within my HTML <script src=/socket.io/socket.io.js></script> The script
For example suppose I have the following string we went to the (big) zoo
For example suppose I have the following ranges 5031..5032 6248..6249 How do I generate
Suppose I have the following function: ## Just an example f = function() {
For example suppose I have the following join join on p.match_id = o.match_id Could
Let's suppose I have the following regex: -(\d+)- and I want to replace, using
Let's suppose I have a the following simplified example database consisting of three tables:

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.