Hello World(ウェブアプリケーション)
  Hello World(ウェブアプリケーション)のソースを実行結果を以下に示す。

  index.html
  <HTML> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>hello</title> </head> <BODY> <h4>hello</h4> <span id="message">message</span><br/> </BODY> </HTML>

  HelloComponent.java
  package woolpack.samples.hello; import java.util.Date; public class HelloComponent { public String getMessage() { return "hello " + new Date(); } }

  HelloServlet.java
  package woolpack.samples.hello; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Arrays; import java.util.regex.Pattern; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.transform.TransformerException; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Node; import woolpack.action.ActionDef; import woolpack.action.ActionInvoker; import woolpack.action.ForwardDef; import woolpack.adapter.OGE; import woolpack.adapter.OGNLUtils; import woolpack.container.ContainerUtils; import woolpack.convert.ConvertUtils; import woolpack.ee.ActionBuilder; import woolpack.ee.EEContext; import woolpack.ee.HttpSessionMap; import woolpack.ee.ServletContextMap; import woolpack.ee.ServletInputStreamFactory; import woolpack.ee.ServletRequestAttributeMap; import woolpack.el.ELUtils; import woolpack.factory.FactoryUtils; import woolpack.fn.Delegator; import woolpack.fn.Fn; import woolpack.fn.FnUtils; import woolpack.html.HtmlUtils; import woolpack.id.IdUtils; import woolpack.utils.Utils; import woolpack.web.WebUtils; import woolpack.xml.XmlTransformerContext; import woolpack.xml.XmlTransformerUtils; import woolpack.xml.XmlUtils; /** * Hello World Web アプリケーションです。 * @author nakamura * */ public class HelloServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public final transient Delegator<String, Node, Exception> toNode; public final transient Fn<EEContext, Void, Exception> fn; public HelloServlet() { super(); OGNLUtils.setting(); final ActionBuilder actionBuilder = new ActionBuilder(new ActionInvoker( FnUtils.switching(Utils.map( "index", new ActionDef( ELUtils.NULL, new ForwardDef("index", new OGE("container.helloComponent")))))), Arrays.asList("name", "id")); toNode = new Delegator<String, Node, Exception>(null); fn = FnUtils.seq(Utils.<Fn<? super EEContext, Void, ? extends Exception>> list(IdUtils.<Void, RuntimeException>convertId(ConvertUtils.convertRegExp(Pattern.compile("^.*/([^/]+)$"), "$1"))) .list(WebUtils.setContainerContext(FnUtils.exec(FnUtils.join( ContainerUtils.getKey(), FnUtils.castTo(String.class), FnUtils.switching( Utils.map( "helloComponent", WebUtils.request(FactoryUtils.newInstance(HelloComponent.class))) ))))) .list(actionBuilder.getActionExpression()) .list(XmlUtils.<EEContext, Exception>setNode(FnUtils.join(IdUtils.GET_ID, toNode))) .list(HtmlUtils.NORMALIZE_CASE) .list(actionBuilder.getAutoUpdateExpression()) ); } @Override public void init(final ServletConfig servletConfig) throws ServletException { super.init(servletConfig); toNode.setFn(FnUtils.join( ConvertUtils.convertRegExp(Pattern.compile("^(.*)$"), "/html/sample/hello/$1.html"), XmlUtils.nodeFactory( FactoryUtils.inputStreamReaderFactory(new ServletInputStreamFactory<IOException>( servletConfig.getServletContext()), "UTF-8"), XmlTransformerUtils.TRANSFORMER))); } @Override protected void service( final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { final EEContext context = new EEContext(); context.setId(request.getRequestURI()); context.setInput(request.getParameterMap()); context.setRequest(new ServletRequestAttributeMap(request)); context.setSession(Utils.concurrentMap(new HttpSessionMap( request.getSession()), request.getSession())); context.setApplication(Utils.concurrentMap(new ServletContextMap( request.getSession().getServletContext()), request.getSession().getServletContext())); try { fn.exec(context); } catch (final Exception e) { throw new ServletException(e); } final Writer w = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8")); try { final XmlTransformerContext tc = new XmlTransformerContext(); tc.setSource(new DOMSource(context.getNode())); tc.setResult(new StreamResult(w)); XmlTransformerUtils.TRANSFORMER.exec(tc); } catch (final TransformerException e) { throw new ServletException(e); } finally { w.close(); } } }

  web.xml
  <?xml version="1.0"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>hello</servlet-name> <servlet-class>woolpack.samples.hello.HelloServlet</servlet-class> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello/*</url-pattern> </servlet-mapping> </web-app>

  以下は実行結果です。

  <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <META content="text/html; charset=UTF-8" http-equiv="Content-Type"> <TITLE>hello</TITLE> </HEAD> <BODY> <H4>hello</H4> <SPAN id="message">hello Tue Jan 23 16:24:26 JST 2007</SPAN> <BR> </BODY> </HTML>

トップに戻る
Copyright (C) 2006-2007 Takahiro Nakamura. All rights reserved.