X

Geertjan's Blog

  • October 19, 2005

Writing to web.xml from a NetBeans Project Template

Geertjan Wielenga
Product Manager
I created a module project, used the Project Template wizard, declared a module dependency on J2EE DD API and then added this to the iterator's instantiate() method, in order to add a servlet, servlet mapping, and welcome file to the project's deployment descriptor (web.xml) and to let the IDE open it when the project is created:

FileObject webXmlFo = dir.getFileObject("web/WEB-INF/web.xml");
WebApp webApp = DDProvider.getDefault().getDDRoot(webXmlFo);
DataObject webXmlDo = DataObject.find(webXmlFo);
try {
Servlet servlet = (Servlet) webApp.createBean("Servlet");
servlet.setServletName(name);
servlet.setServletClass("wicket.protocol.http.WicketServlet");
ServletMapping mapping = (ServletMapping) webApp.createBean("ServletMapping");
mapping.setServletName(name);
mapping.setUrlPattern("/"+name+"/\*");
WelcomeFileList welcomefilelist = (WelcomeFileList) webApp.createBean("WelcomeFileList");
welcomefilelist.addWelcomeFile("index.html");
webApp.setWelcomeFileList(welcomefilelist);
webApp.addServlet(servlet);
webApp.addServletMapping(mapping);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
webApp.write(webXmlFo);
OpenCookie open = (OpenCookie) webXmlDo.getCookie(OpenCookie.class);
if (open != null) {
open.open();
}

By the way, the data object is only there so that the OpenCookie can be used. For details on data objects, see the NetBeans DataLoader Module Tutorial. My shortcut understanding of data objects vs. file objects is that data objects are higher up in the evolutionary chain and can therefore "do" more things, such as consume cookies without breaking teeth...

Join the discussion

Comments ( 1 )
  • guest Monday, February 27, 2006
    wat to add?
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.