Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do we use servletconfig interface and servletcontext interface [closed]

Tags:

java

jsp

servlets

Hi all I am new in servlet and jsp I am not clear with servletconfig interface and servletcontext interface I started jsp again I encounter the term PageContext. So any body explain me these term with the nice example.

servletconfig interface and servletcontext interface and PageContext in jsp

like image 395
Varun Avatar asked Apr 14 '15 09:04

Varun


People also ask

What is the use of ServletConfig and ServletContext?

ServletConfig and ServletContext, both are objects created at the time of servlet initialization and used to provide some initial parameters or configuration information to the servlet.

What are the advantages of ServletConfig interface?

The core advantage of ServletConfig is that you don't need to edit the servlet file if information is modified from the web. xml file.

Why do we use ServletConfig?

ServletConfig is an object containing some initial parameters or configuration information created by Servlet Container and passed to the servlet during initialization. ServletConfig is for a particular servlet, which means one should store servlet-specific information in web. xml and retrieve them using this object.

What is the difference between ServletContext and PageContext?

Q9. What is the major difference between ServletContext and PageContext? Ans: The major difference between ServletContect and PageContext is, the ServletContext is designed to provide information about the Container and on the other hand, the PageContext is designed to provide information about the Request.


2 Answers

ServletConfig

ServletConfig object is created by web container for each servlet to pass information to a servlet during initialization.This object can be used to get configuration information from web.xml file.

when to use : if any specific content is modified from time to time. you can manage the Web application easily without modifing servlet through editing the value in web.xml

Your web.xml look like :

 <web-app>  
      <servlet>  
        ......     
        <init-param>  
        <!--here we specify the parameter name and value -->
          <param-name>paramName</param-name>  
          <param-value>paramValue</param-value>  
        </init-param> 
        ......  
      </servlet>  
    </web-app>

This way you can get value in servlet :

public void doGet(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
     //getting paramValue
    ServletConfig config=getServletConfig();  
    String driver=config.getInitParameter("paramName"); 
    } 

ServletContext

web container create one ServletContext object per web Application. This object is used to get information from web.xml

when to use : If you want to share information to all sevlet, it a better way to make it available for all servlet.

web.xml look like :

<web-app>  
 ......  

  <context-param>  
    <param-name>paramName</param-name>  
    <param-value>paramValue</param-value>  
  </context-param>  
 ......  
</web-app>  

This way you can get value in servlet :

public void doGet(HttpServletRequest request,HttpServletResponse response)  
throws ServletException,IOException  
{  
 //creating ServletContext object  
ServletContext context=getServletContext();  

//Getting the value of the initialization parameter and printing it  
String paramName=context.getInitParameter("paramName");   
}  

PageContext

Is class in jsp, its implicit object pageContext is used to set , get or remove attribute from following scope:

1.page

2.request

3.session

4.application

like image 182
dut gurung Avatar answered Sep 22 '22 11:09

dut gurung


ServletConfig is implemented by GenericServlet (which is a superclass of HttpServlet). It allows the application deployer to pass parameters to the servlet (in the web.xml global config file), and servlet to retrieve those parameters during its initialization.

For example, your web.xml could look like :

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.company.(...).MyServlet</servlet-class>
    <init-param>
        <param-name>someParam</param-name>
        <param-value>paramValue</param-value>
    </init-param>
</servlet>

In you servlet, the "someParam" param can then be retrieved like this :

public class MyServlet extends GenericServlet {
    protected String myParam = null;

    public void init(ServletConfig config) throws ServletException {
        String someParamValue = config.getInitParameter("someParam");
    }
}

ServletContext is a bit different. It is quite badly named, and you'd better think of it as "Application scope".

It is an application-wide scope (think "map") that you can use to store data that is not specific to any user, but rather belongs to the application itself. It is commonly used to store reference data, like the application's configuration.

You can define servlet-context parameters in web.xml :

<context-param>
        <param-name>admin-email</param-name>
        <param-value>[email protected]</param-value>
</context-param>

And retrieve them in your code like this in your servlet :

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    String adminEmail = getServletContext().getInitParameter("admin-email")); 
}
like image 28
Olivier Croisier Avatar answered Sep 21 '22 11:09

Olivier Croisier