A very easy JSP (Java Server Page)
Archive - Originally posted on "The Horse's Mouth" - 2009-05-01 17:01:49 - Graham Ellis"Can you give me an easy first example" ... a common question on a course and - yes, I usually can. And so can other tutors. But then they go on and add things to make them complicated. So here is a really easy JSP example!
JSP is what I call an "HTML++ language" ... you write the HTML of the web page and you embed, within it, extra code of a sort that the browser wouldn't understand. Then you add a filter on your web server telling it to identify these extra codes (tags) and perform the instructions they contain.
Here is the HTML++ code ...
<% int wide = 5; int deep = 3;
int spaces = wellho.Utils.mully(wide, deep); %>
<html>
<head>
<title>Laptops I have poured coffee over</title>
</head>
<body>
<h1>This is my web page</h1>
You have room for
<%= spaces %> laptops
<hr>
Copyright etc
</body>
</html>
... and here is the result of running it, as received by my browser ...
<html>
<head>
<title>Laptops I have poured coffee over</title>
</head>
<body>
<h1>This is my web page</h1>
You have room for
13 laptops
<hr>
Copyright etc
</body>
</html>
The original code from <% to %> is Java statements that the web server runs, and the code from <%= to %> is a Java expression which is worked out, cast to a string, and output.
To complete the example, I chose to call a method in another java class (wellho.Utils.mully) to perform my calculations - it's representative of the work that goes on (business logic) behing my web page. Here's the source code that defined it:
package wellho;
public class Utils {
public static int mully(int dis, int dat) {
int wesult = dis * dat -2;
return wesult;
}
}
We've just scheduled - at short notice - two extra Java courses for May. Learning to program in Java from 11th and Java Bootcamp which starts a day later for those with prior programming experience. As this is a very late offering, you could twist my arm to give you five days for the price of four, or four days for the price of three. And the groups size will be 'just a handful' so there will be a superb tutor to student ratio to ensure that every delegate gets a lot from the course.