jWAN
titel
Get jWAN at SourceForge.net. Fast, secure and Free Open Source software downloads
Startseite

3  Tutorial

3

Tutorial

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);}
 }

3.1

Hello World

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

3.2

Simple Menu

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.

Listing 2: net.sf.jwan.tutorial.servlet.MenuLayerServlet

public class MenuLayerServlet extends AbstractWanServletLayer
{ 
 static Logger logger = Logger.getLogger(MenuLayerServlet.class);
 public static final long serialVersionUID=1;
 
 public MenuLayerServlet()
 {
  super("lMls");
  layerTitle = "MenuLayerServlet";
  layerServletPath = "async";
 }
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 {  
  alWanRenderables.clear();
  response.setContentType("text/xml");
  response.setStatus(HttpServletResponse.SC_OK);
  
  WanDiv wd = new WanDiv();
   wd.setDivclass(WanDiv.DivClass.iBlock);
   wd.addContent(new WanParagraph("Here we are!"));
  alWanRenderables.add(wd);
  
  PrintWriter out = response.getWriter();
  try{out.println(renderAsync());}
  catch (WanRenderException e) {logger.error(e);}
  finally {out.close();}
 }
}

The result is shown in figure Abb.  2. The navigation Home on the right screen is automatically generated.

Abb.  2: Menu and MenuLayer