Presents your JAVA E-NEWSLETTER for June 24, 2002 ------------------------------------------- LEARN HOW TO MANAGE SERVLET VARIABLES If managing names within session and request parameters/attributes causes obscure bugs and is hard to document, use two interfaces to hold the names. The Servlet application program interface (API) provides map style methods that store values in a session or obtain values from a request. A string indexes these values, and it's common to use the particular string frequently within the code, such as: String query = request.getParameter("query"); Person person = (Person)session.getAttribute("current_person"); This type of inquiry is problematic for two reasons. The first is that it's not typed, meaning that incorrect name entries or misspellings are not noticed until the Web page begins to behave oddly. Nothing points out that the following code is missing an 'e': String query = request.getParameter("qury"); or that this code contains invalid capitalization: String query = request.getParameter("Query"); The second problem is that there's no easy application documentation of the request and session variables. Both of these problems can be solved by the methodical usage of interfaces to hold the variable names, as well as comments concerning their use. For example: package com.generationjava.application.jobspage; public interface RequestVars { // used to get the query field from a search box static public String QUERY = "query"; } and package com.generationjava.application.jobspage; public interface SessionVars { // the currently logged in person static public String CURRENT_PERSON = "current_person"; } Here's the usage of these typed and documented variables: String query = request.getParameter(RequestVars.QUERY); Person person = (Person)session.getAttribute(SessionVars.CURRENT_PERSON); By adhering to this coding convention, order is brought to the chaotic world of request and session variable names. ----------------------------------------