Java Technology in the e-Labs

The website code is heavy on Java. If you don't know Java, this wiki is not going to help you.

Even if you do know Java, you may still encounter several Java-related technologies in the code that you aren't familiar with. While not a comprehensive guide, the outlines below will help you start learning what you need to know about them.

First, a few reminders in case you haven't used Java in a while:

Java syntax:
  • All lines end with a semicolon;
  • // single-line comment, /* multi-line comment */
  • Seriously, comment your code.
  • In-line comments are allowed
  • Lines automatically continue across newlines and indentation
  • Whitespace doesn't matter
  • Angle brackets indicate what type of data a multi-element object (like an array or matrix) holds: TextbookTitleArray<String> is an array of strings.
Java naming conventions:
  • Class, method, and most variable identifiers are camel-cased
  • Class names start with an upper-case letter and are nouns: class ConnectionMaker
  • Method names start with a lower-case letter and are verbs: updateDataArray()
  • Static final values (i.e., constants) are UPPER_CASE_WORDS separated by underscores

JSP

JSP stands for JavaServlet Pages, and is one of the primary formats of the e-Labs site. Again, this wiki will not teach you JSP if you don't already know it, but a little reference information is always useful.

JSP is organized around four scopes; from narrowest to broadest they are
  1. page
  2. request
  3. session
  4. application

Universal Expression Language

You will frequently encounter syntax of the form ${dot.separated.expression} in the .java, .jsp, and Ant files. This is an example of the Universal Expression Language (UEL) common to the greater Java ecosystem (JSP, JavaServer Faces, JavaBeans, Ant, etc.). You can learn much more about it from Oracle's documentation page.

The ${} syntax is only one component of the UEL, but it's by far the most common one in the e-Labs code. It's used to denote an immediate-evaluation read-only value expression. "Value expression" means that it evaluates to something. "Immediate-evaluation" means that the interpreter evaluates it at the time the code is processed, and "read-only" means that it cannot be used to alter stored data. An alternate #{} syntax allows greater flexibility with both timing and writability, but it's rarer in our code.

UEL value expressions can refer to the properties and attributes of

The first element of a ${} expression must be a previously-defined variable. The interpreter will look for it within the JSP scopes, beginning with the narrowest (page). Subsequent elements are methods that act upon the preceding elements in the manner of regular Java method chains. That is, the UEL expression ${variable.method1.method2} would be equivalent to the ordinary Java expression variable.method1().method2(). [Q: what about arguments to methods?]

variable can be a JavaBeans component and method1, method2 accessor methods for reading data from it using the standard Beans syntax where ${component.value} equates to component.getValue().

Java Database Connectivity (JDBC)

The JDBC API is a standard part of Java SE that allows Java code to interface with databases. Its classes are contained in the java.sql (basic) and javax.sql (extended) packages (the e-Labs use only the basic java.sql, though). If you see Connection objects or the *Statement objects PreparedStatement and CallableStatement, these are JDBC tools.

JavaBeans

JavaBeans is a syntax within JSP that allows many common coding practices to be streamlined. A JSP page that uses JavaBeans will declare this with a <jsp:useBean> tag.

Miscellaneous

class literals: the .class token

Everything in Java is an object. So, everything in Java is an instance of some class. Integers like 5 and 46 aren't just numbers, they're Integer objects that are instances of the Integer class.

The same is true of classes themselves! Every class in Java is a Class object that is an instance of the Class class. Note that this is completely independent of extension, inheritance, etc. MyChildClass might extend MyParentClass, and both implicitly extend the Object class, but MyChildClass, MyParentClass and Object are all instances of the Class class -- not subclasses of it. (Of course, it is possible for a class to be a subclass of Class, but such things are the exception.)

So, if you need to refer to a class as a Class object, append the token .class to it: MyChildClass.class. This forms a class literal, and it lets the interpreter know that you want to manipulate directly the instance of Class that MyChildClass represents. This method of forming a literal can be a little confusing, because it's different from other literals. For example, "example string" is already a string literal -- you don't have to go around writing "example string".string to have the interpreter recognize it as a literal, nor do you have to write things like 1.integer + 2.integer = 3.integer in order to have these literals recognized.

The class literal is a special case because the name of the class can be used for other things, like invoking a class method or declaring a new object:

String sample = new String();
sample = "example string";
Int sampleSize = String.length(sample);

To avoid confusion, you must be extra-explicit about forming a class literal, hence the .class token. For example, consider the two cases below:

String stringName = String.getName();
String className = String.class.getName();

The first calls the getName() method of the String class. This method doesn't actually exist, but it might for any other given class, and what it returns is up to that method. The second specifies the getName() method of the Class object String.class, and it therefore invokes the getName() method of the Class class. This returns the name of the given class instance as a string; in this case, that's "String".

You can also form the class literal of non-class object like a method or variable; in that case the class literal is the Class instance of the class that the object is an instance of. For example, "example string".class is equivalent to String.class, as they both refer to the Class instance String.

synchronized

The synchronized keyword is used to protect sensitive objects from conflicting writes. There's some good documentation from Oracle about it.

When you create a synchronized statement, you must provide an object to serve as a "lock" -- essentially, a unique identifier for the locked thread. When a Java object is subject to a method or statement marked as "synchronized," then no other methods or statements synchronized under the same lock may affect the object until the first synchronized process is complete. Essentially, it's the exact opposite of what it says. Actions that are declared synchronized are prevented from happening at the same time.

A method or statement synchronized under one lock is free to affect an object synchronized under a different lock, as if there were no synchronization. This allows you to have multiple unrelated synchronization threads without them interfering with one another.

-- Main.JoelG - 2016-05-17

Comments

 
Topic revision: r6 - 2016-05-19, JoelG
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback