Skip to content

Instantly share code, notes, and snippets.

@masahitojp
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masahitojp/73f14fb967bb4816de31 to your computer and use it in GitHub Desktop.
Save masahitojp/73f14fb967bb4816de31 to your computer and use it in GitHub Desktop.
apply plugin: 'idea'
apply plugin: 'java'
group = 'me.masahito'
version = "0.0.1"
sourceCompatibility = targetCompatibility = 1.8
repositories.mavenCentral()
dependencies {
compile 'io.undertow:undertow-core:1.1.1.Final'
compile 'io.undertow:undertow-servlet:1.1.1.Final'
compile "javax.servlet:javax.servlet-api:3.1.0"
}g
package me.masahito;
import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by masahito on 15/01/07.
*/
public class Main {
public static void main(String[] args) throws Exception {
final DeploymentInfo servletBuilder = Servlets.deployment()
.setClassLoader(Main.class.getClassLoader())
.setContextPath("/myapp")
.setDeploymentName("test.war")
.addServlets(
Servlets.servlet("HelloServlet", HelloServlet.class)
.addMapping("/*"));
final DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
manager.deploy();
final PathHandler path = Handlers.path(Handlers.redirect("/myapp"))
.addPrefixPath("/myapp", manager.start());
final Undertow server = Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(path)
.build();
server.start();
}
public static class HelloServlet extends HttpServlet {
private static final long serialVersionUID = -6154475799000019575L;
private static final String greeting = "Hello World";
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(greeting);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment