Java WebApp embedding Jetty__web

Source: Internet
Author: User

Why the Java Web is not very popular among student groups. When I think about it, I rarely use Java as a Web site in college. Why, then?

First, the development, debugging inconvenient

Two, too bulky, need to rely on too many jars, build an empty environment, write a Hello world will experience vomiting blood ...

Three, a variety of Chinese garbled


Solve the development debugging problem, here to introduce a way to integrate jetty into the WebApp, as long as the JDK and WebApp code can be quickly developed, debugging, where the jetty embedding is directly embedded in the jetty of the boot code into the project, Not in the form of Eclipse Plug-ins or mvn plug-ins.

Advantages: rapid development, portability, debug startup debugging is very convenient


One, the dependent jar

		<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactid>jetty</ artifactid>
			<version>6.1.24</version>
			<scope>test</scope>
		</ dependency>
		<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactid >jsp-2.1-jetty</artifactId>
			<version>6.1.22</version>
			<scope>provided</ Scope>
		</dependency>


Second, start class

/** * WebApp an embedded jetty container startup class. * * @author Lywybo * @version $Id: Jettystarter.java, v 0.1 2012-8-26 pm 02:39:38 Lywybo EXP $/public class Jettysta

    Rter {/** Log object/private static log = Logfactory.getlog (Jettystarter.class);
        /** * @param args */public static void main (string[] args) {Server jettyserver = null;
            try {//Load jetty configuration information URL jettyconfig = Classloader.getsystemresource ("Jetty/jetty-config.xml");
            if (Jettyconfig = = null) {log.fatal ("Unable to locate jetty-config.xml on the Classpath");
            //Start jetty Service jettyserver = new Server ();
            Xmlconfiguration configuration = new Xmlconfiguration (jettyconfig);
            Configuration.configure (Jettyserver);
        Jettyserver.start ();
        catch (Exception e) {log.fatal ("Could not start the Jetty server:" + E); }
    }
}

Three, jetty server's configuration file

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE Configure Public "-//mort Bay consulting//dtd configure//en" "Http://jetty.mortbay.org/configure.dtd" > < Configure id= "Server" class= "Org.mortbay.jetty.Server" > <set name= "ThreadPool" > <new class= " Org.mortbay.thread.BoundedThreadPool "> <set name=" minthreads ">10</Set> <set name=" MaxThreads " >250</Set> <set name= "lowthreads" >25</Set> </New> </Set> <property name= "org.m Ortbay.util.URI.charset "default=" UTF-8 "/> <call name=" AddConnector "> <Arg> <new class=" ORG.MORTB Ay.jetty.nio.SelectChannelConnector "> <set name=" host "> <systemproperty name=" jetty.host "/> &
				lt;/set> <set name= "Port" > <systemproperty name= "jetty.port" default= "no"/> </Set> <set name= "MaxIdleTime" >30000</Set> <set name= "acceptors" >2</Set> <set name= "Statson">false</Set> <set name= "Confidentialport" >8443</Set> <set name= "Lowresourcesconnections" &G t;5000</set> <set name= "Lowresourcesmaxidletime" >5000</Set> </New> </Arg> </cal l> <set name= "Handler" > <new id= "Handlers" class= "Org.mortbay.jetty.handler.HandlerCollection" > < Set name= "Handlers" > <array type= "Org.mortbay.jetty.Handler" > <Item> <new id= "Contexts" c lass= "Org.mortbay.jetty.handler.ContextHandlerCollection"/> </Item> <Item> <new id= "Def Aulthandler "class=" Org.mortbay.jetty.handler.DefaultHandler "/> </Item> <Item> <new id= "Requestlog" class= "Org.mortbay.jetty.handler.RequestLogHandler"/> </Item> </Array> &LT;/SET&G
		T </New> </Set> <set name= "Handler" > <new id= "Handlers" class= "Org.mortbay.jetty.handler.HandlerC" Ollection "> &LT Set name= "Handlers" > <array type= "Org.mortbay.jetty.Handler" > <Item> <new class= "Org.mort Bay.jetty.webapp.WebAppContext "> <set name=" contextpath ">/</Set><!--contextpath-->-&L T Set name= "resourcebase" >./src/main/resources</Set><!--Web application root directory--> <set name= "descriptor" >./s Rc/main/resources/web-inf/web.xml</set> <!--WEB application root directory--> <call name= "ADDSERVL ET "> <Arg>org.mortbay.jetty.servlet.DefaultServlet</Arg> <Arg>/</Arg> &L
		T;/call> <!--Add other servlet--> </New> </Item> </Array> </Set>
	</New> </Set> <set name= "Userrealms" > <array type= "Org.mortbay.jetty.security.UserRealm"/> </Set> <set name= "Stopatshutdown" >true</Set> <set name= "Sendserverversion" >true</set > <set name= "GracefulShutdown ">1000</Set> </Configure> 


Four, the operation

The above information can be configured successfully after the Jettystart.java right button run or Debug. Debug time as long as not to change the method, the name of the interface, you can do without restarting the server, direct debugging, very convenient, fast.


V. Problems that may be encountered

Unable to find a javac compiler;
The Com.sun.tools.javac.Main is isn't on the classpath. Perhaps Java_home does not point to the JDK at Org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory.getCompiler (compileradapterfactory.java:105) at Org.apache.tools.ant.taskdefs.Javac.compile (javac.java:924) at Org.apache.tools.ant.taskdefs.Javac.execute ( javac.java:757) at Org.apache.jasper.compiler.AntCompiler.generateClass (antcompiler.java:219) at Org.apache.jasper.compiler.Compiler.compile (compiler.java:297) at Org.apache.jasper.compiler.Compiler.compile ( compiler.java:276) at Org.apache.jasper.compiler.Compiler.compile (compiler.java:264) at Org.apache.jasper.JspCompilationContext.compile (jspcompilationcontext.java:563) at Org.apache.jasper.servlet.JspServletWrapper.service (jspservletwrapper.java:303) at Org.apache.jasper.servlet.JspServlet.serviceJspFile (jspservlet.java:314) at Org.apache.jasper.servlet.JspServlet.service (jspservlet.java:264) at Javax.servlet.http.HttpServlet.serVice (httpservlet.java:802) at Org.mortbay.jetty.servlet.ServletHolder.handle (servletholder.java:511) at Org.mortbay.jetty.servlet.ServletHandler.handle (servlethandler.java:390) at Org.mortbay.jetty.security.SecurityHandler.handle (securityhandler.java:216) at Org.mortbay.jetty.servlet.SessionHandler.handle (sessionhandler.java:182) at Org.mortbay.jetty.handler.ContextHandler.handle (contexthandler.java:765) at Org.mortbay.jetty.webapp.WebAppContext.handle (webappcontext.java:418) at Org.mortbay.jetty.handler.HandlerCollection.handle (handlercollection.java:114) at Org.mortbay.jetty.handler.HandlerWrapper.handle (handlerwrapper.java:152) at Org.mortbay.jetty.Server.handle ( server.java:326) at Org.mortbay.jetty.HttpConnection.handleRequest (httpconnection.java:542) at Org.mortbay.jetty.httpconnection$requesthandler.headercomplete (httpconnection.java:926) at Org.mortbay.jetty.HttpParser.parseNext (httpparser.java:549) at Org.mortbay.jetty.HttpParser.parseAvailable ( httpparser.java:212)
	At Org.mortbay.jetty.HttpConnection.handle (httpconnection.java:404) at Org.mortbay.io.nio.SelectChannelEndPoint.run (selectchannelendpoint.java:410) at Org.mortbay.thread.boundedthreadpool$poolthread.run (boundedthreadpool.java:451)

In the embedded jetty and need to run JSP, you need to rely on the following package, or you will report the above error
<dependency>
	<groupId>org.mortbay.jetty</groupId>
	<artifactid>jsp-2.1-jetty </artifactId>
	<version>6.1.22</version>
	<scope>provided</scope>
< /dependency>


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.