ServletConfig in Java

Using Servlet Config in Java

 
e-trainings-servlet-config-example

ServletsHome


 

ServletConfig in Java:

Sets initialization parameters for specific servlet in web.xml

ServletConfigDemo.java


package codingzon;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletConfigDemo extends HttpServlet {
   public void init(ServletConfig config)
       throws ServletException{
        super.init(config);
   }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //ServletConfig config=;

        response.setContentType("text/html;charset=UTF-8");
 
       PrintWriter pw = response.getWriter();
 
 
     try {

      pw.println(getInitParameter("Address"));

        } finally {
           pw.close();
        }
    }

}

In web.xml, along with servlet, add the parameter details as shown in below given XML code.


  <servlet>
    <servlet-name>ServletConfigDemo</servlet-name>
    <servlet-class>ServletConfigDemo</servlet-class>
     <init-param>
        <param-name>Address</param-name>
        <param-value>Delhi</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletConfigDemo</servlet-name>
    <url-pattern>/ServletConfigDemo</url-pattern>
  </servlet-mapping>

    ...

  </web-app>

Output:



e-trainings-ervletconfig-demo


 
 
 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Shoppingcart using PHP Sessions - Miniproject

Export Data to Excel with PHP and MySQL