Moving up to Java 1 5 and Tomcat





![Old context. xml <!DOCTYPE doc [ <!ENTITY MYDB SYSTEM "file: /opt/sa_forms/java/dev/edu/iu/uis/security/my/MYDB. xml"> ] > Old context. xml <!DOCTYPE doc [ <!ENTITY MYDB SYSTEM "file: /opt/sa_forms/java/dev/edu/iu/uis/security/my/MYDB. xml"> ] >](https://slidetodoc.com/presentation_image_h2/09c99cfe47967644a57c7af197529463/image-6.jpg)
![New context. xml <!DOCTYPE doc [ <!ENTITY MYDB SYSTEM “file: /opt/sa_forms/java/dev/edu/iu/uis/security/my/MYDB. xml”> ] > New context. xml <!DOCTYPE doc [ <!ENTITY MYDB SYSTEM “file: /opt/sa_forms/java/dev/edu/iu/uis/security/my/MYDB. xml”> ] >](https://slidetodoc.com/presentation_image_h2/09c99cfe47967644a57c7af197529463/image-7.jpg)





























- Slides: 36

Moving up to Java 1. 5 and Tomcat 5. 5 Java Hour – Sept 23, 2005

Infrastructure p <TOMCAT_HOME>/common/lib n n p ojdbc 14. jar commons-dbcp-1. 2. 1. jar commons-pool-1. 2. jar commons-collections-3. 1. jar May want to copy your old cacerts file into <JAVA_HOME>/jre/lib/security if you have added any hosts to cacerts in the past

Add a new JRE in Eclipse 1. 2. 3. 4. 5. 6. Window->Preferences. . . Expand “Java” Click “Installed JREs” Click the Add button Name it whatever you like, click the Browse button and point it at your <JAVA_HOME> Click the OK button

Modify Tomcat 5 in Eclipse 1. 2. 3. 4. 5. 6. 7. 8. Window->Preferences. . . Expand “My. Eclipse” Expand “Application Servers” Expand “Tomcat 5” Enable it if not already enabled Click the Browse button for “Tomcat Home Directory” and select your <TOMCAT_HOME> Click on “JDK” in the tree to the left under Tomcat 5 Select your JRE for Java 1. 5 that you created on the previous slide

Application Changes p Recompile your code. It is possible that you have used a keyword that is new to Java 1. 5 p context. xml – the tomcat developers/designers decided to change from xml tags or elements to attributes
![Old context xml DOCTYPE doc ENTITY MYDB SYSTEM file optsaformsjavadeveduiuuissecuritymyMYDB xml Old context. xml <!DOCTYPE doc [ <!ENTITY MYDB SYSTEM "file: /opt/sa_forms/java/dev/edu/iu/uis/security/my/MYDB. xml"> ] >](https://slidetodoc.com/presentation_image_h2/09c99cfe47967644a57c7af197529463/image-6.jpg)
Old context. xml <!DOCTYPE doc [ <!ENTITY MYDB SYSTEM "file: /opt/sa_forms/java/dev/edu/iu/uis/security/my/MYDB. xml"> ] > <Context path="/my-dev" reloadable="true" doc. Base="c: javaprojectsmymy"> <Resource name="jdbc/dev/my/MYDB" auth="Container" type="javax. sql. Data. Source"/> <Resource. Params name="jdbc/dev/my/MYDB"> <parameter> <name>factory</name> <value>org. apache. commons. dbcp. Basic. Data. Source. Factory</value> </parameter> <name>max. Active</name> <value>100</value> </parameter> <name>max. Idle</name> <value>5</value> </parameter> <name>max. Wait</name> <value>10000</value> </parameter> <name>driver. Class. Name</name> <value>oracle. jdbc. driver. Oracle. Driver</value> </parameter> &MYDB; <parameter> <name>validation. Query</name> <value>select 1 from dual</value> </parameter> </Resource. Params> </Context>
![New context xml DOCTYPE doc ENTITY MYDB SYSTEM file optsaformsjavadeveduiuuissecuritymyMYDB xml New context. xml <!DOCTYPE doc [ <!ENTITY MYDB SYSTEM “file: /opt/sa_forms/java/dev/edu/iu/uis/security/my/MYDB. xml”> ] >](https://slidetodoc.com/presentation_image_h2/09c99cfe47967644a57c7af197529463/image-7.jpg)
New context. xml <!DOCTYPE doc [ <!ENTITY MYDB SYSTEM “file: /opt/sa_forms/java/dev/edu/iu/uis/security/my/MYDB. xml”> ] > <Context path=“/my-dev” reloadable=“true” doc. Base=“c: javaprojectsmymy” > &MYDB; </Context>

Guts of the Include File (MYDB. xml) <Resource name="jdbc/dev/my/MYDB" auth="Container" type="javax. sql. Data. Source" username=“xx" password=“xxxxx" driver. Class. Name="oracle. jdbc. driver. Oracle. Driver" url="jdbc: oracle: thin. . . " max. Active="100" max. Idle="5" max. Wait="10000" validation. Query="select 1 from dual"/>

Timeline? p Discussion… questions?

Some Neat Things in Java 1. 5

Quick Overview Generics p Enhanced for loop p Autoboxing and auto-unboxing p Typesafe enumerated types p Variable arguments p Static imports p Metadata or annotations p

Other noteworthy changes… p p apt – annotation processing tool Enhancements to core libraries n n n n Networking (connect timeouts, ipv 6, proxy server, etc) Security (more standards, SASL, crypto, etc) Formatter (printf, layout and alignment, support for common types) Scanner (parse input: scanner. next. Int(), can use regex) Concurrency (powerful thread package – low level) Monitoring and management (can monitor jvm and the os from within your application) More support for profiling – extended the API

Generics provides compile-time type checking and eliminates the need for casts. This serves to optimize collections of older Java versions. p No need to cast anymore… you know for sure what is in the collection and the compiler will help you – no runtime errors! p Cleaner code! p

Enhanced for loops p Enhanced for-loops make it easy to traverse through collections while avoiding error-prone iterators. for ( Type x: collection ) { x. do. Something(); } p p No more Iterator! Less code, more reliable, better performance because the compiler knows best.

Example: Where’s the bug? 01: 02: 03: 04: 05: 06: 07: 08: 09: List<Suit> suits =. . . ; List<Rank> ranks =. . . ; List<Card> sorted. Deck = new Array. List<Card>(); for (Iterator<Suit> s = suits. iterator(); s. has. Next(); ) { for (Iterator<Rank> r = ranks. iterator(); r. has. Next(); ) { sorted. Deck. add(new Card(r. next(), s. next())); } }

Fixed the bug with new for loops 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: List<Suit> suits =. . . ; List<Rank> ranks =. . . ; List<Card> sorted. Deck = new Array. List<Card>(); // Fixed and pretty for (Suit suit : suits) { for (Rank rank : ranks) { sorted. Deck. add(new Card(rank, suit)); } }

Final thoughts on the new loop p Can not be used when: n n n you want to remove an element from a collection. you want to modify an element while iterating. you want to iterate multiple collections at the same time. Any other time, go for it! p Note: can be used with arrays too. p Example 1. java – take a look in Eclipse p

Autoboxing and Auto-unboxing reduce the efforts previously required to cast primitive types back and forth. The compiler now does this for you. p Before Java 1. 5 p Integer grade = new Integer(10); int g = grade. int. Value(); p In Java 1. 5 Integer grade = 10; int g = grade;

Another example p Autoboxing list. add(23); // list is List<Integer> p Auto-unboxing int value = list. get(0);

Caveats The wrappers are created behind the scenes, so performance degradation is possible. p Use them to make your code more readable: IMO, I would sacrifice a bit of performance for maintainability. p Don’t use them when: p n n you are inside of an large inner loop items in a collection might be null

Typesafe enums provide a way to define enumerated types that can be checked by the compiler for type safety. p Better than the C/C++ counterpart: they are a class, not a glorified integer. p Example: p enum Suit { CLUB, DIAMOND, HEART, SPADE };

01: public class Chess. Piece { 02: 03: public static final int TYPE_KING = 0; 04: public static final int TYPE_QUEEN = 1; 05: public static final int TYPE_ROOK = 2; 06: public static final int TYPE_KNIGHT = 3; 07: public static final int TYPE_BISHOP = 4; 08: public static final int TYPE_PAWN = 5; 09: 10: public static final int COLOR_WHITE = 0; 11: public static final int COLOR_BLACK = 1; 12: 13: private final int type; 14: private final int color; 15: 16: public Chess. Piece(int type, int color) { 17: this. type = type; 18: this. color = color; 19: } 20: 21: public int get. Type() { 22: return this. type; 23: } 24: public int get. Color() { 25: return this. color; 26: } 27: 28: public String to. String() { 29: String out = this. color == COLOR_WHITE? “white“: “black“; 30: switch (this. type) { 31: case TYPE_KING: return out + “ king”; 32: case TYPE_QUEEN: return out + “ queen”; 33: case TYPE_ROOK: return out + “ rook”; 34: case TYPE_KNIGHT: return out + “ knight”; 35: case TYPE_BISHOP: return out + “ bishop”; 36: case TYPE_PAWN: return out + “ pawn”; 37: default: return “invalid chess piece”; 37: } 38: } 39: } Typical “enumerated types” before Java 1. 5 - used a bunch of ints Can you think of any problems with this solution?

Problems Not type safe: constructor takes ints… nothing is enforcing a type and color. p Can do things like: p int x = Chess. Piece. COLOR_BLACK + Chess. Piece. TYPE_QUEEN; Printed values are uninformative: just ints. p No easy way to enumerate/iterate. No bounds. p

A fix with typesafe enums 01: public class Chess. Piece { 02: 03: public static enum Type {KING, QUEEN, ROOK, KNIGHT, BISHOP, PAWN}; 04: 05: public static enum Color { WHITE, BLACK }; 06: 07: private final Type type; 08: private final Color color; 09: 10: public Chess. Piece(Type type, Color color) { 11: this. type = type; 12: this. color = color; 13: } 14: 15: public Type get. Type() { 16: return this. type; 17: } 18: 19: public Color get. Color() { 20: return this. color; 21: } 22: 23: public String to. String() { 24: return System. out. printf("%s %sn", this. color, this. type); 25: } 26: }

Benefits p p p p p Compile time safety Loaded at run time – don’t have to recompile client code. Allows arbitrary fields and methods. Can implement interfaces. Can be iterated/used in collections. Inherit from java. lang. Object Implement Comparable and Serializable Nice printed values Performance is similar to using ints – no worries. Can be used in switch statements, unlike other objects.

01: public enum Planet { 02: // Constants 03: MERCURY (3. 303 e+23, 2. 4397 e 6), 04: VENUS (4. 869 e+24, 6. 0518 e 6), 05: EARTH (5. 976 e+24, 6. 37814 e 6), 06: MARS (6. 421 e+23, 3. 3972 e 6), 07: JUPITER (1. 9 e+27, 7. 1492 e 7), 08: SATURN (5. 688 e+26, 6. 0268 e 7), 09: URANUS (8. 686 e+25, 2. 5559 e 7), 10: NEPTUNE (1. 024 e+26, 2. 4746 e 7), 11: PLUTO (1. 27 e+22, 1. 137 e 6); 12: 13: // Fields 14: private final double mass; // in kilograms 15: private final double radius; // in meters 16: 17: // Constructor Enums can be 18: Planet(double mass, double radius) { by giving them 19: this. mass = mass; 20: this. radius = radius; methods! 21: } 22: 23: // Methods 24: public double get. Mass() { return mass; } 25: public double get. Radius() { return radius; } 26: public static final double G = 6. 67300 E-11; 27: 28: public double get. Surface. Gravity() { 29: return G * mass / (radius * radius); 30: } 31: public double get. Surface. Weight(double other. Mass) { 32: return other. Mass * get. Surface. Gravity(); 33: } 34: } more powerfull their own

Using the Planet enum 01: public class Test { 02: 03: public static void main(String[] args) { 04: double earth. Weight = Double. parse. Double(args[0]); 05: double mass = earth. Weight / EARTH. get. Surface. Gravity(); 06: for (Planet p : Planet. values()) { 07: System. out. printf("Your weight on %s is %fn", 08: p, p. get. Surface. Weight(mass)); 09: } 10: } 11: 12: } $ java Test Your weight Your weight Your weight 175 on MERCURY is 66. 107583 on VENUS is 158. 374842 on EARTH is 175. 000000 on MARS is 66. 279007 on JUPITER is 442. 847567 on SATURN is 186. 552719 on URANUS is 158. 397260 on NEPTUNE is 199. 207413 on PLUTO is 11. 703031

Variable arguments p p Varargs allow a variable number of arguments to be passed to a method, yet another borrowed idea from C/C++. A good example is the new printf method. Can only be used as the last variable to a method. Basically, and array is created behind the scenes n Can even call. length to find how many arguments were passed to the method call

Old School (the max method) p Here is a basic max method int max(int a, int b) { return a > b ? a : b; } p For multiple arguments you would call it as follows: // works, but very hard to read int m = max(a, max(b, max(c, d))); p Another solution: write overloaded methods: ex: int max(int a, int b, int c)

Using varargs 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: // return the int with the maximum value int max(int. . . values) { int max = Integer. MIN_VALUE; for (int i : values) { if (i > max) { max = i; } } return max; } int m 2 = max(a, b); int m 3 = max(a, b, c); int m 4 = max(a, b, c, d); // what if the values already come in an array? int[] values = {1, 2, 3, 1}; int m = max(values);

Static imports p p Static imports simplify the task of importing constants and other static members in your code like when you are importing packages. Instead of: double r = Math. cos(Math. PI * theta); p Import it and use it to make your code more readable import java. lang. Math. cos; import java. lang. Math. PI; //. . . double r = cos(PI * theta);

A common Anti-pattern Some ways to get at constants have been to stick them in Interfaces and implement the interface, or just use that interface to get to global constants. p Interfaces are public APIs, and logic (constants) really should not be in a public API. p Just not very clean, and now there is a solution built into the language. p

When vs. When Not? p It’s best not to overuse this neat new feature. It can make your code harder to read if you statically import too much. p Use it when you are accessing a contant a lot, or when you feel the desire to write one of those anit-patterns

Metadata or Annotations Metadata or annotations provide a standard way to annotate your code. p Javadoc and xdoclet are examples p Examples: @Override and EJB p http: //www. devx. com/Java/Article/27235 p

Questions? p Lots of information out there on the web… just ask Google.

Code used in demo… package examples; import java. util. Array. List; import java. util. List; public class Example 1 { public static void main(String [] args) { test 1(); test 2(); } private static void test 1() { List<String> names = new Array. List<String>(); names. add("Joe"); names. add("Beth"); names. add("Randy"); for (String name: names) { System. out. printf("test 1: My name is %sn", name); } } private static void test 2() { List names = new Array. List(); names. add("Joe"); names. add("Beth"); names. add("Randy"); for (Object name: names) { System. out. printf("test 2: My name is %sn", (String)name); } } }