Home > Java > javaTutorial > body text

Using Jetty8 for Web server processing in Java API development

WBOY
Release: 2023-06-18 10:57:09
Original
1303 people have browsed it

Use Jetty8 for Web server processing in Java API development

Jetty is an open source Java-based web server. It provides a way to embed a web server in Java applications, which can help developers in their applications. Provides Web services. An important feature of Jetty is its lightweight and fast startup speed, which makes it a top choice among developers.

This article will introduce how to use Jetty8 for Web server processing in Java API development. Jetty8 is an early version of Jetty, but is still widely used and considered stable.

  1. Installing Jetty8

First, you need to download the installation file of Jetty8, which can be downloaded from the Jetty official website. Once the download is complete, unzip the file and place it in your project directory. Then, use Maven or Gradle to add Jetty8 to your project. The configuration of Maven is as follows:

<dependencies>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>8.1.16.v20140903</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>8.1.16.v20140903</version>
    </dependency>
</dependencies>
Copy after login
  1. Writing Servlet

Servlet is a component in Java Web that handles HTTP requests and responses. You need to write a Servlet to process the requests received by Jetty . The following is a sample code that uses Jetty to handle requests:

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>Hello, World!</h1>");
    }
}
Copy after login

In the above code, MyServlet inherits the HttpServlet class and overrides the doGet() method to handle HTTP GET requests. In the doGet() method, set the content of the response to "Hello, World!".

  1. Configuring the Jetty server

Next, you need to configure the Jetty server so that it can start and run the servlet. Create the startJetty() method to start the Jetty server:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class JettyServer {
    private static final int DEFAULT_PORT = 8080;

    public static void startJetty() {
        Server server = new Server(DEFAULT_PORT);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");

        ServletHolder servletHolder = new ServletHolder(new MyServlet());
        context.addServlet(servletHolder, "/*");

        server.setHandler(context);

        try {
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Copy after login

In the above code, the default port number DEFAULT_PORT is first defined as 8080, and the Jetty server is initialized by creating a Server object. ServletContextHandler is responsible for managing the context of the Servlet (that is, the configuration information of the Servlet), and uses the addServlet() method to add MyServlet to the ServletContextHandler. Finally, start the Jetty server through the server.start() method.

  1. Run the Jetty server

After completing the above three steps, you can compile and run the Jetty server. Add the main() method to the JettyServer class for testing:

public static void main(String[] args) {
    startJetty();
}
Copy after login

Then, run the main() method, which will start the Jetty server and output the following information on the console:

2018-09-05 14:54:29.289:INFO::main: Logging initialized @212ms
2018-09-05 14:54:29.334:INFO:oejs.Server:main: jetty-8.1.0.RC5
2018-09-05 14:54:29.355:INFO:oejs.AbstractConnector:main: Started SocketConnector@0.0.0.0:8080
Copy after login

At this time, The Jetty server has been started on port 8080. You can use a browser to access "http://localhost:8080" to view the response of MyServlet.

Summary

This article introduces how to use Jetty8 for Web server processing in Java API development. First, you need to download and configure Jetty8, then write a Servlet to handle HTTP requests and responses, and finally initialize and start the Jetty server. Jetty8 is a lightweight and fast-starting web server, ideal for embedding web servers in Java applications.

The above is the detailed content of Using Jetty8 for Web server processing in Java API development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!