Wednesday 23 April 2014

How to print Request Header Information using Servlet

First of all,

STEP 1:   Create folder in webapps which is exising in Apache Tomcat folder
           
                Folder name as RequestHeaders

STEP 2:   Now in Hello folder you have to create index.html file and code as Below


                 <html>
                 <head>
                 <title>Wel come</title>
                 </head>

               <body >
                              <a href="RequestHeaders">Click here</a>


               </body>
                </html>
       
             I hope you have created it.

STEP 3: After that create new folder in RequestHeaders foder which name is WEB-INF

STEP 4: Now create one file which name is web.xml and make sure it's extenstion is .xml and coded
              with below code
        
             <web-app>
                 <servlet>
                               <servlet-name>RequestHeaders</servlet-name>
                               <servlet-class>RequestHeaders</servlet-class>
                </servlet>
          
                <servlet-mapping>
                              <servlet-name>RequestHeaders</servlet-name>
                              <url-pattern>/RequestHeaders</url-pattern>
               </servlet-mapping>

             </web-app>

            this file used to mapping between servlet to your servlet file
  
STEP 5: Now create one folder in WEB-INF foder which name is classes

STEP 6: In classes folder create your servlet file which name is Hello.java
              complie it
              as per below code
           
              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();
               String title = "Servlet Example: Showing Request Headers";
               String docType =
                        "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
                            "Transitional//EN\">\n";
                out.println(docType +
                "<HTML>\n" +
                "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
                "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
                "<B>Request Method: </B>" +
                request.getMethod() + "<BR>\n" +
                "<B>Request URI: </B>" +
                request.getRequestURI() + "<BR>\n" +
                "<B>Request Protocol: </B>" +
                request.getProtocol() + "<BR><BR>\n" +
                "<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
                "<TR BGCOLOR=\"#FFAD00\">\n" +
                "<TH>Header Name<TH>Header Value");
                  Enumeration headerNames = request.getHeaderNames();

                    while(headerNames.hasMoreElements()) {
                      String headerName = (String)headerNames.nextElement();
                      out.println("<TR><TD>" + headerName);
                  out.println("    <TD>" + request.getHeader(headerName));
            }
                    out.println("</TABLE>\n</BODY></HTML>");
         }



                 public void doPost(HttpServletRequest request,
                                                     HttpServletResponse response)
                                                     throws ServletException, IOException {
                                                  doGet(request, response);
                                                  }
                               }


STEP 7:After complation of your programm you have to open Apche server in
              browser
    
             like ,
                    http://localhost:8080/

            click on manager apps
             and then clcik on list of Application
            After that you found your example name as RequestHeaders,
           Click on RequestHeaders link

   

Enjoy........  

Download Example

Downlod files and Extract in webapps folder

No comments:

Post a Comment

How to change Background color by selecting color from Listview

activitymain.xml file::> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xm...