Read The Init-Params Defined In The web.xml

[eg. of web.xml

<web-app>
<servlet>
<servlet-name>ReadInitParams</servlet-name>
<servlet-class>com.company.MyServlet</servlet-class>
<display-name>Example Servlet</display-name>
<init-param>
<param-name>emailHost</param-name>
<param-value>192.168.1.1</param-value>
</init-param>
</servlet>
</webapp>

In Servlet Code:

public void init(ServletConfig config) throws ServletException {
String emailHost = config.getInitParameter(“emailHost”);
}

or

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String host = getServletConfig().getInitParameter(“emailHost”);
}

In JSP Code:

<html>
<head><title>Read Init params from a JSP</title></head>
<body>
<%
String emailHost = null;

public void jspInit() {
ServletConfig config = getServletConfig();
emailHost = config.getInitParameter(“emailHost”);
}
%>
<table border=”1″>
<tr><td>email server</td><td><%=emailHost%></td></tr>
</table>

<% // It can also be read directly from the implicit object – config %>
<%=config.getInitParameter(“sys”)%><br><br>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.