Tuesday, February 6, 2018

Servlet Pratical LAB

-------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

Servlet LAB

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

Ø  Eclipse IDE (2020) JEE

Ø  Copy : C:\xampp\tomcat\lib\servlet-api.jar

Ø  Copy : C:\xampp\tomcat\lib\jsp-api.jar

 

Ø  Paste:  lib\ext\servlet-api.jar

Ø  Paste:  lib\ext\jsp-api.jar

 

Creating servlet example in eclipse ide, saves a lot of work to be done. It is easy and simple to create a servlet example. Let's see the steps, you need to follow to create the first servlet example.

  • Create a Dynamic web project
  • create a servlet
  • add servlet-api.jar file
  • Run the servlet

1) Create the dynamic web project:

For creating a dynamic web project click on File Menu -> New -> Project..-> Web -> dynamic web project -> write your project name e.g. first -> Finish.

2) Configure servlet-api.jar & jsp-api.jar Jar file in Eclipse IDE

Ø  Right Click on Project (firstServlet)-> Build Path -> Configure Build Path

      

 

 

 

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

LAB -1 .  Reading Servlet Parameters

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

Servlet Program to demonstrate Reading Servlet Parameters

//  WebContent/PostParametersServlet.jsp

<html>

<head>

<meta charset="ISO-8859-1">

</head>

<body>

       <form name="Form1" method="post"

              action="http://localhost:8080/firstServlet/PostParametersServlet">

              <table>

                     <tr>

                           <td><B>Employee</td>

                           <td><input type=textbox name="e" size="25" value=""></td>

                     </tr>

                     <tr>

                           <td><B>Phone</td>

                           <td><input type=textbox name="p" size="25" value=""></td>

                     </tr>

              </table>

              <input type=submit value="Submit">

       </form>

</body>

</html>

// src/PostParametersServlet.java

import java.io.*;

import java.util.*;

import javax.servlet.*;

 

public class PostParametersServlet extends GenericServlet {

       public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

// Get print writer.

              PrintWriter pw = response.getWriter();

// Get enumeration of parameter names.

              Enumeration e = request.getParameterNames();

// Display parameter names and values.

              while (e.hasMoreElements()) {

                     String pname = (String) e.nextElement();

                     pw.print(pname + " = ");

                     String pvalue = request.getParameter(pname);

                     pw.println(pvalue);

              }

              pw.close();

       }

}

 

// WEB-INF/web.xml

<servlet>

   <servlet-name>PostParametersServlet</servlet-name>

   <servlet-class>PostParametersServlet</servlet-class>

</servlet>

 

<servlet-mapping>

   <servlet-name>PostParametersServlet</servlet-name>

   <url-pattern>/PostParametersServlet</url-pattern>

</servlet-mapping>

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

LAB -2 .  Handling HTTP GET Requests

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

Servlet Program to demonstrate Handling HTTP GET Requests

//  WebContent/ColorGet.jsp

<html>

<body>

<center>

<form name="Form1" 

  action="http://localhost:8080/firstServlet/ColorGetServlet">

<B>Color:</B>

<select name="color" size="1">

<option value="Red">Red</option>

<option value="Green">Green</option>

<option value="Blue">Blue</option>

</select>

<br><br>

<input type=submit value="Submit">

</form>

</body>

</html>

// src/ ColorGetServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class ColorGetServlet extends HttpServlet {

 

  public void doGet(HttpServletRequest request, 

    HttpServletResponse response) 

  throws ServletException, IOException {

 

    String color = request.getParameter("color");

    response.setContentType("text/html");

    PrintWriter pw = response.getWriter();

    pw.println("<B>The selected color is:  ");

    pw.println(color);

    pw.close();

  }

}

// WEB-INF/web.xml

<servlet>

   <servlet-name>ColorGetServlet</servlet-name>

   <servlet-class>ColorGetServlet</servlet-class>

</servlet>

 

<servlet-mapping>

   <servlet-name>ColorGetServlet</servlet-name>

   <url-pattern>/ColorGetServlet</url-pattern>

</servlet-mapping>

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

LAB -3 .  Handling HTTP POST Requests

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

Servlet Program to demonstrate Handling HTTP POST Requests

// WEB-INF/web.xml

<servlet>

   <servlet-name>ColorPostServlet</servlet-name>

   <servlet-class>ColorPostServlet</servlet-class>

</servlet>

 

<servlet-mapping>

   <servlet-name>ColorPostServlet</servlet-name>

   <url-pattern>/ColorPostServlet</url-pattern>

</servlet-mapping>

 

 

 

 

// src/ColorPostServlet.java

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class ColorPostServlet extends HttpServlet {

 

  public void doPost(HttpServletRequest request, 

    HttpServletResponse response) 

  throws ServletException, IOException {

 

    String color = request.getParameter("color");

    response.setContentType("text/html");

    PrintWriter pw = response.getWriter();

    pw.println("<B>The selected color is:  ");

    pw.println(color);

    pw.close();

  }

}

 

//  WebContent/ColorPost.jsp

<html>

<body>

<center>

<form name="Form1" 

  method="post"

  action="http://localhost:8080/firstServlet/ColorPostServlet">

<B>Color:</B>

<select name="color" size="1">

<option value="Red">Red</option>

<option value="Green">Green</option>

<option value="Blue">Blue</option>

</select>

<br><br>

<input type=submit value="Submit">

</form>

</body>

</html>

 

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

LAB -4 .  Using ADD Cookies

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

Servlet Program to demonstrate AddCookie

//  WebContent/AddCookie.html

<html>

<body>

<center>

<form name="Form1" 

  method="post"

  action="http://localhost:8080/firstServlet/AddCookieServlet">

<B>Enter a value for MyCookie:</B>

<input type=textbox name="data" size=25 value="">

<input type=submit value="Submit">

</form>

</body>

</html>

// src/AddCookieServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 public class AddCookieServlet extends HttpServlet {

 

  public void doPost(HttpServletRequest request, 

    HttpServletResponse response) 

  throws ServletException, IOException {

 

    // Get parameter from HTTP request.

    String data = request.getParameter("data");

 

    // Create cookie.

    Cookie cookie = new Cookie("MyCookie", data);

     // Add cookie to HTTP response.

    response.addCookie(cookie);

 

    // Write output to browser.

    response.setContentType("text/html");

    PrintWriter pw = response.getWriter();

    pw.println("<B>MyCookie has been set to");

    pw.println(data);

    pw.close();

  }

}

// WEB-INF/web.xml

<servlet>

   <servlet-name>AddCookieServlet</servlet-name>

   <servlet-class>AddCookieServlet</servlet-class>

</servlet>

 

<servlet-mapping>

   <servlet-name>AddCookieServlet</servlet-name>

   <url-pattern>/AddCookieServlet</url-pattern>

</servlet-mapping>

 

 

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

LAB - 5 .  Using GET Cookies

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

Servlet Program to demonstrate GETCookie

// WEB-INF/web.xml

 

<servlet>

   <servlet-name>GetCookiesServlet</servlet-name>

   <servlet-class>GetCookiesServlet</servlet-class>

</servlet>

 

<servlet-mapping>

   <servlet-name>GetCookiesServlet</servlet-name>

   <url-pattern>/GetCookiesServlet</url-pattern>

</servlet-mapping>

// src/GetCookiesServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class GetCookiesServlet extends HttpServlet {

 

  public void doGet(HttpServletRequest request, 

    HttpServletResponse response) 

  throws ServletException, IOException {

 

    // Get cookies from header of HTTP request.

    Cookie[] cookies = request.getCookies();

 

    // Display these cookies.

    response.setContentType("text/html");

    PrintWriter pw = response.getWriter();

    pw.println("<B>");

    for(int i = 0; i < cookies.length; i++) {

      String name = cookies[i].getName();

      String value = cookies[i].getValue();

      pw.println("name = " + name + 

        "; value = " + value);

    }

    pw.close();

  }

}

Run in URL:

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

LAB - 6 .  Using Session Tracking

---------------- ----------------------------- --------------------- ------------------------------ ---------------------- -------------

The following servlet illustrates how to use session state:

 

That object is a Date object that encapsulates the date and time when this page was last accessed. A Date object encapsulating the current date and time is then created. The setAttribute( ) method is called to bind the name "date" to this object.

 

// src/DateServlet.java

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class DateServlet extends HttpServlet {

 

  public void doGet(HttpServletRequest request, 

    HttpServletResponse response) 

  throws ServletException, IOException {

 

    // Get the HttpSession object.

    HttpSession hs = request.getSession(true);

 

   

// Get writer.

    response.setContentType("text/html");

    PrintWriter pw = response.getWriter();

    pw.print("<B>");

 

    // Display date/time of last access.

    Date date = (Date)hs.getAttribute("date");

    if(date != null) {

      pw.print("Last access: " + date + "<br>");

    }

 

    // Display current date/time.

    date = new Date();

    hs.setAttribute("date", date);

    pw.println("Current date: " + date);

  }

}

Run in browser:

http://localhost:8080/firstServlet/DateServlet

CORBA Java Tutorial using Netbeans and Java 8.

CORBA-Example A simple CORBA implementation using Java Echo.idl module EchoApp{ interface Echo{ string echoString(); }; }; ...