import javax.servlet.ServletContext; import java.util.Enumeration; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = getServletContext(); Enumeration initParams = context.getInitParameterNames(); while (initParams.hasMoreElements()) { String paramName = (String) initParams.nextElement(); String paramValue = context.getInitParameter(paramName); System.out.println(paramName + " = " + paramValue); } } }
import javax.servlet.ServletContext; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.*; public class MyServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = getServletContext(); String databaseURL = context.getInitParameter("databaseURL"); String databaseUsername = context.getInitParameter("databaseUsername"); String databasePassword = context.getInitParameter("databasePassword"); // Use the initialization parameters to connect to the database // ... } }This example gets specific initialization parameters (`databaseURL`, `databaseUsername`, `databasePassword`) from the `web.xml` file and uses them to connect to a database. Package library: javax.servlet