JSP Insider - http://www.jspinsider.com

News - Articles/Tutorials/Code/Reviews - JSP/Servlet Hosting Companies - Links

JSPBuzz Vol I: Issue 3 -- 8/19/2000

HTTP://WWW.JSPInsider.com/ .

___________________________ Sponsors ________________________________

This newsletter sponsored by: JSP Insider.
_____________________________________________________________________


Like this newsletter? Tell a friend about the JSP Buzz!

***** IN THIS ISSUE ************************************************** 
 
News	
1:JSP 1.2 and Servlet 2.3 drafts announced.
2:Sun has changed and updated the Java Forum Section.

What's new at JSP Insider?
3:BeanMailer
4:JSP Reference Index

Rambles 
1: Getting to the Source of the Matter
	
Links 	
1:BlueJ a New Simplified Java IDE.
2:Tag Libraries Tutorial
3:Java: Can Sun Control the Flood?
4:JSP Professional Quiz
5:Thirty Minute Code Break
6:Data Binding from XML: Parts 1 & 2
7:JSP Technology and SVG (Scalable Vector Graphics)

Main Topic 
1: Speed is life! Optimizing your JSP & Java code.
**********************************************************************

================================================================
              THE LATEST NEWS
================================================================

1) JSP 1.2 and Servlet 2.3 drafts announced.(8/18/2000)
The public draft 1 (PD1) of the JSP 1.2 and Servlet 2.3 specification 
is now available. The public review for this spec is during the
next 30 days. This is your chance to give your feedback and help 
make JSP better. Sources for the API classes will be made 
available through the Tomcat project at jakarta.apache.org. This is the
start of the process for the new versions. The final specification will 
be issued during the second quarter of 2001.

2) Sun has changed and updated the Java Forum Section. (8/11/2000)
The forum has been updated and is now easier to use. The overall 
layout is clean and easy to navigate. The forum has a nice
watch utility that lets you track specific messages over time. You may 
also have the forum email you when a specific topic has received an 
answer. Creating a user account is a bit confusing, but when you are 
posting a message the system will prompt you to create a user account.
It's very active, with over 1600 members in the JSP section and 
thousands more in the other sections. This a good place for
finding help to solve your JSP and Java problems.


================================================================
              What's new at JSPInsider
================================================================

1) BeanMailer (8/2/2000)
BeanMailer is a small email utility designed to send mail from a JSP
page to a SMTP server.

2) JSP Reference Index (8/8/2000)
JSP documentation written by programmers for programmers, with code 
examples and detailed usage notes.


================================================================
                   Rambles by Casey Kochmer
================================================================

******   Getting to the Source of the Matter.  *******
In server page development, using the view source ability of the
browser is one of the most important tools we have for fixing bugs 
and verifying that our client-side code is correct. It's a feature 
I have used without much thought, since it is very easy to use and 
apparently works as one would expect. Well, over the past few 
weeks, I have discovered several quirks and at the same time (in 
reading the current Risks Digest) come across a possible view source
bug in the latest version of Internet Explorer 5.5.  

Two bugs caught my attention. The first was in Internet 
Explorer 4.0, where it would just drop lines of JavaScript 
from the view source display. The second bug I found was in Netscape 4.7,
where view source will not show the code correctly in a recently 
changed page due to caching problems. For server side developers, 
view source is a critical tool for our debugging.  But please keep in 
mind, it's not perfect. What you see may not be what you get. 
So if you are having extra troubles tracking down a bug, 
remember this fact: When you are looking at the client-side source 
in your browser, it may not reveal the whole picture.

Referenced and Supporting Materials:
[Forum] The Risks Digest (Peter G. Neumann  7/28/2000) 
        When what you see isn't what you get (Lloyd Wood, 7/17/2000)

Please note: I have tried to duplicate the Internet Explorer 5.5 
bug as listed in Risks Digest, but was unable to do so. I include 
this reference as food for thought. Also, Risks Digest is a great 
forum, worthy of a link reference. It is a wonderful source of 
information about the challenges we face due to poor design and 
programming errors. 

================================================================
              Links of Interest
================================================================

[JAVA IDE]  BlueJ a New Simplified Java IDE.  
BlueJ was developed specifically to teach object orientation with Java.  
It's free and easy to use. BlueJ is geared toward helping new 
programmers learn both Java and the general principals of object 
orientation. It was developed by the Monash University and is 
quickly gaining acceptance in other universities.

[Tutorial] Tag Libraries Tutorial (Sun 07/25/00) 
A new tag libraries tutorial from the Sun web site.

[Article] Java: Can Sun Control the Flood? (Charles Babcock, 6/4/2000)
This is a state of the union article about the current status of Java.
It reviews Sun's relationships with other Java supporting companies
and with Java programmers. However, the most interesting statement
is that Java is replacing Visual Basic as the fastest growing language.

[JSP Quiz] JSP Professional Quiz (Dan Malks  8/11/2000)
A simple 10 question quiz to take about JSP. The quiz is written in 
JSP and AFTER you take the quiz, it gives you a chance to review the 
JSP code behind the quiz.

[Web Page][Applet][Toys] Thirty Minute Code Break.
You have just spent an intense 1/2 hour optimizing code in your latest
project, so you deserve a break!  Sodaconstructor is an example
of a well designed applet. You build a small, two dimensional model 
made out of masses and springs, then breathe life into your 
construction. Be warned!  This is one of the more addictive sites I have 
yet visited. It is amazingly fun and if you are in need of something 
to make you smile, this is the site for you!

[Article] Data Binding from XML: Parts 1 & 2 (Brett McLaughlin 7/20/2000)
Part 1 Part 2 
If you are beginning to use XML and Java then this is the documentation 
for you. It is clearly written, concise and contains a perfect overview 
of the methods used to access XML datasets. Brett clearly defines what
your options are when trying to use XML from Java. The articles focuses
on how to convert an XML dataset into a Java Object by a method called
data binding.

[Article]  JSP technology and SVG (Scalable Vector Graphics) (Sun)
Scalable Vector Graphics format is a XML-based syntax for building 
vector-based 2D graphics. This article discusses using 
JSP to generate graphics built using the SVG syntax. The article
includes sample code and a basic introduction to using SVG.


================================================================
                     MAIN TOPIC by Casey Kochmer
================================================================
Speed is life! Optimizing your JSP & Java code.

Java is a wonderful product for writing object orientated code.
However, even if your code is perfect you will fail if it doesn't 
perform at acceptable speeds.  Speed is life for a program. The 
simple truth is, if your JSP site is consistently slow then 
your users will just go elsewhere.

With JSP, several areas are of concern if web sites are to run at 
acceptable rates. In this article, I am going to concentrate on 
optimizing the Java aspects of a JSP Site.  These Java optimizations 
may be broken down into two major categories: Server optimization 
and Java code optimization.


[Server Optimization:]
With JSP, we are fortunate since we have tremendous control over our 
server environment, a luxury that most program developers don't have.
One of the secrets to success in JSP is to leverage that control and 
optimize your web and JSP container to run quickly and smoothly. 
Here's some tips.

* Select a JSP container that will be fast enough to handle your 
expected computational needs. Tomcat is a wonderful reference 
implementation, but it isn't very fast. Running either the Resin or
Orion JSP Containers will provide you with results that range from
4 to 8 times faster than Tomcat. Many containers are available in the
marketplace, so in choosing one, make sure it is fast enough for your
web site implementations.

* Buy as much memory as you can afford!! The simple fact is that
Java is memory intensive. It's a simple equation:  more memory means
more speed. In addition, with higher amounts of memory comes the added
benefit of using extra session variables to help speed up key processes.
JSP adds session variables to the Java mix. These session variables 
demand extra memory from your JVM, which already needs lots of memory.
The end result is that you can never have enough memory for your JSP
server. While I don't recommend going wild with huge session 
variables, I know of sites storing upwards to a gigabyte worth of 
session data during peak times and still having great response times. 

* Use the latest JVM your server can support. Each new JVM increases 
the performance of Java tremendously. Java 1.3 is roughly eight times 
faster than Java 1.2. However, this is a generalization, and you 
should be prepared to test for your system configuration.

* Load balance your memory usage so your web site doesn't run out of 
memory during your peak times. If you are running low on memory while
in your testing phase, don't expect your performance to get better 
when your site is really getting hammered! Over estimate your memory 
needs by a large factor, since, as discussed above, having extra memory 
never hurts.

* Not all JVM's are created equal. Sun isn't the only company to make 
a JVM. IBM, for example, has created some nimble JVM's. If you are in 
need of some speed and the other tips aren't helping, then look around 
and check into a another JVM.  For instance, in one IBM test, they 
found the IBM 1.1.8 JVM ran faster than the SUN 1.2 JVM for a 
JavaServer Page implementation.

* Not all JDBC drivers are created equal. Read the documentation
carefully before you buy an JDBC driver and make sure it supports 
the cursor implementation you need, with an adequate response time. 
I have used JDBC drivers where the memory utilization was so bad for 
scrollable cursors that, in effect, you could only use the forward only 
cursor for acceptable response times. If you are buying an JDBC driver, 
make sure it has a trial usage period. If you are using a free driver, 
be prepared to buy a speedier driver if your response times are not 
acceptable for your web site needs.

* Avoid using the JDBC-ODBC bridge.  It is not recommended for 
production systems and has known memory leak problems.

* Use database connection pooling to speed up database connectivity on
the server.


[Java Code Optimization:]
Several problems commonly cause speed slowdowns within Java code. 
Here is a partial list with some solutions.

* If you are performing large amounts of string additions, 
then use the StringBuffer Class.

   For example:
 
   String ls_hello = "Hello" + "world" + "this is me";

   is slower than

   StringBuffer("Hello").append("world").append("this is me").toString();

Since, in JSP, much of HTML life revolves around strings, this is an 
important tip!

* Significant performance trade-offs exist for the various object 
storage options.

  -An array is at least 4 times faster than an ArrayList.
  -An ArrayList is at least 50% faster than using a Vector.
  -Don't use Vectors or Hashtables! Using a Vector or Hashtable will 
   just slow your code down and give you a headache due to 
   synchronization issues.
  -A HashSet is faster than a TreeSet (but both are much slower than 
   an ArrayList)

* Have a well balanced design. Trade-offs exist in any design. A 
design built for reusability usually comes at the cost of speed. You 
may build a wonderful code library with thousands of classes, but then 
you will pay the price of load time and overhead for those classes. 
You may build a super class which does everything, but then your one 
mega class will be too hard to manage and maintain. The key to design 
lies in moderation and practical trade-offs of reusability versus speed. 
This is where the overall greatest gains and losses are to be had. It 
is also the hardest thing to do well. How to best balance your design is 
a matter of debate in the programming community.  Practical experience
is your best guide.  For more on this topic refer to: "Thinking in JAVA." . 

* Design your java code to avoid using the synchronize keyword. 
It has been shown that adding synchronization can slow your process 
by a factor of eight. Note:  Some objects are automatically 
synchronized, such as the Vector object. Carefully check an object's
documentation so you understand what design issues you will have
when you implement your code.

* Use the scope private or final when possible as your default scope.
.
* Object creation is very expensive. Reuse your objects when possible.  
You may build a re-initialization option into your object.  Often times,
re-initializing your object is faster than creating a new object. From 
a JSP point of view, this means you could use the session to store 
and then reinitialize the object as needed. If a JSP page is heavy 
with object usage (especially beans) and it is being re-accessed 
frequently, then you should consider this option to speed code 
execution.  Other methods of object pooling are available and it 
may be worth your time to check into them. 

* Java can leak memory and garbage collection isn't perfect. But 
garbage collection is JVM's way to free up memory. In light of the 
importance of memory as discussed above, garbage collection can't be 
ignored in time sensitive processes.  Only use a garbage collection
call when absolutely necessary, since, once it starts, it can slow 
your processing while it cleans up.  You may want to avoid or minimize 
its use in the middle of peak loads.  The trick is to call garbage 
collection while things are idle (a tough challenge for a web site).  
But this may be out of your control since JVM can start up garbage 
collection at anytime.  One option before initiating a memory 
intensive process is to null out all references to objects you no 
longer need, call garbage collection to clean up the memory, and 
then start up your process.

[A warning]
Sun recommends you save optimization for your last step of program
development for two reasons. First, if you are not careful, you may 
spend too much time optimizing code that doesn't really need 
optimization.  Second, the Java compiler performs quite a bit of 
optimization for you (and every new version improves upon this 
optimization).  I suggest a balanced approach to optimization. 
In your first pass, work at your code, making sure your logic is clean 
and easy to understand. Then perform periodic reviews of your code 
and verify that the speed is acceptable. If you notice a performance 
problem, look for the 10% of the code/environment which is causing
80% of your slowdown. Concentrate on those problems.  It is surprising, 
but true, that many of your speed bottlenecks will be concentrated in 
small sections of your code.   Use the Java profile option to help 
determine some of your problem areas.


[In Conclusion:]
Java is a robust language, and many ways exist to help improve your 
performance. However, Java is a varied language with many exceptions 
and variations due to the different versions of JVM implementations. 
It all really boils down to one issue.  The most important Java 
optimization tool is an experienced programmer.  Don't only put junior 
programmers on a JSP application where speed will be an major issue. 
Learning to optimize Java is a fine art, and having a veteran Java 
programmer working on the critical objects for your project is often 
the key to success. 


[Additional Reference Materials:]
Java Optimization  (Nate Sammons) 

Building High-Performance Applications and Servers in Java  (IBM)

================================================================
                           SUBSCRIPTION
================================================================
You may unsubscribe from or subscribe to this newsletter by going to:
http://www.jspinsider.com/jspbuzz/index.html

Browse all of the JSP Insider source-code.

Questions or comments? Contact support@jspinsider.com.