Random ruminations of a java developer

Thursday, August 24, 2006

Quick JSP (Java Server Pages) Programming Tutorial

This is a quick tutorial of the JSP technique. JSP pages are used with Java application servers, for example Tomcat, Weblogic or Websphere.

Some quick facts before we get going into more details:

-JSP files are executed at server.
-JSP files contain HTML + varying amounts of embedded Java code. (There's a bit more though, like tags and EL, but you can ignore them when beginning)
-When a web user surfs to an address like http://foo.com/index.jsp, this jsp page is executed at the server, and the result is a normal HTML page for the client browser.

Very easy so far?

JSP has two styles of embedding Java code into JSP:

1) Java expression:

..html...
<%= java expression %>
..html..

This first style is used when you want to output the result of a simple java expression into the resulting HTML. For example:

<div>
<%= customer.getName() %>
</div>

Notice that there is no semicolon (;) here.

2) A block of Java code:

...html...
<% // java code %>
...html...

This style is used when you want to execute larger blocks of code. For example:

<p>
<%
Customer customer = new Customer();
customer.setName( "customername" );
%>
</p>

Importing classes

The import is a bit different from normal Java imports. It goes like this:

<%@ page import="java.util.List,
java.util.Iterator"
%>

Which functions in identical way to this java class import declaration:

import java.util.List;
import java.util.Iterator;

The JSP import declaration should be in the beginning of the page, just like a normal Java import.

Outputting strings to resulting html from code blocks

This is easy:

<%
...java code...
out.println( "foo" );
...java code...
%>

Thats it , now you are ready to code JSP pages. There is more stuff, but this will get you started with producing real results.

Tags:

Tuesday, August 22, 2006

Novice Java Programmer Mistakes, part 1

I've done a lot of code reviews over the years and the same mistakes always pop up. Often they are really trivial ones like this one, maybe I'll write a top 10 of these some day :)

Using == when really should be comparing objects with equals()

String a = "aa";
String b = "bb";

if( a == b ) // this is wrong
{
// do something
}

The previous example of course really should be like this:

if( a.equals( b ) ) // this is correct
{
// do something
}

Here's an additional tip, when comparing string literals to some String variable its quite useful to write the comparison like this:

"foobar".equals( a );

This makes it possible to leave out the null check, which in turn results in cleaner code.

Tags:

Sunday, August 20, 2006

Web application testing with JMeter

http://jakarta.apache.org/jmeter/

Jmeter is a useful tool for testing applications, I have mainly used it for web application testing, it can be used for other types software too. The target application does not have to be a Java application.

When developing web applications its essential to load test the application early and often. Using JMeter it is easy to have a working load test script ready all the time while working on the application.

Creating a simple web application test is easy with JMeter, just add a thread group, add some http requests to the group and go. This is the bare minimum test script.

To actually get some meaningful results you need to add some type of listener, for example graph listener which shows graphically the number of requests served by the web application. The numbers it shows include throughput and the average response time, this is very useful to get a quick look at the application performance.

For testing more complicated web applications you need to create some regular expressions for parsing the resulting response pages. This might sound complicated but it is really easy, I will write about this in another post.