Showing posts with label servlet. Show all posts
Showing posts with label servlet. Show all posts

Form data and Servlets - Factorial of a number


Simple program to demonstrate accessing form data in servlets. Factorial of a number

 

// Form to submit number to server

 

<!-- html forms -->

 

<html>

 

<head>

     <title>HTML Forms and Servlets</title>

     <style>

          label

          {

              font-family:vivaldi;

              font-size:x-large;

              letter-spacing:2px;

              line-height:40px;

          }

     </style>

</head>

 

<body>

     <form method="GET" action="Fact">

          <center>

          <h2>HTML and Servlets</h2></br>

         

          <label>Enter Number  </label>

          <input type="text" Name="Num" tabindex=1/>

          </br> </br>

         

          <button class="btn" type="button " >Calculate!!! </button>

          </center>

             

     </form>

 

</body>

</html>

 

// Servlet to access form data and generate response to client browser

 

import javax.servlet.http.*; 

import javax.servlet.*; 

import java.io.*;

import java.util.*; 

 

public class Fact extends HttpServlet

{ 

     public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException, IOException 

     { 

          //setting the content type

          res.setContentType("text/html");

 

          //create the stream to write the data 

          PrintWriter pw=res.getWriter();

           

          int N = Integer.parseInt(req.getParameter("Num"));

          int f = 1;

         

          //writing html in the stream 

          pw.println("<html><body>"); 

                   

          for(int i=1;i<=N;i++)

              f = f*i;

             

          pw.println("<H2>Factorial of "+N+" is "+ f +"</H2>");       

         

          pw.println("</body></html>"); 

         

          //closing the stream

          pw.close(); 

     }

} 

Servlet and request object


Simple program to display elements and values in request object

 

import javax.servlet.http.*; 

import javax.servlet.*; 

import java.io.*;

import java.util.*; 

 

public class Req_parameters extends HttpServlet

{ 

     public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException, IOException 

     { 

          //setting the content type

          res.setContentType("text/html");

 

          //create the stream to write the data 

          PrintWriter pw=res.getWriter();

           

          String addr = req.getRemoteAddr();

          String host = req.getRemoteHost();

          String pl = req.getProtocol();

          boolean sec = req.isSecure(); 

          StringBuffer req_url = req.getRequestURL();

         

          //writing html in the stream 

          pw.println("<html><body>"); 

          pw.println("<h2>Client Address = "+addr+"</br>"); 

          pw.println("Client Name = "+host+"</br>"); 

          pw.println("Protocol = "+pl+"</br>");

          pw.println("Security = "+sec+"</br>");

          pw.println("Request URL = "+req_url+"</br>");

         

          pw.println("</br>");

          pw.println("Header Fields: </br></h2>");

         

          pw.println("<h3>");

         

          String HdrName, HdrVal;

         

          Enumeration Hdr = req.getHeaderNames();

         

          if(Hdr==null)

              pw.println("Unable to access Header...</br>");

          else

          {

              while(Hdr.hasMoreElements())

              {

                   HdrName = (String)Hdr.nextElement();

                   HdrVal = req.getHeader(HdrName);

                   pw.println(HdrName + " : " + HdrVal + "</br>");

              }

          }

         

         

          pw.println("</h3>"); 

          pw.println("</body></html>"); 

         

          //closing the stream

          pw.close(); 

     }

} 

 

Form data and servlets


Simple program to demonstrate accessing a form data in a servlet

 

// Form to submit user name

 

<!-- html forms -->

 

<html>

 

<head>

     <title>HTML Forms and Servlets</title>

     <style>

          label

          {

              font-family:vivaldi;

              font-size:x-large;

              letter-spacing:2px;

              line-height:40px;

          }

     </style>

</head>

 

<body>

     <form method="get" action="http://localhost:8088/anu/formdata">

 

          <center>

          <h2>HTML and Servlets</h2></br>

         

          <label>First Name  </label>

          <input type="text" Name="UsrName" tabindex=1/>

          </br> </br>

         

          <button class="btn" type="button " >Click me!!! </button>

          </center>

             

     </form>

</body>

</html>

 

 

// Servlet to access form data and display a welcome message

 

import javax.servlet.http.*; 

import javax.servlet.*; 

import java.io.*;

import java.util.*; 

 

public class formdata extends HttpServlet

{ 

     public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException, IOException 

     { 

          //setting the content type

          res.setContentType("text/html");

 

          //create the stream to write the data 

          PrintWriter pw=res.getWriter();

            

          String N = (String)req.getParameter("UsrName");

         

          //writing html in the stream 

          pw.println("<html><body>"); 

         

         

          if(N.length()==0)   

              pw.println("<H2>Welcome Guest.</H2>");

          else

              pw.println("<H2>Welcome "+N+".</H2>");

         

          pw.println("</body></html>"); 

         

          //closing the stream

          pw.close(); 

     }

} 

 

 

   

 

 

 

Hit Counter


Simple program to demonstrate accessing a servlet in Apache Tomcat Server

Hit counter program

 

import javax.servlet.http.*; 

import javax.servlet.*; 

import java.io.*; 

 

public class HitCounter extends HttpServlet

{ 

 

     private int counter = 0;

     public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException, IOException 

     { 

          //setting the content type

          res.setContentType("text/html");

 

          //create the stream to write the data 

          PrintWriter pw=res.getWriter();

            

          counter++;

         

          //writing html in the stream 

          pw.println("<html><body>"); 

          pw.println("<h2>Hit Count = "+counter+"</h2>"); 

          pw.println("</body></html>"); 

         

          //closing the stream

          pw.close(); 

     }

}