Saturday 29 March 2014

Servlets : Webserver vs Application Server


Click here to watch in Youtube : https://www.youtube.com/watch?v=WkaLSZ888as

Click the below Image to Enlarge
Webserver vs Application Server
See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Servlets : Application Server



    Click here to watch in Youtube : https://www.youtube.com/watch?v=ntQlB2fWyE0

    Click the below Image to Enlarge
    Application Server
    Application Server
    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Servlets : ServletLifeCycle - Example


    Click here to watch in Youtube : https://www.youtube.com/watch?v=6AcVTFcZkCk

    Click the below Image to Enlarge

    ServletInterface Project Dir Structure
    ServletLifeCycle - Example

    ServletsLifeCycle.java
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    // Extend HttpServlet class
    public class ServletsLifeCycle extends HttpServlet
    {
    
      private static final long serialVersionUID = 1L;
      private int i;
    
      public void init() throws ServletException
      {
        System.out.println("------------------------------------------------------");
        System.out.println(" Init method is called ");
        System.out.println("------------------------------------------------------");
      }
    
      public void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
                      IOException
      {
        ++i;
        System.out.println(" service method is called "+i+ " time");
        // Set response content type
        response.setContentType("text/html");
    
        // Actual logic goes here.
        PrintWriter out = response.getWriter();
        out.println("<h1>" + " service method is called "+i+ " time" + "</h1>");
      }
    
      public void destroy()
      {
        System.out.println("------------------------------------------------------");
        System.out.println(" destroy method is called ");
        System.out.println("------------------------------------------------------");
      }
    }
    

    web.xml
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app 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_3_0.xsd"
      version="3.0"
      metadata-complete="true">
    
        <display-name>ServletsLifeCycle Application</display-name>
        <description>
            This is a simple web application with a source code organization
            based on the recommendations of the Application Developer's Guide.
        </description>
    
         <servlet>
            <servlet-name>ServletsLifeCycle</servlet-name>
            <servlet-class>ServletsLifeCycle</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>ServletsLifeCycle</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
        
    </web-app>
    


    index.html
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    <!DOCTYPE HTML><html lang="en"><head>
    <meta charset="UTF-8">
    <title>ServletsLifeCycle</title>
    </head>
    <body>
    <p>
    <h3>ServletsLifeCycle</H3>
    <p></p>
    <ul>
    <li><a href="hello">ServletsLifeCycle Test</a></li>
    
    </ul>
    </body></html>
    

    Environment Used

    JDK version :1.6.0_30
    Tomcat version : 7.0.50 

    To Download HelloworldApp Project Click the below link

    https://sites.google.com/site/javaee4321/servlets/ServletsLifeCycleApp.zip?attredirects=0&d=1

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Servlets : Webserver


    Click here to watch in Youtube : https://www.youtube.com/watch?v=ZY6SO-qeb08

    Click the below Image to Enlarge
    Webserver
    Webserver
    Webserver

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Tuesday 25 March 2014

    Servlets : ServletConfig


    Click here to watch in Youtube : https://www.youtube.com/watch?v=cfwI_bdwQa4

    Click the below Image to Enlarge
    ServletConfig
    ServletConfig
    ServletConfig

    ServletConfig Project Dir Structure
    ServletConfig

    ServletConfigServlet.java
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Enumeration;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class ServletConfigServlet extends HttpServlet
    {
    
      private static final long serialVersionUID = 1L;
    
      ServletConfig             servletConfig           = null;
    
      public void init( ServletConfig servletConfig )
      {
        this.servletConfig = servletConfig;
        System.out.println("\n-----------------------------------------------------");
        System.out.println("init method has been called and servlet is initialized");
        System.out.println("-----------------------------------------------------");
    
        System.out.println("Using 'getInitParameter(String name)' method get the value");
        String driverName = servletConfig.getInitParameter("driverName");
        System.out.println("Driver Name : " + driverName);
    
        System.out.println("\nUsing 'getInitParameterNames()'  method get the value");
        Enumeration<String> paramNames = servletConfig.getInitParameterNames();
    
        while( paramNames.hasMoreElements() )
        {
          String paramName = paramNames.nextElement();
    
          String value = servletConfig.getInitParameter(paramName);
    
          System.out.println("paramName : " + paramName + ", Value: " + value);
    
        }
    
      }
    
      public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
                      IOException
      {
    
        System.out.println("\n-----------------------------------------------------");
        System.out.println("doGet method has been called");
        System.out.println("-----------------------------------------------------");
    
        ServletConfig servletConfig = getServletConfig();
        System.out.println("Got servletconfig object using getServletConfig() method : " + servletConfig);
        System.out.println("Servlet Name : " + servletConfig.getServletName());
        System.out.println("ServletContext Object : " + servletConfig.getServletContext());
    
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print("<html><body>");
        out.print("<b>hello HTTP servlet</b>");
        out.print("</body></html>");
      }
    
      public void destroy()
      {
        System.out.println("-----------------------------------------------------");
        System.out.println("destroy method has been called and servlet is destroyed");
        System.out.println("-----------------------------------------------------");
      }
    
      public ServletConfig getServletConfig()
      {
        return servletConfig;
      }
    }
    

    web.xml
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0">
      <display-name>ServletConfigServlet</display-name>
      <description>
            This is a simple web application with a source code organization
            based on the recommendations of the Application Developer's Guide.
        </description>
      <servlet>
        <servlet-name>ServletConfigServlet</servlet-name>
        <servlet-class>ServletConfigServlet</servlet-class>
        
         <init-param>  
          <param-name>driverName</param-name>  
          <param-value>oracle.jdbc.driver.OracleDriver</param-value>  
        </init-param>
        
         <init-param>  
          <param-name>username</param-name>  
          <param-value>ram</param-value>  
        </init-param>
        
        <init-param>  
          <param-name>password</param-name>  
          <param-value>ram</param-value>  
        </init-param>   
        
      </servlet>
      
      <servlet-mapping>
        <servlet-name>ServletConfigServlet</servlet-name>
        <url-pattern>/servletConfig</url-pattern>
      </servlet-mapping>
    </web-app>
    

    index.html
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <!DOCTYPE HTML><html lang="en"><head>
    <meta charset="UTF-8">
    <title>ServletConfigServlet</title>
    </head>
    <body>
    <p>
    <h3>ServletConfigServlet</H3>
    <p></p>
    <ul>
    
    <li><a href="servletConfig">ServletConfigServlet</a></li>
    
    </ul>
    </body></html>
    

    Environment Used: 

    JDK version :1.6.0_30
    Tomcat version : 7.0.50 

    To Download ServletConfigApp Project Click the below link

    https://sites.google.com/site/javaee4321/servlets/ServletConfigApp.zip?attredirects=0&d=1

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Monday 24 March 2014

    Servlets : HTTPServlet


    Click here to watch in Youtube : https://www.youtube.com/watch?v=JUFBrXOgAlk

    Click the below Image to Enlarge
    HTTPServlet

    HTTPServlet Project Dir Structure

    HTTPServlet

    HelloHttpServlet.java
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class HelloHttpServlet extends HttpServlet
    {
    
      private static final long serialVersionUID = 1L;
    
      ServletConfig             config           = null;
    
      public void init( ServletConfig config )
      {
        this.config = config;
        System.out.println("-----------------------------------------------------");
        System.out.println("init method has been called and servlet is initialized");
        System.out.println("-----------------------------------------------------");
      }
    
      public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
                      IOException
      {
    
        System.out.println("-----------------------------------------------------");
        System.out.println("doGet method has been called");
        System.out.println("-----------------------------------------------------");
    
        response.setContentType("text/html");
    
        PrintWriter out = response.getWriter();
        out.print("<html><body>");
        out.print("<b>hello HTTP servlet</b>");
        out.print("</body></html>");
      }
    
      public void destroy()
      {
        System.out.println("-----------------------------------------------------");
        System.out.println("destroy method has been called and servlet is destroyed");
        System.out.println("-----------------------------------------------------");
      }
    
    }
    

    web.xml
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0">
      <display-name>HelloHttpServlet</display-name>
      <description>
            This is a simple web application with a source code organization
            based on the recommendations of the Application Developer's Guide.
        </description>
      <servlet>
        <servlet-name>HelloHttpServlet</servlet-name>
        <servlet-class>HelloHttpServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>HelloHttpServlet</servlet-name>
        <url-pattern>/helloHttpServlet</url-pattern>
      </servlet-mapping>
    </web-app>
    

    index.html
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <!DOCTYPE HTML><html lang="en"><head>
    <meta charset="UTF-8">
    <title>HelloHttpServlet</title>
    </head>
    <body>
    <p>
    <h3>HelloHttpServlet</H3>
    <p></p>
    <ul>
    
    <li><a href="helloHttpServlet">HelloHttpServlet</a></li>
    
    </ul>
    </body></html>
    

    Environment Used: 

    JDK version :1.6.0_30
    Tomcat version : 7.0.50 

    To Download HTTPServletApp Project Click the below link

    https://sites.google.com/site/javaee4321/servlets/HTTPServletApp.zip?attredirects=0&d=1 

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Sunday 23 March 2014

    Servlets : GenericServlet


    Click here to watch in Youtube : https://www.youtube.com/watch?v=ieweBKuDVe8

    Click the below Image to Enlarge
    GenericServlet

    GenericServlet Project Dir Structure
    GenericServlet
    HelloGenericServlet.java

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.GenericServlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public class HelloGenericServlet extends GenericServlet
    {
    
      private static final long serialVersionUID = 1L;
    
      ServletConfig             config           = null;
    
      public void init( ServletConfig config )
      {
        this.config = config;
        System.out.println("-----------------------------------------------------");
        System.out.println("init method has been called and servlet is initialized");
        System.out.println("-----------------------------------------------------");
      }
    
      public void service( ServletRequest req, ServletResponse res ) throws IOException, ServletException
      {
        System.out.println("-----------------------------------------------------");
        System.out.println("service method has been called");
        System.out.println("-----------------------------------------------------");
    
        res.setContentType("text/html");  
          
        PrintWriter out=res.getWriter();  
        out.print("<html><body>");  
        out.print("<b>hello generic servlet</b>");  
        out.print("</body></html>");  
    
      }
    
      public void destroy()
      {
        System.out.println("-----------------------------------------------------");
        System.out.println("destroy method has been called and servlet is destroyed");
        System.out.println("-----------------------------------------------------");
      }
    
    }
    

    web.xml

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0">
      <display-name>HelloGenericServlet</display-name>
      <description>
            This is a simple web application with a source code organization
            based on the recommendations of the Application Developer's Guide.
        </description>
      <servlet>
        <servlet-name>HelloGenericServlet</servlet-name>
        <servlet-class>HelloGenericServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>HelloGenericServlet</servlet-name>
        <url-pattern>/helloGenericServlet</url-pattern>
      </servlet-mapping>
    </web-app>
    

    index.html

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <!DOCTYPE HTML><html lang="en"><head>
    <meta charset="UTF-8">
    <title>HelloGenericServlet</title>
    </head>
    <body>
    <p>
    <h3>HelloGenericServlet</H3>
    <p></p>
    <ul>
    
    <li><a href="helloGenericServlet">HelloGenericServlet</a></li>
    
    </ul>
    </body></html>
    

    Environment Used: 

    JDK version :1.6.0_30
    Tomcat version : 7.0.50

    To Download GenericServletApp Project Click the below link

    https://sites.google.com/site/javaee4321/servlets/GenericServletApp.zip?attredirects=0&d=1 

     See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Servlets : Client Server Architecture of the Internet | Client--server Model


    Click here to watch in Youtube :  https://www.youtube.com/watch?v=b5l8D3YBcdI

    Click the below Image to Enlarge
    Client Server Architecture of the Internet | Client--server Model
    Client Server Architecture of the Internet | Client--server Model

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Servlets : Client Server Architecture | Client--server Model


    Click here to watch in Youtube :  https://www.youtube.com/watch?v=6g62gt5f0TI

    Click the below Image to Enlarge
    Client Server Architecture | Client--server Model


    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Servlets : What is Server


    Click here to watch in Youtube : https://www.youtube.com/watch?v=WovjF_nTJdY

    Click the below Image to Enlarge
    What is Server

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Servlets : Servlet Interface


    Click here to watch in Youtube :  https://www.youtube.com/watch?v=X9AheaJGMBI

    Click the below Image to Enlarge
    Servlet Interface

    ServletInterface Project Dir Structure
    Servlet Interface

    FirstServlet.java

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public class FirstServlet implements Servlet
    {
      ServletConfig config = null;
    
      public void init( ServletConfig config )
      {
        this.config = config;
        System.out.println("-----------------------------------------------------");
        System.out.println("init method has been called and servlet is initialized");
        System.out.println("-----------------------------------------------------");
      }
    
      public void service( ServletRequest req, ServletResponse res ) throws IOException, ServletException
      {
        System.out.println("-----------------------------------------------------");
        System.out.println("service method has been called");
        System.out.println("-----------------------------------------------------");
    
        res.setContentType("text/html");
    
        PrintWriter out = res.getWriter();
        out.print("<html><body>");
        out.print("<b>hello simple servlet</b>");
        out.print("<br>");
        out.print("<b>Servlet Info :</b> "+getServletInfo());
        out.print("<br>");
        out.print("<b>Get Servlet config object :</b> "+getServletConfig());
        
        out.print("</body></html>");
    
      }
    
      public void destroy()
      {
        System.out.println("-----------------------------------------------------");
        System.out.println("destroy method has been called and servlet is destroyed");
        System.out.println("-----------------------------------------------------");
      }
    
      public ServletConfig getServletConfig()
      {
        return config;
      }
    
      public String getServletInfo()
      {
        return "copyright 2014-01-01 version : 3.0";
      }
    
    }
    

    Web.xml

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     metadata-complete="true" version="3.0">
     <display-name>Servlet Interface</display-name>
     <description>
            This is a simple web application with a source code organization
            based on the recommendations of the Application Developer's Guide.
        </description>
     <servlet>
      <servlet-name>FirstServlet</servlet-name>
      <servlet-class>FirstServlet</servlet-class>
    
     </servlet>
     <servlet-mapping>
      <servlet-name>FirstServlet</servlet-name>
      <url-pattern>/firstServlet</url-pattern>
     </servlet-mapping>
    
    </web-app>
    

    index.html

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <!DOCTYPE HTML><html lang="en"><head>
    <meta charset="UTF-8">
    <title>ServletInterface</title>
    </head>
    <body>
    <p>
    <h3>ServletInterface</H3>
    <p></p>
    <ul>
    
    <li><a href="firstServlet">FirstServlet</a></li>
    
    </ul>
    </body></html>
    

    Environment Used:

    JDK version :1.6.0_30
    Tomcat version : 7.0.50 

    To Download ServletInterfaceApp Project Click the below link

    https://sites.google.com/site/javaee4321/servlets/ServletInterfaceApp.zip?attredirects=0&d=1 
     
    See also:

  • Servlets Tutorial
  • All Design Patterns Links