- Standalone server
There is no installation procedure for Jetty. Just unzip.
Run[code language=”java”]
java -jar start.jar
[/code]If success
[code language=”java”]
INFO:oejs.Server:main: Started @11245ms
[/code]By default, Jetty runs on port 8080. To change port open start.ini and change the line
[code language=”java”]
## Connector port to listen on
jetty.http.port=8070
[/code]and restart Jetty.
To deploy a app copy *.war file to webapps folder and open page by[code language=”java”]
http://localhost:8070/name_of_war_file
[/code]To change WebApps folder you need to edit start.ini file
[code language=”java”]
# Monitored directory name (relative to $jetty.base)
# jetty.deploy.monitoredDir=webapps
[/code] - Embedded server
- Maven dependencies
[code language=”java”]
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.11.v20150529</version>
</dependency>
[/code] -
Develop app
[code language=”java”]
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.servlet.*;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpStatus;public class jmain {
public static void Main(String[] args) {
Server sever = new Server(7077);
ServletContextHandler handler = new ServletContextHandler(sever, "/example");
handler.addServlet(ExampleServlet.class, "/");
sever.start();
}public class ExampleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setStatus(HttpStatus.OK_200);
resp.getWriter().println("EmbeddedJetty");
}
}
}
[/code]
- Maven dependencies