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