WTAD Practical - 11
Write a Servlet to display all the headers available from request.
WEB-INF/web.xml
WEB-INF/classes/RequestHeaders.java
Preview
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>
<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>");
}
}
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>");
}
}
How to Run?
> Start Apache Server
> Start localhost and open program folder
> type url '/requestheaders'
> Start Apache Server
> Start localhost and open program folder
> type url '/requestheaders'
1 comments :
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