Question :
Example of ServletConfig to get all the
initialization parameters
Ans :
we are getting all the initialization parameter
from the web.xml file and printing this information in the servlet.
DemServlet.java
1. import java.io.IOException;
2. import java.io.PrintWriter;
3. import java.util.Enumeration;
4.
5. import javax.servlet.ServletConfig;
6. import javax.servlet.ServletException;
7. import javax.servlet.http.HttpServlet;
8. import javax.servlet.http.HttpServletRequest;
9. import javax.servlet.http.HttpServletResponse;
10.
11.
12. public class DemServlet extends HttpServlet {
13. public void doGet(HttpServletRequest request, HttpServletResponse response)
14. throws ServletException, IOException {
15.
16. response.setContentType("text/html");
17. PrintWriter out = response.getWriter();
18.
ServletConfig config=getServletConfig();
Enumeration<String> e=config.getInitParameterNames();
String str="";
while(e.hasMoreElements()){
str=e.nextElement();
out.print("<br>Name: "+str);
out.print(" value: "+config.getInitParameter(str));
}
out.close();
19. }
20.
21. }
web.xml
1. <web-app>
2.
3. <servlet>
4. <servlet-name>DemServlet</servlet-name>
5. <servlet-class>DemServlet</servlet-class>
6.
<init-param>
<param-name>username</param-name>
<param-value>system</param-value>
</init-param>
7.
8. <init-param>
9. <param-name>password</param-name>
10. <param-value>oracle</param-value>
11. </init-param>
12.
13. </servlet>
14.
15. <servlet-mapping>
16. <servlet-name>DemServlet</servlet-name>
17. <url-pattern>/servlet1</url-pattern>
18. </servlet-mapping>
19.
20. </web-app>
If this information is important to you , and you want to , I will continue writing some more details of programming language, so click on the advertising, available on this page. This is motivate me for writing some more blogs.
Comments
Post a Comment