package org.kodejava.example.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.Servlet;
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 ContextInitParameter extends HttpServlet implements Servlet {
public ContextInitParameter() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
// Get an instance of ServletContext
ServletContext context = getServletContext();
// To read context initialization parameter we can call context.getInitParameter()
// and pass the name of initialization parameter to be read. If the named
// parameter does not exists the returned value will be null.
//
// In this example we read an initialization parameter called LOG.PATH
String logPath = context.getInitParameter("LOG.PATH");
writer.println("Log Path: " + logPath + "<br/>");
// Reads all the name of servlet's initialization parameters. If the
// servlet doesn't have any an empty enumeration will be returned.
Enumeration enumeration = context.getInitParameterNames();
while (enumeration.hasMoreElements()) {
String paramName = (String) enumeration.nextElement();
String paramValue = context.getInitParameter(paramName);
writer.println("Context Init Param: [" + paramName + " = " + paramValue + "]<br/>");
}
}
}
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019