In one of the servlets' configuration, configure a Servlet Initialization parameter and retrieve this parameter value in the servlet code. Configure a Context Initialization parameter and retrieve it in any servlet.
Configuration for the servlet (along with Servlet Initialization Parameter) and the Context Initialization parameter,
URL in browser:
package info.icontraining.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InitParametersDemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
String servletInitParam = getServletConfig().getInitParameter("initParam");
String contextInitParam = getServletContext().getInitParameter("contextParam");
out.println("<html><body>");
out.println("<p>Servlet Init Parameter Value = ");
out.println(servletInitParam + "<br/><br/>");
out.println("Context Init Parameter Value = ");
out.println(contextInitParam + "<br/><br/>");
out.println("</p></body></html>");
out.close();
}
}
Configuration for the servlet (along with Servlet Initialization Parameter) and the Context Initialization parameter,
<servlet>
<servlet-name>initParametersDemoServlet</servlet-name>
<servlet-class>info.icontraining.servlets.InitParametersDemoServlet</servlet-class>
<init-param>
<param-name>initParam</param-name>
<param-value>initValue</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>initParametersDemoServlet</servlet-name>
<url-pattern>/initParameters</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextParam</param-name>
<param-value>contextValue</param-value>
</context-param>
URL in browser:
http://localhost:8080/WebAppName/initParameters
No comments:
Post a Comment