Wednesday, April 9, 2008

[Java, JSP] JSP for beginners: Are you prepared?


After a week or so of inactivity, we are back with a beginner tutorial about JSP (Java Server Pages).
Are you prepared? (this is a quote from a popular MMORPG).

Basically JSP is a way to use the power of Java inside usual HTML pages.
The first step is to install a capable web server, like Apache Tomcat: JSP files have to be put in the webapps directory or in a subfolder.

As you put Java into HTML files, you can insert neither one Java line of code and simply rename an html file into JSP (.jsp extension) and it will work!
Actually, when the web server open for the first time the page, it compiles it: it leads to a slower loading (only the first time), that happens each time you modify your page (even if there is no Java code), so don't change in JSP each page of your application without discrimination!

How do I embed Java code?

<HTML>
<BODY>
<%
String message = "<p>Hello, this is my first JSP page!!</p>"
String endPage = "<p>Bye bye!</p>"
System.out.println(message);
%>
<p>Above you are reading a wellcome message.</p>
<%=endPage%>
</BODY>
</HTML>


As you can see, inside the <% ... %> there is pure Java code: this is called a scriplet.
More over using <%=endPage%> you can output a java expression, that is the same as calling the System.out class: using JSP you have not to specify the System.out.print() function and call directly out.print(), where out is a global member that outputs directly.

If you need a specific Java class, you can import its package (that should be in the classpath, depending on you web server) usign a JSP directive:

<%@ page import="java.util.Date, java.util.ArrayList" %>


If you need to insert another JSP page inside the current page, in which I suppose you have a particular functionality you use frequently in several other pages, you can import it:

<%@ include file="hello.jsp" %>


In order to interact with your HTML controls, you use the current request object (that comes from the JSP context) and as for session variabiles you'll use the session global variable:

<%

String action = request.getParameter("submitted");

if( action.equals("true") )
{
/* saves a variable as a session variabile:
* from now on it is always available */
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
}


if(session.getAttribute("theName") == null)
{
%>
<FORM METHOD="POST" ACTION="thisPage.jsp">
What's your name? <INPUT TYPE="TEXT" NAME="username" SIZE="20">
<INPUT TYPE="HIDDEN" NAME="submitted" VALUE="true">
<INPUT TYPE="SUBMIT">
</FORM>
<%
}
else
{
out.println("<p>Hello "+ session.getAttribute("theName") +" !!!</p>");
}
%>


What does it mean?
Mainly if you have already set a session variabile with your own name, so there is no need to print out a form, but just a welcome.
First of all the action string is used to store the submitted vairaible, that comes from the submission of the form with the hidden input control.

In case of submission, the action variabile is equal to true, and in that case we store the session variabile thaName with the value of the input text control named username: from now on this session variabile will be available in all your JSP pages till the end on the session (depending on the settings of your web server).

Afterwards if there is not a value for the session variable theName, than prints out the form (without using the out.println() but just as HTML content); else prints out a welcome message, retrieving the session variable theName.

There is also a way of using particular JSP tags to use common Java functionalities...but it is another story!
Stay tuned!!

2 comments:

Abava said...

check out Coldtags suite here:
http://www.servletsuite.com/jsp.htm
300+ components for JSP can simplify your developers life

Elsa said...

se ti va passa dal mio blog a leggere una storia di Porto Torres

ciao