Saturday, October 13, 2012

Servlet Config and Context


ServletConfig:

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

Methods of ServletConfig:

java.lang.String getInitParameter(java.lang.String name)
          
Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.

java.util.Enumeration getInitParameterNames()
     
Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.

ServletContext  getServletContext()

          Returns a reference to the ServletContext in which the caller is executing.
 
java.lang.String getServletName()

          Returns the name of this servlet instance.


To get the ServletConfig Object

        ServletConfig config = getServletConfig();




web.xml


Servlet_demo

index.html



  simpleServlet
  com.jsl.servlets.SimpleServlet
  
   driver
   oracle.jdbc.driver.OracleDriver
  


  simpleServlet
  /simple


SimpleServlet.java

packagecom.jsl.servlets;

importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassSimpleServletextendsHttpServlet {
 privatestaticfinallongserialVersionUID = 1L;
 protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
   ServletConfigconfig=getServletConfig();
   response.setContentType("text/html");
   PrintWriter out=response.getWriter();
   out.print("<html><head><title>View Dept Details</head>");
   out.print("<body>");
   String driver=config.getInitParameter("driver");
   out.print("

The Driver name is using servlet 3.0 :

"+driver); out.print("</body>"); out.print("</html>"); } protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { } }




             Servlet 3.0 the init parameters can be set by using the annotaions. we no need to configure in the web.xml



packagecom.jsl.servlets;

importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebInitParam;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
@WebServlet(
 urlPatterns={"/simple"},
 initParams={
   @WebInitParam(name="driver",value="jdbc.oracle.driver.OracleDrvier")
 }
)
publicclassSimpleServletextendsHttpServlet {
 privatestaticfinallongserialVersionUID = 1L;
 protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
   ServletConfigconfig=getServletConfig();
   response.setContentType("text/html");
   PrintWriter out=response.getWriter();
   out.print("<html><head><title>View Dept Details</head>");
   out.print("<body>");
   String driver=config.getInitParameter("driver");
   out.print("

The Driver name is using servlet 3.0 :

"+driver); out.print("</body>"); out.print("</html>"); } protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { } }


Servlet Context

public interface 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. 


The context parameters can be set in the web.xml

    <web-app> 
        <context-param>
               <param-name>driver</param-name>
               <param-value>oracle.jdbc.driver.OracleDriver</param-value>
          </context-param>
    </web-app>



To read this context-param

    ServletContext context=getServletContext();
    String dirver=context.getInitParam("driver");


Diffrence Between the ServletConfig and ServletContext 

ServletConfig
ServletContext
A servlet configuration object used by a servlet container to pass information to a servlet during initialization. 
A ServletContext defines a set of methods that a servlet uses to communicate with its servlet container.
For each servlet there will one ServletConfig object
The entire web application will have one ServletContext Object
The parameters specified in web.xml inside the <servlet> tag
The paramemters are specified in web.xml inside <web-app>
These parameters can be read by using config.getInitParameter(“name”);
These parameters can be read by using context.getInitParameter(“name”);

No comments:

Post a Comment

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...