I am developing a web application using Java servlets, and I would like to know what the software development community thinks of the following programming practices:

  1. Use static member variables for the names of input parameters. I created a separate class that contains static member variables to store the names of input parameters. In other words, I am using the following code:

    String username = (String)request.getParameter(ServletConstants.USERNAME_FIELD);

    instead of the traditional method:

    String username = (String)request.getParameter("username");
  2. Use static member variables for trace statements. I am simplifying log statements in my program by using the following:

    System.out.println(ServletConstants.USERNAME_FIELD + " is null.");

    instead of the traditional method:

    System.out.println("username is null.");

The reason why I am using static member variables is to prevent typos in my code. If I can prevent myself from misspelling the word "username", then the debugging process becomes less difficult. This also allows me to re-use my code more efficiently. For example, if I have to write another method to validate the "fullName" field, then I can just replace ServletConstants.USERNAME_FIELD with ServletConstants.FULLNAME_FIELD.

1 Answer 1


Using constants for parameters is generally considered a best practice. As you stated, as long as you get the value correct in the constant you get type checking from the compiler each time you reference the constant.

Note: When you declare the constant you should use public static final i.e.:

public static final String USERNAME_FIELD = "username";

An alternative may be to use an enum - something like:

public enum UserFields {

Which you can access as:


That has the advantage of only typing the parameter name once, but it can have some disadvantages, for example if you want the parameter to be lower case and/or you have a use case where you need a true constant rather than a technically "calculated" value as the .name() method does.

With respect to the logging part of your question, its good that you are investigating the low level Servlet API (you will understand whats really happening under the hood), however in the long run you will end up writing a bunch of boiler plate that you really don't need to write.

I would suggest you look into Java frameworks (two popular ones being: Spring Boot and DropWizard).

Both of these have validation/mapping support built into the frameworks so they will map incoming (paths, parameters and message bodies) to Java objects for you.

They both also have logging frameworks, so all you need to do is put the correct annotation of a field and it will generate the validation failure message for you, in your logs.

Based on the question you have asked, I suspect you are fairly new to Java development - so you may be better off asking specific technical questions on Stack Overflow, however I have given you the "Software Engineering" answer here:

TL;DR: - Except in very specific cases, don't use the raw Servlet API - let a framework take the strain for you. **

** - This advice assume you already know what the servlet API does and how much of a pain it is, to hand code everything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.