3 Tutorial
© 2009 Thorsten Kisner
The listing Listing 1 demonstrated how to start the jetty server in embedded mode. All servlets used in this example are added to the Context.
Listing 1: Starting Jetty
public JwanJettyServer(int port) { try { Server server = new Server(port); ContextHandlerCollection ctxHC = new ContextHandlerCollection(); server.setHandler(ctxHC); ArrayList<WanLayer> alSubLayer = new ArrayList<WanLayer>(); MenuLayerServlet menuLayerServlet = new MenuLayerServlet(); alSubLayer.add(menuLayerServlet); SimpleMenuServlet simpleMenuServlet = new SimpleMenuServlet(); simpleMenuServlet.addSubLayer(alSubLayer); simpleMenuServlet.setLyrMenu(menuLayerServlet); JwanHelloWorldServlet jwanHelloWorld = new JwanHelloWorldServlet(); Context ctxJwan = new Context(ctxHC,"/jwan",Context.SESSIONS); ctxJwan.addServlet(new ServletHolder(jwanHelloWorld), "/hello"); ctxJwan.addServlet(new ServletHolder(simpleMenuServlet), "/menu"); ctxJwan.addServlet(new ServletHolder(menuLayerServlet), "/"+menuLayerServlet.getLayerTarget().getTargetLink()); ctxJwan.addServlet(new ServletHolder(new ServletMultiRessourceLoader()), "/WebApp/*"); server.start(); server.join(); } catch (Exception e) {logger.error(e);} }
Listing: JwanHelloWorldServlet
public class JwanHelloWorldServlet extends AbstractWanServletPage { static Logger logger = Logger.getLogger(JwanHelloWorldServlet.class); public static final long serialVersionUID=1; public JwanHelloWorldServlet() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setStatus(HttpServletResponse.SC_OK); response.setContentType("text/html"); WanContentLayer mainLayer = new WanContentLayer("Home"); mainLayer.setLayerTitle("Home"); mainLayer.setSubDiv(true); mainLayer.addContent(new WanParagraph("Hello World")); title = "Hello World Title"; setMainLayer(mainLayer); PrintWriter out = response.getWriter(); try{out.println(render());} catch (WanRenderException e) {logger.error(e);e.printStackTrace();} finally {out.close();} } }
Abb. 1: Servlet Hello World
Listing: net.sf.jwan.tutorial.servlet.SimpleMenuServlet
public class SimpleMenuServlet extends AbstractWanServletPage { static Logger logger = Logger.getLogger(SimpleMenuServlet.class); public static final long serialVersionUID=1; private WanLayer lyrMenu; public SimpleMenuServlet() { title = "Simple Menu"; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setStatus(HttpServletResponse.SC_OK); response.setContentType("text/html"); WanMenu wm = new WanMenu(); wm.setTitle("Main Menu"); WanMenuEntry wmi = new WanMenuEntry(); wmi.setName("jWAN Item"); HtmlHref href = lyrMenu.getLayerTarget(); href.setRev(HtmlHref.Rev.async); wmi.setHtmlref(href); wm.addItem(wmi); WanContentLayer mainLayer = new WanContentLayer("Home"); mainLayer.setLayerTitle("Home"); mainLayer.setSubDiv(false); mainLayer.addContent(wm); setMainLayer(mainLayer); PrintWriter out = response.getWriter(); try{out.println(render());} catch (WanRenderException e) {logger.error(e);e.printStackTrace();} finally {out.close();} } public void setLyrMenu(WanLayer lyrMenu) {this.lyrMenu = lyrMenu;} }
Listing Listing 2 shows the next layer in the menu. The response is rendered in an asychronous way. Not the whole HTML page is transmitted, only the WanDiv wd.
The result is shown in figure Abb. 2. The navigation Home on the right screen is automatically generated.