Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Java Servlet :Obtaining HTTP request Headers
Previous Home Next

The following example demonstrates how you can use the HttpServletRequest interface to obtain all the header names and sends the header name/value pairs to the browser.

index.html

<html>
	<head>
	 <title>Application of post and get method</title>
	</head>
	<body>
	 <form method="get" action="post">
	 <input type="submit" value="click">
	 </form>
	</body>
<html>

web.xml

<web-app>
	<servlet>
	 <servlet-name>s1</servlet-name>
	 <servlet-class>RegisterServlet</servlet-class>
	</servlet>
	<servlet-mapping>
	 <servlet-name>s1</servlet-name>
	 <url-pattern>post</url-pattern>
	</servlet-mapping>
</web-app>

RegisterServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class RegisterServlet extends HttpServlet
 {
  public void doGet(HttpServletRequest req,
   HttpServletResponse res)throws ServletException,IOException
 {
  res.setContentType("text/html");
  PrintWriter out=res.getWriter(); 
  Enumeration enumeration=req.getHeaderNames();
  while(enumeration.hasMoreElements())
  {
   String header=(String)enumeration.nextElement();
   out.println(header+":"+req.getHeader(header)+"<br>");
  }
 }
}
Previous Home Next