Tag Archives: Nullpointer exception

Testing a request parameter in a JSP

Introduction

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:
    if    
  ("value".equals(request.getParameter("param"))) {
        // ...
    }
%>