• JSP
  • PHP
  • SQL Tutorial
  • Servlet
  • RubyonRails
  • Core Java
  • Struts
  • JavaScript
  • Java IO

  • <

    Java Servlet Context

    It is created when web application deployed.It is used to get configuration from web.xml.Servlet context is the communication link between servlets.Only one ServletContext is available per web application.Can access application level parameter.


    ServletContext Methods


  • public String getInitParameter(String param)
  • public Enumeration getInitParameterNames()
  • public void setAttribute(String name,Object object)
  • public Object getAttribute(String name)
  • public String getServerInfo()
  • public String getContextPath()

  • Java Servlet Context Example


    dservlet.java
    import java.io.*;  
    import javax.servlet.*;  
    import javax.servlet.http.*;  
      
      
    public class Servlet extends HttpServlet{  
    public void doGet(HttpServletRequest req,HttpServletResponse res)  
    throws ServletException,IOException  
    {  
    res.setContentType("text/html");  
    PrintWriter pw=res.getWriter();  
     
    ServletContext context=getServletContext();  
    
    
    String driverName=context.getInitParameter("drivername");  
    pw.println("driver name is="+driverName);  
      
    pw.close();  
      
    }}  
    


    web.xml
    <web-app>  
      
    <servlet>  
    <servlet-name>protutorialplus</servlet-name>  
    <servlet-class>dservlet</servlet-class>  
    </servlet>  
      
    <context-param>  
    <param-name>dname</param-name>  
    <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
    </context-param>  
      
    <servlet-mapping>  
    <servlet-name>protutorialplus</servlet-name>  
    <url-pattern>/context</url-pattern>  
    </servlet-mapping>  
      
    </web-app>  
    
















    © copyright 2017-2021 Completedone pvt ltd.