Jetty6 embedded in JavaProject

Jetty is an open source servlet container, which provides a runtime environment for Java-based web content, such as JSP and servlet .

Jetty has a deep advantage with its efficient, compact, embedded It is popular, especially the startup speed of Tomcat 6 is beyond the reach.

Jetty as an optional servlet container is just an additional feature, and it is really famous because it is a servlet that can be embedded in other Java code Designed for containers. This means that the development team provides Jetty as a set of Jar files, so you can instantiate the servlet container into an object in your own code and manipulate the container object.

It can provide customers with an application with start, stop and management functions: Jetty only needs 350k of memory for ordinary HTTP services (without servlet), which makes it possible Use it in smart devices. You can provide a web-based control panel and have all the functions of a Java web application without worrying about the pressure brought by those independent containers.

Add jetty in the java project

1, Download http://dist.codehaus.org/jetty/jetty-6.1.14/jetty-6.1.14.zip

2, create a new java project (jettytest)

3. Create a jetty directory under the project directory, and then create four directories, etc, lib, logs, and webapps under the jetty directory

4. Add in the lib directory

core-3.1.1.jar

< p style="font-size: 14px;">ant-1.6.5.jar

jetty-6.1.14.jar

jetty-util-6.1.14.jar

jsp-2.1.jar

jsp-api-2.1.jar

servlet-api-2.5-6.1.14.jar

5. Add the following configuration files in the etc directory

jetty.xml

webdefault.xml

login.properties

realm.properties

6. Modify the content of jetty.xml and assign it to webdefault.xml, webapps directory, realm. The specific location of the properties and logs directory is the outermost directory of the project by default. Since these two are placed under the jetty directory, the corresponding directory location needs to be modified.

/jetty/etc/realm.properties

/jetty/etc/webdefault.xml

/jetty/webapps

/yyyy_mm_dd.request.log

7. Create a new directory manager under the webapps directory (Equivalent to the name of a web project), and then add a.jsp file in this directory

8. Add a java file to start jetty

package hb.jetty;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.xml.XmlConfiguration;
import org.xml.sax.SAXException;

public class JettyServer {

public static void main(String[] args) {
Server server = new Server(8080);
server.setHandler(new DefaultHandler());
XmlConfiguration configuration = null;
try {
configuration = new XmlConfiguration(new FileInputStream("D:\JavaProject\jettytest\jetty\etc\jetty.xml"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (SAXException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
configuration.configure(server);
server.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

9. Enter http://localhost:8080/manager/ in the address bar to display the added jsp file

< strong>How to add servlet to jetty

Follow the above steps

1. Add the WEB-INF directory in the manger directory, and then add a web.xml file in it, the content can be based on Copy the content in the webdefault.xml file and make relevant changes

< ?xml version="1.0" encoding="UTF-8"?>

myweb



filter2
hb.jetty.Filter2



filter2
/filter/*


2. Always enter the following content in the address bar http://localhost:8080/manager/filter/

Directory deconstruction image

2. Set jetyy’s project directory without using configuration files

import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

public class JettyServer2 {

public static void main(String[] args) throws Exception {
//Get the root directory of the project
String relativelyPath=System.getProperty("user.dir");
System.out.println(relativelyPath);

Server server = new Server(8080);

        WebAppContext context = new WebAppContext();
        context.setContextPath("/manager");
// context.setDescriptor("D:/JavaProject/jettytest/jetty/webapps/manager/WEB-INF/web.xml");
// context.setResourceBase("D:/JavaProject/jettytest/jetty/webapps/manager");
        context.setDescriptor(relativelyPath + "/jetty/webapps/manager/WEB-INF/web.xml");
        context.setResourceBase(relativelyPath + "/jetty/webapps/manager");
        context.setParentLoaderPriority(true);
        server.setHandler(context);
  
        server.start();
        server.join();
}

}

Jetty is an open source servlet container, which provides a runtime environment for Java-based web content, such as JSP and servlet.

Jetty has a deep advantage with its efficient, compact, embedded It is popular, especially the startup speed of Tomcat 6 is beyond the reach.

Jetty as an optional servlet container is just an additional feature, and it is really famous because it is a servlet that can be embedded in other Java code Designed for containers. This means that the development team provides Jetty as a set of Jar files, so you can instantiate the servlet container into an object in your own code and manipulate the container object.

It can provide customers with an application with start, stop and management functions: Jetty only needs 350k of memory for ordinary HTTP services (without servlet), which makes it possible Use it in smart devices. You can provide a web-based control panel and have all the functions of a Java web application without worrying about the pressure brought by those independent containers.

Add jetty in the java project

1, Download http://dist.codehaus.org/jetty/jetty-6.1.14/jetty-6.1.14.zip

2, create a new java project (jettytest)

3. Create a jetty directory under the project directory, and then create four directories, etc, lib, logs, and webapps under the jetty directory

4. Add in the lib directory

core-3.1.1.jar

< p style="font-size: 14px;">ant-1.6.5.jar

jetty-6.1.14.jar

jetty-util-6.1.14.jar

jsp-2.1.jar

jsp-api-2.1.jar

servlet-api-2.5-6.1.14.jar

5. Add in the etc directory The following configuration file

jetty.xml

webdefault.xml

login.properties

realm.properties

6. Modify the content of jetty.xml to specify the specific location of webdefault.xml, webapps directory, realm.properties, and logs directory. The default is The outermost directory of the project, because these two are placed under the jetty directory, you need to modify the corresponding directory location.

/jetty/etc/realm.properties

/jetty/etc/webdefault.xml

/jetty/webapps

/yyyy_mm_dd.request.log

7. Create a new directory manager under the webapps directory (Equivalent to the name of a web project), and then add a.jsp file in this directory

8. Add a java file to start jetty

package hb.jetty;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.xml.XmlConfiguration;
import org.xml.sax.SAXException;

public class JettyServer {

public static void main(String[] args) {
Server server = new Server(8080);
server.setHandler(new DefaultHandler());
XmlConfiguration configuration = null;
try {
configuration = new XmlConfiguration(new FileInputStream("D:\JavaProject\jettytest\jetty\etc\jetty.xml"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (SAXException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
configuration.configure(server);
server.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

9. Enter http://localhost:8080/manager/ in the address bar to display the added jsp file

< strong>How to add servlet to jetty

Follow the above steps

1. Add the WEB-INF directory in the manger directory, and then add a web.xml file in it, the content can be based on Copy the content in the webdefault.xml file and make relevant changes

< ?xml version="1.0" encoding="UTF-8"?>

myweb



filter2
hb.jetty.Filter2



filter2
/filter/*


2. Always enter the following content in the address bar http://localhost:8080/manager/filter/

Directory deconstruction image

2. Set jetyy’s project directory without using configuration files

import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

public class JettyServer2 {

public static void main(String[] args) throws Exception {
//Get the root directory of the project
String relativelyPath=System.getProperty("user.dir");
System.out.println(relativelyPath);

Server server = new Server(8080);

        WebAppContext context = new WebAppContext();
        context.setContextPath("/manager");
// context.setDescriptor("D:/JavaProject/jettytest/jetty/webapps/manager/WEB-INF/web.xml");
// context.setResourceBase("D:/JavaProject/jettytest/jetty/webapps/manager");
        context.setDescriptor(relativelyPath + "/jetty/webapps/manager/WEB-INF/web.xml");
        context.setResourceBase(relativelyPath + "/jetty/webapps/manager");
        context.setParentLoaderPriority(true);
        server.setHandler(context);
  
        server.start();
        server.join();
}

}

Leave a Comment

Your email address will not be published.