Friday, 21 June 2013

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

WTAD Practical - 11
Write a Servlet to display all the headers available from request.
WEB-INF/web.xml


<web-app>
<servlet>
<servlet-name>s11</servlet-name>
<servlet-class>RequestHeaders</servlet-class>
</servlet>
<!-- Servlet Mapping -->
<servlet-mapping>
<servlet-name>s11</servlet-name>
<url-pattern>/requestheaders</url-pattern>
</servlet-mapping>
</web-app>


WEB-INF/classes/RequestHeaders.java


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class RequestHeaders extends HttpServlet
{
public void doGet( HttpServletRequest request,HttpServletResponse response ) throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<HTML>" + 
"<HEAD><TITLE>WTAD.practical-11 @iAmLearningHere</TITLE></HEAD>" +
"<body bgcolor=YellowGreen>");

out.println("Request method:" + request.getMethod() + "<br><br>");
out.println("Request URI:" + request.getRequestURI() + "<br><br>");
out.println("Request protocol:" + request.getProtocol() + "<br><br>");
out.println("<table border=1 align=left><tr><th>Header Name</th><th>Header Value</th></tr>");
Enumeration headerNames=request.getHeaderNames();
while(headerNames.hasMoreElements())
{
String headerName=(String)headerNames.nextElement();
out.println("<tr><td>"+headerName + "</td>");
out.println("<td>"+request.getHeader(headerName) + "</td></tr>");
}
out.println("</table></body></HTML>");

}
}

Preview


How to Run?
> Start Apache Server
> Start localhost and open program folder
> type url '/requestheaders'

For all WTAD programs wtad-pra cticals.html
To get all programs in your inbox.Enter your email

Kindly Bookmark and Share it:

1 comments :

Unknown on Wednesday, September 05, 2018 11:42:00 am said...

Thanks for sharing good blog with servlet to display the all headers available in the request,it is good example for android app development
MOBILE APPLICATION DEVELOPMENT

Post a Comment

Any Query ? any suggestion ? comment here

 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT