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: Java Programming JSP