Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Tuesday, 14 February 2012

Write a Servlet to display all the headers available from request.


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class pro11 extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
  res.setContentType("text/html");
  PrintWriter out=res.getWriter();
  out.printf("<html><head><title>2ND PROGRAM-pankaj</title></head><body bgcolor='limegreen'>");
  out.println("<table border=1 align='center'><tr><th>FIELD<th>VALUE");
  out.printf("<tr><td>METHOD");
  out.printf("<td>"+req.getMethod());
  out.printf("<tr><td>Request URL");
  out.printf("<td>"+req.getRequestURL());
  out.printf("<tr><td>Protcol");
  out.printf("<td>"+req.getProtocol());
  out.printf("<tr><th colspan='2' align='center'>HEADER NAMES");
  Enumeration name=req.getHeaderNames();
  
  while(name.hasMoreElements())
  {
   String hname = (String)name.nextElement();
   String hvalue= req.getHeader(hname);
   out.println("<tr><td>"+hname);
   out.println("<td>"+hvalue);
  }
  out.printf("</table>");
  out.printf("</body>");
 }
}

1 comment: