views:

387

answers:

1

What is the easiest way to edit web.xml from an Eclipse plugin? (I'm looking for an API -- as opposed to using a PrintWriter etc.)

+1  A: 

The easiest way I've found is to get hold of the WebApp object. For instance, to add a servlet to web.xml:

import org.eclipse.jst.j2ee.model.ModelProviderManager;
import org.eclipse.jst.javaee.web.WebApp;

WebApp webApp = (WebApp) ModelProviderManager.getModelProvider(project).getModelObject();
Servlet servlet = WebFactory.eINSTANCE.createServlet();
servlet.setServletName("myServlet"); //$NON-NLS-1$
servlet.setServletClass("org.my.class.path"); //$NON-NLS-1$
webApp.getServlets().add(servlet);

One can add other tags like servlet-mappings and filters similarly.

Nocturne