Join us on Facebook

Please wait..10 Seconds Cancel

1.03.2014

// // Leave a Comment

Demonstration of ServletContext in Java.


ServletContext

Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)
In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.
The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.

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"> 
 
   <context-param> 
    <param-name>filepath</param-name> 
    <param-value>/text.txt</param-value> 
    </context-param> 
    <listener> 
        <listener-class>feeder.init</listener-class> 
    </listener> 
    <servlet> 
        <servlet-name>display</servlet-name> 
        <servlet-class>view.display</servlet-class> 
    </servlet> 

    <servlet-mapping> 
        <servlet-name>display</servlet-name> 
        <url-pattern>/display</url-pattern> 
   </servlet-mapping> 
 
    <session-config> 
        <session-timeout> 
            30 
        </session-timeout> 
    </session-config> 
 
    <welcome-file-list> 
        <welcome-file>display</welcome-file> 
    </welcome-file-list> 
</web-app> 
 

text.txt

This is sample TEXT. 
ABC your line. 
Now this config shows multiple lines that are stored on the single file. 

view.Display class

 
package view; 
 
import java.io.*; 
import java.net.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
 
public class display extends HttpServlet 
 { 
      private int i; 
  protected void processRequest(HttpServletRequest request, HttpServletResponse 
response) 
      throws ServletException, IOException 
  { 
        response.setContentType("text/html;charset=UTF-8"); 
                PrintWriter out = response.getWriter(); 
            try  
    { 
                ServletContext c=getServletContext(); 
                int i1=2; 
                String s = c.getAttribute("i1").toString();      
                i1=Integer.valueOf(s); 
                out.println("<html>");  
                out.println("<head>"); 
                out.println("<title>Display Text</title>");   
                out.println("</head>"); 
                out.println("<body>"); 
                for(i=0;i<i1;i++) 
                { 
                  String val=(String) c.getAttribute("s["+i+"]");  
                  out.println(val); 
                  out.println("<br />"); 
                } 
                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"; 
    } 
} 

feeder.init class

 
package feeder; 
 
import java.io.*; 
import javax.servlet.*; 
 
public class init  implements ServletContextListener 
{ 
    public void contextInitialized(ServletContextEvent arg0) 
    { 
         try  
  { 
                ServletContext c = arg0.getServletContext(); 
                String value = c.getInitParameter("filepath"); 
                InputStream i; 
                BufferedReader br; 
                i = c.getResourceAsStream(value); 
             
                br = new BufferedReader(new InputStreamReader(i)); 
                String line; 
                int i1=0; 
                while ((line=br.readLine()) != null) 
    { 
                  c.setAttribute("s["+i1+"]",line); 
                  i1++; 
                } 
                c.setAttribute("i1",i1); 
          } 
       catch (IOException ex) {} 
        } 
    public void contextDestroyed(ServletContextEvent arg0) 
  { 
          throw new UnsupportedOperationException("Not supported yet."); 
      } 
} 

0 comments:

Post a Comment