Get settings from ServletContext : Context « Servlets « Java






Get settings from ServletContext

 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WebAppProperties extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
      IOException {
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    ServletContext context = getServletContext();
    String displayName = context.getServletContextName();
    if (displayName == null) {
      displayName = "(no display-name element defined)";
    }

    out.println("<html>");
    out.println("<body>");
    out.println("<br>Name: " + displayName);
    out.println("<br>Context: " + req.getContextPath());

    out.println("<h2><center>");
    out.println("Initialization Parameters</center></h2>");
    out.println("<br>");

    out.println("<center><table>");

    Enumeration e = context.getInitParameterNames();

    while (e.hasMoreElements()) {
      String name = (String) e.nextElement();
      out.println("<tr>");
      out.println("<td>" + name + "</td>");
      out.println("<td>" + context.getInitParameter(name) + "</td>");
      out.println("</tr>");
    }
    out.println("</table></center>");

    out.println("</body>");
    out.println("</html>");
    out.flush();
  }
}

 








Related examples in the same category

1.Servlets Context Sample
2.Servlets ServletContextListener Demo
3.Context log
4.Context logger
5.Context binder
6.Context accessor
7.Set the context parameters in web.xml
8.Log in ServletContext
9.Context Attributes Servlet
10.Using Contexts Servlet
11.Context Parameters Servlet