Saturday 3 May 2014

Servlets : load-on-startup - Example


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

Click the below Image to Enlarge

LoadOnStartup Project Dir Structure


HelloWorld.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;

// Extend HttpServlet class
public class HelloWorld extends HttpServlet
{

 private static final long serialVersionUID = 1L;
 private String message;

 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");

  message = "Hello World";
 }

 public void service(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
 {
  System.out
          .println("\n**********************************************\n");
  System.out
          .println("HelloWorld servlet service method has been called . ");
  System.out
          .println("\n*********************************************\n");

  doGet(request, response);
 }

 public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
 {
  // Set response content type
  response.setContentType("text/html");

  // Actual logic goes here.
  PrintWriter out = response.getWriter();
  out.println("<h1>" + message + "</h1>");
 }

 public void destroy()
 {
  // do nothing.
 }
}
ServletsLifeCycle.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;

// 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("\n**********************************************\n");
  System.out
          .println("ServletsLifeCycle servlet init method "
            + "has been called ..... ");
  System.out
          .println("\n********************************************\n");
 }

 public void service(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
 {
  ++i;

  System.out
          .println("\n*******************************************\n");
  System.out
          .println("ServletsLifeCycle servlet service method "
            + "has been called ..... ");
  System.out
          .println("\n***************************************\n");

  // 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("------------------------------------------------------");
 }
}
CustomerInfoServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CustomerInfoServlet extends HttpServlet
{

 private static final long serialVersionUID = 1L;

 public void init() throws ServletException
 {

  System.out
          .println("\n******************************************\n");
  System.out
          .println(" CustomerInfoServlet servlet init method "
            + "has been called ..... ");
  System.out
          .println("\n*********************************************\n");

 }

 public void service(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
 {
  System.out
          .println("\n******************************************\n");
  System.out
          .println("CustomerInfoServlet servlet service method "
            + "has been called ..... ");
  System.out
          .println("\n*************************************************\n");

  doGet(request, response);
 }

 public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
 {

  response.setContentType("text/html");

  PrintWriter out = response.getWriter();

  out.println("Read data using  '<b>String getParameter(String name)</b>' "
    + "method");

  String docType = "<!doctype html public \"-//w3c//dtd html 4.0 "
          + "transitional//en\">\n";
  out.println(docType + "<html>\n" + "<head><title>" + "GET"
          + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n"
          + "<ul>\n" + "  <li><b>First Name</b>: "
          + request.getParameter("first_name") + "\n"
          + "  <li><b>Last Name</b>: "
          + request.getParameter("last_name") + "\n" + "</ul>\n"
          + "</body></html>");

  out.println("\nRead data using '<b>Enumeration getParameterNames()</b>' "
    + "method \n");

  Enumeration<String> paramNames = request.getParameterNames();

  out.println("<ul>");

  while (paramNames.hasMoreElements())
  {

   String paramName = paramNames.nextElement();
   out.print(" <li><b>" + paramName + ":</b>");
   String paramValue = request.getParameter(paramName);
   out.println(paramValue + "</li>");

  }

  out.println("</ul>");

  out.println("\nRead data using '<b>Map getParameterMap()</b>' method\n");

  Map<String, String[]> paramMap = request.getParameterMap();

  Set<String> paramNamesSet = paramMap.keySet();

  out.println("<ul>");
  for (String paramName : paramNamesSet)
  {
   out.print(" <li><b>" + paramName + ":</b>");
   String[] paramValue = paramMap.get(paramName);
   for (int i = 0; i < paramValue.length; i++)
   {
    out.println(paramValue[i] + "</li>");
   }

  }
  out.println("</ul>");

 }

 public void destroy()
 {
  // do nothing.
 }

}
web.xml
<?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>Load on Startup</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>HelloWorld</servlet-name>
  <servlet-class>HelloWorld</servlet-class>
  <load-on-startup>0</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>HelloWorld</servlet-name>
  <url-pattern>/hello</url-pattern>
 </servlet-mapping>


 <servlet>
  <servlet-name>ServletsLifeCycle</servlet-name>
  <servlet-class>ServletsLifeCycle</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>ServletsLifeCycle</servlet-name>
  <url-pattern>/ServletsLifeCycle</url-pattern>
 </servlet-mapping>

 <servlet>
  <servlet-name>CustomerInfoForm</servlet-name>
  <servlet-class>CustomerInfoServlet</servlet-class>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>CustomerInfoForm</servlet-name>
  <url-pattern>/CustomerInfoForm</url-pattern>
 </servlet-mapping>


</web-app>
index.html
<!DOCTYPE HTML><html lang="en"><head>
<meta charset="UTF-8">
<title>Load on Startup</title>
</head>
<body>
<p>
<h3>Load on Startup</H3>
<p></p>
<ul>

<li><a href="hello">HelloWorld</a></li>
<li><a href="ServletsLifeCycle">ServletsLifeCycle Test</a></li>

<li><a href="customerform.html">Customer Form</a></li>

</ul>
</body></html>
customerform.html
<html>
<body>
 <form action="CustomerInfoForm" method="GET">
 
 First Name: <input type="text" name="first_name">
 <br />
 
 Last Name: <input type="text" name="last_name" />
 
 <input type="submit" value="Submit" />
 
 </form>
</body>
</html>
Environment Used 

JDK version :1.6.0_30
Tomcat version : 7.0.50 

To Download LoadOnStartupApp Project Click the below link

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

  See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • 1 comment:

    1. Nice Article. Thank you for sharing the informative article with us. biznesshub.

      ReplyDelete