How to print HttpServletRequest’s All Available Request Parameters and Request Attributes

If you ever debugged your J2EE application, you know the pain of finding out where you request parameters are on the HttpServletRequest object. I always get lost when I am inspecting the request object because it holds so much onto it. This example here shows you how to print all available request parameters and request attributes so you can easily look at what values those attribtues and parameters are holding.

If you run this servlet, you should be able to print these parameters and attributes on to you browser itself.

package com.kushal.tools;
/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * www.icodejava.com
 * 
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.ws.http.HttpConnection;
import com.ibm.ws.http.HttpRequest;

public class HttpRequestParamsAndAttributesPrintServlet extends HttpRequest{

    public HttpRequestParamsAndAttributesPrintServlet(HttpConnection connection) {

        super(connection);

    }

    public void doGet(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException {

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

        printRequestAttributes(req, out);

        printRequestParameters(req, out);

    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException {
        
        doGet(req, res);
    }

    /**
     * This method prints all the request parameters that are 
     * available on the HttpServletRequest.
     * 
     * The result will be printed back to the browser
     *
     */
    private void printRequestParameters(HttpServletRequest req, PrintWriter out) {

        out.println("Printing All Request Parameters From HttpSerlvetRequest:");

        Enumeration<String> requestParameters = req.getParameterNames();
        while (requestParameters.hasMoreElements()) {
            String paramName = (String) requestParameters.nextElement();
            out.println("Request Paramter Name: " + paramName 
                            + ", Value - " + req.getParameter(paramName));
        }
    }

    /**
     * This method prints all the request attributes that are 
     * available on the HttpServletRequest.
     * 
     * The result will be printed back to the browser.
     *
     */
    private void printRequestAttributes(HttpServletRequest req, PrintWriter out) {

        out.println("Printing All Request Parameters From HttpSerlvetRequest:");

        Enumeration<String> requestAttributes = req.getAttributeNames();
        while (requestAttributes.hasMoreElements()) {
            String attributeName = (String) requestAttributes.nextElement();
            out.println("Request Attribute Name: " + attributeName 
                            + ", Value - " + (req.getAttribute(attributeName)).toString());
        }
    }
}

Collection of MIME Types to use for Microsoft Office 2010 File Format Documents

The following are the mime types to be used for MS Office 2010 document file formats.

.docm: application/vnd.ms-word.document.macroEnabled.12
.docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotm: application/vnd.ms-word.template.macroEnabled.12
.dotx: application/vnd.openxmlformats-officedocument.wordprocessingml.template
.potm: application/vnd.ms-powerpoint.template.macroEnabled.12
.potx: application/vnd.openxmlformats-officedocument.presentationml.template
.ppam: application/vnd.ms-powerpoint.addin.macroEnabled.12
.ppsm: application/vnd.ms-powerpoint.slideshow.macroEnabled.12
.ppsx: application/vnd.openxmlformats-officedocument.presentationml.slideshow
.pptm: application/vnd.ms-powerpoint.presentation.macroEnabled.12
.pptm: application/vnd.ms-powerpoint.presentation.macroEnabled.12
.pptx: application/vnd.openxmlformats-officedocument.presentationml.presentation
.xlam: application/vnd.ms-excel.addin.macroEnabled.12
.xlsb: application/vnd.ms-excel.sheet.binary.macroEnabled.12
.xlsb: application/vnd.ms-excel.sheet.binary.macroEnabled.12
.xlsm: application/vnd.ms-excel.sheet.macroEnabled.12
.xlsx: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltm: application/vnd.ms-excel.template.macroEnabled.12
.xltx: application/vnd.openxmlformats-officedocument.spreadsheetml
.xps:  application/vnd.ms-xpsdocument

SQL – Finding Nth Highest Row In DB Table

Generally I come across situations where I have to find nth highest row in a SQL table. The following piece of SQL code is very handy to me to deal with such situations. I thought of posting it to my blog thinking that it might help you either. Please replace N with proper level of highestness required, e.g. 1 (1st highest), 2 (2nd highest) and MYTABLE with your table, and MYCOLUMN with the column that you are trying to find the nth highest value from.

SELECT * FROM MYTABLE T1
WHERE (N =
(SELECT COUNT(DISTINCT (T2.MYCOLUMN))
FROM MYTABLE T2
WHERE T2.MYCOLUMN > T1.MYCOLUMN))

I have tested this code and it runs perfect.