views:

61

answers:

2

It's always seemed a little at odds with the principles of Java that the Java Servlet Spec (2.5 version here) includes a set of magic attributes containing info about included resources, namely:

javax.servlet.include.request_uri
javax.servlet.include.context_path
javax.servlet.include.servlet_path
javax.servlet.include.path_info
javax.servlet.include.query_string

It's not even specifically pointed out in the API documentation, only in the spec where it is a must for correct implementation.

This approach feels very wrong, an exposed implementation detail that clients will use and depend on. Why is this information exposed in this way?

+3  A: 

They are actually specified in the API documentation, in the Constant Field Values part. As you see in the API documentation, you are supposed to use the constants of RequestDispatcher instead, e.g:

String includeRequestURI = request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
BalusC
I never realised they were specified down in the depths of the API docs like that! Using the constants makes a little more sense, thanks!
Brabster
+2  A: 

The fact that you should never use raw constants (magic numbers) in your code does not mean that when an interface does require a constant to be passed in that must be specified.

From the user's point of view only the constants are valid, but from the implementation point of view, those constants must be matched to real values if different systems are to interact. If the constants were inconsistently defined in different implementations, then classes compiled in one implementation (with the constant values possibly inlined in the binary) would fail to work in a different implementation.

Note that it is still important that code does not depend on the magic number, as the spec can define different values in any later review if they decide upon it.

David Rodríguez - dribeas