当前位置: 代码迷 >> Web前端 >> jetty配备
  详细解决方案

jetty配备

热度:567   发布时间:2012-11-16 14:12:14.0
jetty配置
package jetty;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mortbay.jetty.Server;
import org.mortbay.xml.XmlConfiguration;
import org.springframework.util.Log4jConfigurer;
import org.xml.sax.SAXException;


public class JServer {
	
  private static Log LOG = LogFactory.getLog(JServer.class);
  
  public final static String JETTYLOG = "log1234" ;//自己设定
  public final static String JETTYHOME = "./WebContent" ;
  
  public static void main(String[] args) {
  Server server = new Server();
  XmlConfiguration configuration = null;
  System.setProperty("jetty.logs", JETTYLOG);
  System.setProperty("jetty.home", JETTYHOME);
  try {
	Log4jConfigurer.initLogging("./WebContent/WEB-INF/log4j.properties");
	configuration = new XmlConfiguration(new File( "./WebContent/WEB-INF/jetty.xml").toURI().toURL());
	configuration.configure(server);
	server.start();
	if(server.isStarting()){
		LOG.info("starting");
	}
	if(server.isStarted()){
		LOG.info("started");
	}
	if(server.isRunning()){
		LOG.info("Running");
	}
    } catch (MalformedURLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	} catch (SAXException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}
}

<?xml version="1.0"?>
<Configure id="Server" class="org.mortbay.jetty.Server">

	<!-- =========================================================== -->
	<!-- Server Thread Pool -->
	<!-- =========================================================== -->
	<Set name="ThreadPool">
		<New class="org.mortbay.thread.QueuedThreadPool">
			<!-- initial threads set to 10 -->
			<Set name="minThreads">10</Set>

			<!-- the thread pool will grow only up to 200 -->
			<Set name="maxThreads">200</Set>

			<!-- indicates that having 20 and below, the pool will be considered low 
				on threads -->
			<Set name="lowThreads">20</Set>

			<!-- The number of queued jobs (or idle threads) needed before the thread 
				pool is grown (or shrunk) -->
			<Set name="SpawnOrShrinkAt">2</Set>
		</New>
	</Set>

	<!-- =========================================================== -->
	<!-- Set connectors -->
	<!-- =========================================================== -->

	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<!-- To add a HTTPS SSL connector -->
	<!-- mixin jetty-ssl.xml: -->
	<!-- java -jar start.jar etc/jetty.xml etc/jetty-ssl.xml -->
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<Call name="addConnector">
        <Arg>
            <New class="org.mortbay.jetty.nio.SelectChannelConnector">
                <!-- the ip address or domain to bind -->
                <Set name="host">
                    <SystemProperty name="jetty.host" />
                </Set>

                <!-- the port to use/bind, defaults to 8080 if property not set -->
                <Set name="port">
                    <SystemProperty name="jetty.port" default="7979" />
                </Set>

                <!-- the time in milliseconds when a connection is considered idle -->
                <Set name="maxIdleTime">300000</Set>

                <!-- the number of acceptors (their job is to accept the connection and dispatch to thread pool) -->
                <Set name="Acceptors">2</Set>

                <!-- should the connection statistics be turned on? (Not advisable in production) -->
                <Set name="statsOn">false</Set>

                <!-- the confidential port -->
                <Set name="confidentialPort">8443</Set>

                <!-- indicates the minimum number of connections when the server is considered low on resources -->
                <Set name="lowResourcesConnections">20000</Set>

                <!-- when low on resources, this indicates the maximum time (milliseconds) a connection must be idle to not be closed -->
                <Set name="lowResourcesMaxIdleTime">5000</Set>
            </New>
        </Arg>
    </Call>

	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<!-- To add a HTTP blocking connector -->
	<!-- mixin jetty-bio.xml: -->
	<!-- java -jar start.jar etc/jetty.xml etc/jetty-bio.xml -->
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

	<!-- =========================================================== -->
	<!-- Set handler Collection Structure -->
	<!-- =========================================================== -->
	<Set name="handler">
		<!-- the collection of handlers that will handle the request -->
		<New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">
			<Set name="handlers">
				<Array type="org.mortbay.jetty.Handler">
					<!-- primarily handles the request and maps the request to a ContextHandler -->
					<Item>
						<New id="Contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection" />
					</Item>

					<!-- The default handler ... handles the request if not yet handled -->
					<Item>
						<New id="DefaultHandler" class="jetty.MyHandler" />
					</Item>

					<!-- The handler for your request logs -->
					<Item>
						<New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler" />
					</Item>
				</Array>
			</Set>
		</New>
	</Set>

	<!-- =========================================================== -->
	<!-- Configure the context deployer -->
	<!-- A context deployer will deploy contexts described in -->
	<!-- configuration files discovered in a directory. -->
	<!-- The configuration directory can be scanned for hot -->
	<!-- deployments at the configured scanInterval. -->
	<!-- -->
	<!-- This deployer is configured to deploy contexts configured -->
	<!-- in the $JETTY_HOME/contexts directory -->
	<!-- -->
	<!-- =========================================================== -->
	<Call name="addLifeCycle">
		<Arg>
			<New class="org.mortbay.jetty.deployer.ContextDeployer">
				<!-- the ContextHandlerCollection to modify once a webapp is added or 
					removed (Allows Hot Deployment) -->
				<Set name="contexts">
					<Ref id="Contexts" />
				</Set>

				<!-- the directory which will contain your context.xml files -->
				<Set name="configurationDir">
					file:///<SystemProperty name="jetty.home" default="." />/contexts
				</Set>

				<!-- the interval in milliseconds to periodically scan the configurationDir -->
				<Set name="scanInterval">5</Set>
			</New>
		</Arg>
	</Call>

	<!-- =========================================================== -->
	<!-- Configure the webapp deployer. -->
	<!-- A webapp deployer will deploy standard webapps discovered -->
	<!-- in a directory at startup, without the need for additional -->
	<!-- configuration files. It does not support hot deploy or -->
	<!-- non standard contexts (see ContextDeployer above). -->
	<!-- -->
	<!-- This deployer is configured to deploy webapps from the -->
	<!-- $JETTY_HOME/webapps directory -->
	<!-- -->
	<!-- Normally only one type of deployer need be used. -->
	<!-- -->
	<!-- =========================================================== -->
	<Call name="addLifeCycle">
		<Arg>
			<New class="org.mortbay.jetty.deployer.WebAppDeployer">
				<!-- the ContextHandlerCollection to add the webapps to -->
				<Set name="contexts">
					<Ref id="Contexts" />
				</Set>

				<!-- the directory where all the webapps are located (can be exploded 
					or packaged as war -->
				<Set name="webAppDir">
					<SystemProperty name="jetty.home" default="." />
				</Set>

				<!-- indicates whether to lookup/load from the parent class loader first -->
				<Set name="parentLoaderPriority">false</Set>

				<!-- indicates whether to extract the webapp if it is packaged as a war -->
				<Set name="extract">true</Set>

				<!-- indicates whether a deployed webapp on a certain contextPath should 
					have a duplicate webapp deployment -->
				<Set name="allowDuplicates">false</Set>

				<!-- the default descriptor to use to be applied before a webapps' web.xml -->
				<Set name="defaultsDescriptor">
					<SystemProperty name="jetty.home" default="." />/WEB-INF/web.xml
				</Set>

			</New>
		</Arg>
	</Call>

	<!-- =========================================================== -->
	<!-- Configure Authentication Realms -->
	<!-- Realms may be configured for the entire server here, or -->
	<!-- they can be configured for a specific web app in a context -->
	<!-- configuration (see $(jetty.home)/contexts/test.xml for an -->
	<!-- example). -->
	<!-- =========================================================== -->
	<Set name="UserRealms">
		<Array type="org.mortbay.jetty.security.UserRealm">
			<Item>
				<!-- this realm uses a properties file to store/read the user/password/roles -->
				<New class="org.mortbay.jetty.security.HashUserRealm">
					<!-- the name of the realm -->
					<Set name="name">Test Realm</Set>

					<!-- the location of the property file to load from -->
					<!-- <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set> -->
					<!-- the interval in seconds to periodically scan for any changes and 
						refresh/reload if changed -->
					<Set name="refreshInterval">0</Set>
				</New>
			</Item>
		</Array>
	</Set>

	<!-- =========================================================== -->
	<!-- Configure Request Log -->
	<!-- Request logs may be configured for the entire server here, -->
	<!-- or they can be configured for a specific web app in a -->
	<!-- contexts configuration (see $(jetty.home)/contexts/test.xml -->
	<!-- for an example). -->
	<!-- =========================================================== -->
	<Ref id="RequestLog">
		<Set name="requestLog">
			<New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog">
				<!-- the output file name of the log file. Name of the file will be date 
					formatted -->
				<Set name="filename">
					<SystemProperty name="jetty.logs" default="./log" />/yyyy_mm_dd.request.log
				</Set>

				<!-- the date format -->
				<Set name="filenameDateFormat">yyyy_MM_dd</Set>

				<!-- the days to retain the log file -->
				<Set name="retainDays">90</Set>

				<!-- indicates if the new lines should be appended on an existing log 
					file -->
				<Set name="append">true</Set>

				<!-- indicates if the lines logged to the file will be in extended format -->
				<Set name="extended">true</Set>

				<!-- Indicates if the cookie logs should be included in the log file -->
				<Set name="logCookies">false</Set>

				<!-- the timezone of the log -->
				<Set name="LogTimeZone">GMT</Set>
			</New>
		</Set>
	</Ref>

	<!-- =========================================================== -->
	<!-- extra options -->
	<!-- =========================================================== -->

	<!-- Stops the server when ctrl+c is pressed (registers to Runtime.addShutdownHook) -->
	<Set name="stopAtShutdown">true</Set>

	<!-- send the server version in the response header? -->
	<Set name="sendServerVersion">true</Set>

	<!-- send the date header in the response header? -->
	<Set name="sendDateHeader">true</Set>

	<!-- allows requests(prior to shutdown) to finish gracefully -->
	<Set name="gracefulShutdown">1000</Set>
</Configure>

package jetty;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.jetty.servlet.ServletHandler;

public class MyHandler extends ServletHandler{

	@Override
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, int type) throws IOException,
			ServletException {
		// TODO Auto-generated method stub
		System.out.println("handling");
		PrintWriter pw = response.getWriter();
		
        pw.println("w de 我的  goodbye");
        pw.flush();
        pw.close();
	}
 
}

log4j.rootLogger=INFO,CONSOLE,rolling

#log4j.logger.org.springframework=ERROR
#log4j.logger.org.apache=ERROR

log4j.logger.com.ssc.rdr=INFO

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=DEBUG 
log4j.appender.CONSOLE.Target=System.out 
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 
log4j.appender.CONSOLE.layout.ConversionPattern=%d - %c -%-4r [%t] %-5p %c %x - %m%n 
 
log4j.appender.rolling=org.apache.log4j.RollingFileAppender
log4j.appender.rolling.File=${jetty.logs}/rdrServer.log
log4j.appender.rolling.MaxBackupIndex=100
log4j.appender.rolling.MaxFileSize=10MB
log4j.appender.rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.rolling.layout.ConversionPattern=%t %d{ABSOLUTE} %5p %c{1} - %m%n


<?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">
	<display-name>RDR</display-name>
 
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

jar包:
spring.jar
jetty-6.1.25.jar
jetty-util-6.1.25.jar
servlet-api-2.5-20081211.jar
commons-logging.jar
  相关解决方案