java is often criticized for its performance, particularly in comparison to equivalent code written in languages such as c and c++. advances in runtime performance have silenced many critics, but still there are times when no matter how fast java is, its just not fast enough.
fortunately, java does have some unique properties that make a class of optimizations accessible that are not easily applied to other platforms. one technique that can be used to great effect on certain types of tasks is dynamic code generation.
dynamic code generation is the process of generating java classes at runtime that can then be loaded into that same java virtual machine (jvm) and accessed the same way as ordinary compiled java classes. the usefulness of the technique might not be immediately apparent, but its actually used to a great degree of success in several critical, high-profile java technologies.
javaserver pages
perhaps the best-known example of dynamic code generation in java is javaserver pages (jsp). a servlet engine is capable of dispatching requests to a servlet for processing. unfortunately, servlets are static in nature. they typically need to be precompiled and preconfigured in the servlet engine before server startup. although theyre quite useful, web development often requires a more flexible solution.jsp breaks these restrictions and allows servlets to be dynamically created at runtime based on the contents of a jsp file. when a request is made for a jsp file, the servlet engine makes a request to the jsp engine, which in turn will process the jsp file and send a result. a jsp is a textual representation of a set of actions to be performed to present a page to the user, and interpreting it anew for each request would be quite costly. instead, the jsp engine will compile the jsp and dynamically create a servlet object. if the specification changes, a new servlet will be dynamically generated.
dynamic code generation... 下一页