Jetty (i) gradle building a Web project __web

Source: Internet
Author: User

Interllij + gradle + Jetty + Java + Web

1.jettyserver.java

Package com.mind.server;

Import Org.eclipse.jetty.server.Server;
Import Org.eclipse.jetty.servlet.ServletContextHandler;
Import Org.eclipse.jetty.servlet.ServletHolder;

public class Jettyserver {public

    static void Main (string[] args) throws Exception {
        Server server = new Server (80 );

        Servletcontexthandler context = new Servletcontexthandler (servletcontexthandler.sessions);
        Context.setcontextpath ("/");
        Server.sethandler (context);

        Http://localhost:8030/hello
        Context.addservlet (New Servletholder (New Firstservlet ()), "/hello");
        Http://localhost:8030/hello/web Context.addservlet (New Servletholder ("This is my
        Web"), "/ Hello/web ");

        try {
            server.start ();
            Server.join ();
        } finally {
            Server.destroy ();}}}

2. Firstservlet.java

Package com.mind.server;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

    public class Firstservlet extends HttpServlet {private String msg = "Hello world!";
    Public Firstservlet () {} public Firstservlet (String msg) {this.msg = msg; } protected void Doget (HttpServletRequest request, httpservletresponse response) throws Serv
    Letexception, IOException {this.process (request, response); } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Se
    Rvletexception, IOException {this.process (request, response); private void process (HttpServletRequest request, httpservletresponse response) throws Serv Letexception, IOException {respOnse.setcontenttype ("Text/html;charset=utf-8");
        Response.setcharacterencoding ("Utf-8");
        PrintWriter out = Response.getwriter ();
        String title= "Webapp Demo"; Out.println ("<!
        DOCTYPE HTML public \ "-//W3C//DTD XHTML 1.0 strict//en\" \ "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd\" > ");
        Out.println (" 

Run Result:


Gradle

Configure Build_gradle:

Group ' gradle-idea-demo1 '
version ' 1.0-snapshot '

apply plugin: ' java '
Apply plugin: ' War '

Sourcecompatibility = 1.5

repositories {
    mavencentral ()
}

Dependencies {
    //import local dependency pack
    Compile Filetree (dir: ' Libs ', include: [' *.jar '])
    testcompile group: ' JUnit ', Name: ' JUnit ', Version: ' 4.11 '

    / /import dependent
    compile ' org.eclipse.jetty.aggregate:jetty-webapp:7.6.15.v20140411 '} from Maven

warehouse

Project Address: Https://github.com/Minhowe/gradle-idea-demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.