Testing a request parameter in a JSP


In JSP’s you need to be careful with request params since they can cause errors. This example shows how to prevent nullpointer exceptions:

<%@ page Page="page.jsp" %>

    if (request.getParameter("param").equals("value")) {
        // ...

    // The test above will throw a
    // NullPointerException if param is
    // not part of the query string. A better
    // implementation would be:
  ("value".equals(request.getParameter("param"))) {
        // ...


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.