瀏覽器輸入 http://127.0.0.1:8888/Hello/ 即可看見效果
package server;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
public class Main {
public static void main(String[] args) throws Exception {
Server server = new Server(8888);
ContextHandler context = new ContextHandler();
context.setContextPath("/Hello");
context.setHandler(new HelloLoli());
server.setHandler(context);
server.start();
server.join();
}
}
package server;
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.handler.AbstractHandler;
public class HelloLoli extends AbstractHandler {
@Override
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 Loli !!</h1>");
}
}
留言列表