Wednesday, 21 November 2012

J2ee Tutorial part 3:- Reading HttpServletRequest Headers and Cookies

// Reading HttpServletRequest Headers and Cookies example

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * This servlet exists to display all the parameters passed to it.
 */

public class RequestHeaders extends HttpServlet {

    protected void doGet(
        HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException {

        // Set appropriate content type for response, and obtain
        // the HTML output stream.

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        // Set up start of HTML page
        out.write("<html>\n<head>\n<title>Display Request Headers</title>\n</head>\n<body>");

        Enumeration headerNames = request.getHeaderNames();//getting all headers
        out.write("<h2>Request Headers</h2>");
        out.write("<table border=\"1\"><tr><th>Header Name</th><th>Header Values</th></tr>");
        while (headerNames.hasMoreElements()) {
            out.write("<tr>");
            String headerName = (String) headerNames.nextElement();
            out.write("<td>" + headerName + "</td>");
            Enumeration headerValues = request.getHeaders(headerName);//passing header nameto get values
            out.write("<td>");
            while (headerValues.hasMoreElements()) {
                String headerValue = (String) headerValues.nextElement();
                out.write(headerValue + ";");
            }
            out.write("</td></tr>");
        }
        out.write("</table>");
       
        // Obtain HttpSession so you are likely to have at least one cookie
        HttpSession session = request.getSession();
       
        out.write("<h2>Cookie Details</h2>");
        Cookie[] cookies = request.getCookies();//getting all cookies
        int length = (cookies == null) ? 0 : cookies.length;
        for (int i = 0; i < length; i++) {
            Cookie cookie = cookies[i];
            out.write("<table border=\"1\"><tr><th>Cookie Attribute</th><th>Attribute Value</th></tr>");
            out.write("<tr><td>Name</td><td>" + cookie.getName() + "</td></tr>" );
            out.write("<tr><td>Value</td><td>" + cookie.getValue() + "</td></tr>" );
            out.write("<tr><td>Domain</td><td>" + cookie.getDomain() + "</td></tr>" );
            out.write("<tr><td>Path</td><td>" + cookie.getPath() + "</td></tr>" );
            out.write("<tr><td>Comment</td><td>" + cookie.getComment() + "</td></tr>" );
            out.write("<tr><td>MaxAge</td><td>" + cookie.getMaxAge() + "</td></tr>" );
            out.write("<tr><td>Version</td><td>" + cookie.getVersion() + "</td></tr>" );
            out.write("<tr><td>Secure</td><td>" + cookie.getSecure() + "</td></tr></table>" );
           }
       
        // Finish off the HTML page and close cleanly
        out.write("\n</body>\n</html>");
        out.close();

    }


}


Output ;-

No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...