Join us on Facebook

Please wait..10 Seconds Cancel

1.03.2014

// // Leave a Comment

Demonstration of ServletConfig in Java.

ServletConfig

public interface ServletConfig
A servlet configuration object used by a servlet container to pass information to a servlet during initialization.

Web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <servlet> 
        <servlet-name>configdemo</servlet-name> 
        <servlet-class>view.configdemo</servlet-class> 
        <init-param> 
            <param-name>Numbers</param-name> 
             <param-value>1,2,3,4,5,6,7</param-value> 
    </init-param> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>configdemo</servlet-name> 
        <url-pattern>/configdemo</url-pattern> 
    </servlet-mapping> 
    <session-config> 
        <session-timeout> 
            30 
        </session-timeout> 
    </session-config> 
    <welcome-file-list> 
        <welcome-file>configdemo</welcome-file> 
        </welcome-file-list> 
    </web-app> 

Configdemo.java

 
package view; 
 
import java.io.*; 
import java.net.*; 
 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
public class configdemo extends HttpServlet { 
    protected void processRequest(HttpServletRequest request, HttpServletResponse 
response) 
    throws ServletException, IOException { 
        response.setContentType("text/html;charset=UTF-8"); 
        PrintWriter out = response.getWriter(); 
        try { 
             
            ServletConfig c=getServletConfig(); 
            String course=c.getInitParameter("Numbers"); 
            String s[]=course.split(","); 
             
            out.println("<html>"); 
            out.println("<head>"); 
            out.println("<title>Servlet config demo</title>");   
            out.println("</head>"); 
            out.println("<body>");   out.println("<select name='n1'>"); 
            for(int i=0;i<s.length;i++) 
            { 
            out.println("<option value="+s[i]+" ''>"+s[i]+" </option>"); 
            } 
            out.println("</body>"); 
            out.println("</html>"); 
        } finally {  
            out.close(); 
        } 
    }  
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
        processRequest(request, response); 
    }  
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
        processRequest(request, response); 
    } 
 
     
    public String getServletInfo() { 
        return "Short description"; 
    } 
} 
 

0 comments:

Post a Comment