Reading Initialization parameters:
Syntax to provide the initialization parameter for a servlet
The init-param sub-element of servlet is used to specify the initialization parameter for a servlet.
- <web-app>
- <servlet>
- ......
-
- <init-param>
- <param-name>parametername</param-name>
- <param-value>parametervalue</param-value>
- </init-param>
- ......
- </servlet>
- </web-app>
ServletConfig to get initialization parameter
In this example, we are getting the one initialization parameter from the web.xml file and printing this information in the servlet.
DemoServletInit.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
-
- public class DemoServletInit extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- response.setContentType("text/html");
- PrintWriter PW = response.getWriter();
-
- ServletConfig config=getServletConfig();
- String name=config.getInitParameter("name");
- pw.print("Name is: "+driver);
-
- pw.close();
- }
-
- }
- Here web.xml is updated as following
<servlet>
<servlet-name>D</servlet-name>
<servlet-class>DemoServletInit</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>DiyaShirley</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>D</servlet-name>
<url-pattern>/Demo</url-pattern>
</servlet-mapping>
- after executing output displays like following:
Labels: Advanced Java ( Unit 3 )
5 Comments:
Ghjjkkk
Iam getting Null as output
What is the driver in this case ?
Just use pw.print("Name is:"+name);
That should fix this.
Thanks for this!
Post a Comment
Subscribe to Post Comments [Atom]
<< Home