9, జనవరి 2014, గురువారం



How to pass parameters to a servlet – ServletConfig
Here’s a serlvet code example to demonstrate how to pass a parameter to a servlet by using ServletConfig “init-param” in web.xml
In the deployment descriptor (web.xml)
Put your parameter value in “init-param” and make sure inside the “servlet” element
        <servlet>
               <servlet-name>conig </servlet-name>
               <servlet-class>config</servlet-class>

               <init-param>
                       <param-name>email</param-name>
                       <param-value>admin@email.com</param-value>
               </init-param>
        </servlet>

        <servlet-mapping>
               <servlet-name>config</servlet-name>
               <url-pattern>/config</url-pattern>
        </servlet-mapping>
Servlet code
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException{

               PrintWriter pw = response.getWriter();
               pw.println(getServletConfig().getInitParameter("email"));

        }
The “getServletConfig().getInitParameter(“email”)” method is use to get the ServletConfig parameter value in web.xml. Btw, this parameter only available for this servlet only. If you want a parameter which allow globally access by whole web application, you need put the parameter in servlet context element.
 the above is not a full code to execute but u have to make arrange according to architecture

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి