I need to create add servlets at runtime. When I run the following code.
protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title> URI out</title>");
out.println("</head>");
out.println("<body>");
Integer generatedKey = Math.abs(randomiser.nextInt());
out.print(generatedKey);
createServlet(Integer.toString(generatedKey),request.getServletContext());
} finally {
out.println("</body>");
out.println("</html>");
out.close();
}
}
private void createServlet(String generatedKey, ServletContext servletContext) {
String servletMapping = "/"+generatedKey;
ServletRegistration sr = servletContext.addServlet(generatedKey, "com.path.lbs.servlets.testDynamic");
sr.setInitParameter("keyname", generatedKey);
sr.addMapping(servletMapping);
}
I get the following error.
java.lang.IllegalStateException: PWC1422: Unable to configure mapping for servlet 1114600676 of servlet context /123-LBS, because this servlet context has already been initialized
Is it impossible to add new servlets at runtime i.e. after the Servlet Context is initialised or am I doing something wrong?
If there is any error with status code either 404 (Not Found) or 403 (Forbidden ), then ErrorHandler servlet would be called. If the web application throws either ServletException or IOException, then the web container invokes the /ErrorHandler servlet.
Receives notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized.
I'd suggest that an IOException indicates a problem with processing input/output, e.g. problems reading from the request input, or writing the response, whereas a ServletException has more to do with servlet-specific problems, such as errors regarding servlet provisioning/initialisation, and processing requests.
The web container calls the destroy method when it needs to remove the servlet such as at time of stopping server or undeploying the project.
Is it impossible to add new servlets at runtime i.e. after the Servlet Context is initialised?
That's correct. You need to do it in ServletContextListener#contextInitialized()
.
@WebListener
public class Config implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
// Do it here.
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// ...
}
}
However, for your particular functional requirement, a single controller servlet in combination with command pattern is much better suited. You could then add commands (actions) during runtime and intercept on it based on the request URI. See also my answer on Design Patterns web based applications for a kickoff.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With