Jetty Embedded WebApp development (simple Demo) __jetty

Source: Internet
Author: User

Time can take everything---road


Jetty is a lightweight servlet container, a bit like Tomcat, but it's easier to use than Tomcat.

Jetty Download Link: http://download.eclipse.org/jetty/

My development environment:

Operating system: UBuntu 14.04 LTS

Integration tools: Eclipse (Version:luna Service Release 1 (4.4.1) build id:20140925-1800)

Jetty Version: jetty-9.2.6


Let's go to the Hello World tour below.

Open Eclipse, right-click "New" in "Package Explorer"--> "Java Project", and in the pop-up box, fill in the project name "Jettywebapp" and "Finish". As shown in the picture is the construction of a good project


The jetty directory and the Webroot directory are built as shown in the picture.


The project was built, now the most important don't forget the package dependencies, find the following jar package in the downloaded Jetty package and import the package (referenced libraries).


Plus Javax.el-3.0.0.jar (screenshots cut it off)


The following is the source code

Start.java

Package start;

Import Org.eclipse.jetty.server.Server;
Import Org.eclipse.jetty.webapp.WebAppContext;

public class Start {public
	static void Main (string[] args) {
		Server server = new Server (8080);
		
		Webappcontext context = new Webappcontext ();
		Context.setcontextpath ("/jettywebapp");
		Context.setdescriptor ("Webroot/web-inf/web.xml");
		Context.setresourcebase ("WebRoot");
		Context.setparentloaderpriority (true);
		
		Server.sethandler (context);
		
		try {
			server.join ();
			Server.start ();
		} catch (Interruptedexception e) {
			//TODO auto-generated catch block
			e.printstacktrace ();
		} catch ( Exception e) {
			//TODO auto-generated catch block
			e.printstacktrace ();
		}
	}
}


Xml

<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi=
"Http://www.w3.org/2001/XMLSchema-instance" "Xmlns=" Http://java.sun.com/xml/ns/javaee "xmlns:web=" http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version= " 3.0 ">
  <display-name>jettyWebApp</display-name>
  <welcome-file-list>
    < welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
    pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

The above operation is completed then a complete Web project has been built, but also deployed almost, on the poor run. Open Start.java and right-click "Run as"--> "Java application". The results of the operation are as follows



The deployment is over.


Open the browser and enter in the Address bar: http://localhost:8080/jettyWebApp/






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.