groovy 和jetty 搭建小webapp应用(转)
有时候,我们需要的一个简单的不能再简单的web应用,通常我这时候会用jetty来实现一个嵌入式的web容器,写一个bat,鼠标点一下就能跑起来。 目录结构:
引用---App
---www
---WEB-INF
---web.xml
---index.gsp
---index.groovy
---logs
---lib
---commons-logging.jar
---groovy-all-1.5.7.jar
---jasper.jar
---javax.servlet.jar
---jetty5.1.8-z4.jarweb.xml:web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> <web-app> <display-name>groovy</display-name> <description>groovy</description> <servlet> <servlet-name>GSP</servlet-name> <servlet-class>groovy.servlet.TemplateServlet</servlet-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> </servlet> <servlet> <servlet-name>Groovy</servlet-name> <servlet-class>groovy.servlet.GroovyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Groovy</servlet-name> <url-pattern>*.groovy</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>GSP</servlet-name> <url-pattern>*.gsp</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.gsp</welcome-file> </welcome-file-list> </web-app>
app.groovy(启动脚本):
import org.mortbay.http.HttpContext; import org.mortbay.http.HttpListener; import org.mortbay.http.NCSARequestLog; import org.mortbay.http.SocketListener; import org.mortbay.http.SslListener; import org.mortbay.http.handler.MsieSslHandler; import org.mortbay.jetty.Server; import org.mortbay.jetty.servlet.WebApplicationContext; //应用定义 def port = 8089 def approot = ["/":"www"]//虚拟目录,支持多个["/":"www","/demo":"demo"] SocketListener listener = new SocketListener(); listener.setPort(port); listener.setMinThreads(5); listener.setMaxThreads(100); listener.setMaxIdleTimeMs(30000); listener.setLowResourcePersistTimeMs(2000); listener.setAcceptQueueSize(0); Server server = new Server(); server.addListener(listener); server.setRequestsPerGC(2000); server.setStatsOn(false); approot.each(){key,value -> server.addWebApplication(key, value); } server.start();
request.setAttribute("user","1111") context.getRequestDispatcher("/index.gsp?iuser=admin").forward(request,response)
index.gsp(例子):
Java代码<% println params.iuser println request.getAttribute("user") println request.remoteHost println headers.host %>
windows下的bat: run.bat:
java-classpathlib/groovy-all-1.5.7.jar;lib/javax.servlet.jar;lib/jetty5.1.8-z4.jar;lib/commons-logging.jar;lib/jasper.jargroovy.ui.GroovyMainapp.groovyJava代码
相关推荐
87901735 2019-05-29
lizhengfa 2019-05-17
smallwhale 2011-07-13
HatsuneMiku 2011-06-19
stoneechogx 2019-10-23
quguang 2019-10-23
sarsgetaway 2015-03-18
linuxprobe0 2017-09-23
星月高悬 2011-12-16
积跬步以至千里 2016-07-11
TyCoding 2016-04-08
onlylixiaobei 2015-01-04
CSDNMrWang 2011-05-19
tracyn 2014-04-28
TyCoding 2012-05-19
hwhanwan 2010-06-24
码路心路知行合一 2011-11-18