Thursday, 29 May 2014

Servlets : Cookies


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

Click the below Image to Enlarge
Servlets : Cookies


Servlets : Cookies
Servlets : Cookies
See also:

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


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

    Click the below Image to Enlarge
    Servlets : Attribute
    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Servlets : HttpSession Attribute


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

    Click the below Image to Enlarge

    HttpSessionAttributeDemo Project Dir Structure   

    FirstServlet.java
    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;
    import javax.servlet.http.HttpSession;
    
    public class FirstServlet extends HttpServlet
    {
        private static final long serialVersionUID = 1L;
    
        public void init() throws ServletException
        {
            System.out.println("----------------------------------------------");
            System.out.println(" Init method is called in "
                    + this.getClass().getName());
            System.out.println("--------------------------------------------");
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
    
            String username = request.getParameter("username");
            out.print("Welcome " + username);
    
            HttpSession session = request.getSession();
            System.out.println("FirstServlet sessionid :" + session.getId());
            session.setAttribute("Username", username);
            session.setAttribute("Role", "Admin");
    
            out.print("<a href='servlet2'> : visit</a>");
    
            out.close();
    
        }
    
        public void destroy()
        {
            System.out.println("----------------------------------------------");
            System.out.println(" destroy method is called in "
                    + this.getClass().getName());
            System.out.println("----------------------------------------------");
        }
    
    }
    

    SecondServlet.java
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Enumeration;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class SecondServlet extends HttpServlet
    {
        private static final long serialVersionUID = 1L;
    
        public void init() throws ServletException
        {
            System.out.println("----------------------------------------------");
            System.out.println(" Init method is called in "
                    + this.getClass().getName());
            System.out.println("----------------------------------------------");
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
    
            HttpSession session = request.getSession();
    
            System.out.println("Second Servlet sessionid :" + session.getId());
    
            String userName = (String) session.getAttribute("Username");
            out.print("Hello " + userName + "<br>");
    
            out.print("*************************************************************"
                    + "<br>");
    
            Enumeration<String> namesEnum = session.getAttributeNames();
            while (namesEnum.hasMoreElements())
            {
                String name = (String) namesEnum.nextElement();
                String value = (String) session.getAttribute(name);
                out.print(name + ":" + value);
                out.print("<br>");
    
            }
    
            out.close();
        }
    
        public void destroy()
        {
            System.out.println("----------------------------------------------");
            System.out.println(" destroy method is called in "
                    + this.getClass().getName());
            System.out.println("----------------------------------------------");
        }
    
    }
    

    web.xml
    <?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>HttpSessionAttributeDemo</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>s1</servlet-name>
            <servlet-class>FirstServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>s1</servlet-name>
            <url-pattern>/servlet1</url-pattern>
        </servlet-mapping>
    
        <servlet>
            <servlet-name>s2</servlet-name>
            <servlet-class>SecondServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>s2</servlet-name>
            <url-pattern>/servlet2</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    index.html
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Multiple Filters Demo</title>
    </head>
    <body>
        <form action="servlet1" method="GET">
            
            UserName :  <input type="text" name="username" /><br /> 
            
            Password :&nbsp; &nbsp; <input type="password" name="password" /><br /> 
                
            <input type="submit" value="login" />
            
        </form>
    </body>
    </html>
    
    

    Environment Used 

    JDK version :1.7.0_51
    Tomcat version : 7.0.50 

    To Download HttpSessionAttributeDemoApp Project Click the below link

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

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Thursday, 22 May 2014

    Servlets : HttpSession Interface


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

    Click the below Image to Enlarge
    Servlets : HttpSession Interface
    Servlets : HttpSession Interface
    Servlets : HttpSession Interface
    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Tuesday, 6 May 2014

    Servlets : Page Redirection


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

    Click the below Image to Enlarge


    PageRedirection Project Dir Structure



    PageRedirectServlet.java
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class PageRedirectServlet extends HttpServlet
    {
    
        private static final long serialVersionUID = 1L;
    
        public void init() throws ServletException
        {
            System.out.println("---------------------------");
            System.out.println(" Init method is called in "
                    + this.getClass().getName());
            System.out.println("---------------------------");
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
            // Set response content type
            response.setContentType("text/html");
    
            // New location to be redirected
            String site = new String("http://www.google.com");
    
            response.setStatus(response.SC_MOVED_TEMPORARILY);
            response.setHeader("Location", site);
        }
    
        public void destroy()
        {
            System.out.println("---------------------------");
            System.out.println(" destroy method is called in "
                    + this.getClass().getName());
            System.out.println("---------------------------");
        }
    }
    
    
    
    web.xml
    <?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>PageRedirection</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>PageRedirect</servlet-name>
        <servlet-class>PageRedirectServlet</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>PageRedirect</servlet-name>
        <url-pattern>/PageRedirect</url-pattern>
      </servlet-mapping>
    
    </web-app>
    

    index.html
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>PageRedirectDemo</title>
    </head>
    <body>
        <li><a href="PageRedirect">PageRedirect</a></li>
    </body>
    </html>
    
    
    Environment Used 

    JDK version :1.6.0_30
    Tomcat version : 7.0.50 

    To Download PageAutoRefreshApp Project Click the below link

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

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Monday, 5 May 2014

    Servlets : Send Redirect Demo[Transfer control to different domain]


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

    Click the below Image to Enlarge



    SendRedirectDemo Project Dir Structure


    MySearchServlet.java
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class MySearchServlet extends HttpServlet
    {
    
        private static final long serialVersionUID = 1L;
    
        public void init() throws ServletException
        {
            System.out.println("----------------------------------");
            System.out.println(" Init method is called in "
                    + this.getClass().getName());
            System.out.println("----------------------------------");
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
            String searchKey = request.getParameter("searchKey");
            response.sendRedirect("https://www.google.co.in/#q=" + searchKey);
        }
    
        public void destroy()
        {
            System.out.println("----------------------------------");
            System.out.println(" destroy method is called in "
                    + this.getClass().getName());
            System.out.println("----------------------------------");
        }
    }
    

    web.xml
    <?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>SendRedirectDemo</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>search</servlet-name>
            <servlet-class>MySearchServlet</servlet-class>
        </servlet>
        
        <servlet-mapping>
            <servlet-name>search</servlet-name>
            <url-pattern>/search</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    index.html
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>SendRedirectDemo</title>
    </head>
    <body>
        <li><a href="mysearch.html">mysearch</a></li>
    </body>
    </html>
    
    
    mysearch.html
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>SendRedirectDemo</title>
    </head>
    <body>
        <form action="search">
            <input type="text" name="searchKey"> 
            <input type="submit" value="Google Search">
        </form>
    </body>
    </html>
    
    
    Environment Used 

    JDK version :1.6.0_30
    Tomcat version : 7.0.50 

    To Download SendRedirectDemo_Different_ContainerApp Project Click the below link

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

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Sunday, 4 May 2014

    Servlets : Send Redirect Demo[Transfer control to the Servlet which is in the Same Webcontainer]


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

    Click the below Image to Enlarge



    SendRedirectDemo Project Dir Structure

    LoginServlet.java
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class LoginServlet extends HttpServlet
    {
    
        private static final long serialVersionUID = 1L;
    
        public void init() throws ServletException
        {
            System.out.println("--------------------------------------");
            System.out.println(" Init method is called in "
                    + this.getClass().getName());
            System.out.println("--------------------------------------");
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
    
            String userName = request.getParameter("username");
            String passWord = request.getParameter("password");
    
            if (userName.equalsIgnoreCase("ram") && passWord.equals("ram"))
            {
                response.sendRedirect("http://localhost:8080/SendRedirectDemo/welcome");
            }
            else
            {
                out.print("Sorry UserName or Password is Wrong!");
                RequestDispatcher requestDispatcher = request
                        .getRequestDispatcher("/index.html");
                requestDispatcher.include(request, response);
            }
        }
    
        public void destroy()
        {
            System.out.println("----------------------------------------");
            System.out.println(" destroy method is called in "
                    + this.getClass().getName());
            System.out.println("-----------------------------------------");
        }
    }
    
    
    
    
    
    WelcomeServlet.java
    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;
    
    public class WelcomeServlet extends HttpServlet
    {
    
        private static final long serialVersionUID = 1L;
    
        public void init() throws ServletException
        {
            System.out.println("--------------------------------------");
            System.out.println(" Init method is called in "
                    + this.getClass().getName());
            System.out.println("--------------------------------------");
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
    
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
    
            out.print("Welcome");
        }
    
        public void destroy()
        {
            System.out.println("--------------------------------------");
            System.out.println(" destroy method is called in "
                    + this.getClass().getName());
            System.out.println("--------------------------------------");
        }
    }
    
    
    
    
    
    web.xml
    <?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>SendRedirectDemo</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>LoginServlet</servlet-name>
        <servlet-class>LoginServlet</servlet-class>
      </servlet>
      
      <servlet>
        <servlet-name>WelcomeServlet</servlet-name>
        <servlet-class>WelcomeServlet</servlet-class>
      </servlet>
     
      <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/login</url-pattern>
      </servlet-mapping>
      
      <servlet-mapping>
        <servlet-name>WelcomeServlet</servlet-name>
        <url-pattern>/welcome</url-pattern>
      </servlet-mapping>
      
    </web-app>
    
    
    
    index.html
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>RequestDispatcherDemo</title>
    </head>
    <body>
        <form action="login" method="post">
            
            UserName :  <input type="text" name="username" /><br /> 
            
            Password :&nbsp; &nbsp; <input type="password" name="password" /><br /> 
                
            <input type="submit" value="login" />
            
        </form>
    </body>
    </html>
    
    
    Environment Used 

    JDK version :1.6.0_30
    Tomcat version : 7.0.50 

    To Download SendRedirectDemoApp Project Click the below link

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

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Servlets : Display HTTP Request and Response headers


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

    Click the below Image to Enlarge

    DisplayAllHeaders Project Dir Structure

    DisplayHeadersServlet.java
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Enumeration;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class DisplayHeadersServlet extends HttpServlet
    {
    
        private static final long serialVersionUID = 1L;
    
        public void init() throws ServletException
        {
    
            System.out.println("\n*******************************************\n");
            System.out.println("HelloWorld servlet init method has been called");
            System.out.println("\n*******************************************\n");
    
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
    
            response.setContentType("text/html");
    
            PrintWriter out = response.getWriter();
            String title = "HTTP Header Request Example";
            String docType = "<!doctype html public \"-//w3c//dtd html 4.0 "
                    + "transitional//en\">\n";
            out.println(docType + "<html>\n" + "<head><title>" + title
                    + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n"
                    + "<h1 align=\"center\">" + title + "</h1>\n"
                    + "<table width=\"100%\" border=\"1\" align=\"center\">\n"
                    + "<tr bgcolor=\"#949494\">\n"
                    + "<th>Header Name</th><th>Header Value(s)</th>\n" + "</tr>\n");
    
            Enumeration<String> headerNames = request.getHeaderNames();
    
            while (headerNames.hasMoreElements())
            {
                String headerName = (String) headerNames.nextElement();
                out.print("<tr><td>" + headerName + "</td>\n");
                String headerValue = request.getHeader(headerName);
                out.println("<td> " + headerValue + "</td></tr>\n");
            }
            out.println("</table>\n</body></html>");
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
            doGet(request, response);
        }
    
        public void destroy()
        {
            // do nothing.
        }
    }
    

    web.xml
    <?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>ServletForm GET</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>readAllHeaders</servlet-name>
        <servlet-class>DisplayHeadersServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>readAllHeaders</servlet-name>
        <url-pattern>/readAllHeaders</url-pattern>
      </servlet-mapping>
    </web-app>
    

    index.html
    <!DOCTYPE HTML><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Order Pizza</title>
    </head>
    <body>
    <p>
    <h3>Order Pizza</H3>
    <p></p>
    <ul>
        <li><a href="pizza.html">Order Pizza</a></li>
    </ul>
    </body></html>
    
    
    pizza.html
    <html>
    <head>
    <title>Retrieving All Parameters</title>
     </head>
    <body>
    <h4>Order your Pizza Here</h4>
    <form action="readAllHeaders" method="POST">
    <b>Name</b> <input type="text" name="name"><br><br>
    <b>Select the Crust:</b>
    <select name = "crust">
    <option value="pan">Pan</option>
    <option value="thin">Thin Crust</option>
    <option value="deep">Deep Crust</option>
    <option value="cheese">Cheese Burst</option>
    </select> <br><br>
    <b>Toppings: </b><br>
    <input type="checkbox" name="toppings" value="peas">Peas<br>
    <input type="checkbox" name="toppings" value="paneer">Paneer<br>
    <input type="checkbox" name="toppings" value="redpeppers">Red Peppers<br>
    <input type="checkbox" name="toppings" value="pineapple">Pineapple<br>
    <input type="checkbox" name="toppings" value="onion">Onion<br>
    <input type="checkbox" name="toppings" value="tomato">Tomato<br><br>
     
    <b>Select 1 FREE Appetizer</b>
    <input type="radio" name="appetizer" value="Garlic Bread">Garlic Bread
    <input type="radio" name="appetizer" value="Cheese Garlic Bread">Cheese Garlic Bread
    <input type="radio" name="appetizer" value="Veg Soup">Veg Soup
    <input type="radio" name="appetizer" value="Veg Sandwich">Veg Sandwich<br><br>
     
    <b>Address</b><br>
    <textarea name="address" rows=3 cols=40></textarea><br><br>
    <b>Credit Card:</b><br>
    <input type="radio" name="cardType" value="Visa">Visa
    <input type="radio" name="cardType" value="MasterCard">MasterCard
    <input type="radio" name="cardType" value="Amex">American Express
    <br><br>
    <b>Credit Card Number:</b> <input type="password" name="cardNum">
    <b>Repeat Credit Card Number: </b><input type="password" name="cardNum"><br><br>
    <input type="submit" name="submit" value="Order Pizza">
    </form>
    </body>
    </html>
    

    Environment Used 

    JDK version :1.6.0_30
    Tomcat version : 7.0.50 

    To Download DisplayAllHeadersApp Project Click the below link

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

    See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • Google+ Followers

    Google+ Badge