Showing posts with label elements and values of request object. Show all posts
Showing posts with label elements and values of request object. Show all posts

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

     }

}