Jetty is a lightweight embedded servlet container. It is easy to start and run. There are several steps to run the jetty container in Eclipse,
1. Create a common Java Project
Jar is added to make the project support JSP files)
3. Create a web directory. A folder named web contains a WEB-INF folder and a web. xml file.
The content of the web. xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
4. Compile a Java code as follows:
Java code
Package test; import Org. mortbay. jetty. connector; import Org. mortbay. jetty. server; import Org. mortbay. jetty. NIO. selectchannelconnector; import Org. mortbay. jetty. webapp. webappcontext; public class bootstart {public static void main (string [] ARGs) throws exception {Server service = new server (); connector conne= new selectchannelconne (); // set the port connector. setport (8080); // set the host address connector. sethost ("127.0.0.1"); service. setconnectors (new connector [] {connector}); // set the root path webappcontext context = new webappcontext ("Web", "/Web"); service. addhandler (context); service. setstopatshutdown (true); service. setsendserverversion (true); // start the service. start (); service. join ();}}
5. Test the JSP file index. jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
6. Run the main method and test the connection http: // 127.0.0.1: 8080/Web
Run helloworld Jetty