|
CSCI A348/548
|
Assuming that
.jsp extension and
No compiling, no packages, no CLASSPATH settings.
Do you have a web server that supports JSP?
Yes you do, since you installed your Tomcat.
Here's a set of examples to get you started with JSP.
index.html file.
Here's my$TOMCAT_HOME/webapps/ROOT
index.html (Felix the Cat .gif added). You should change yours also just to make sure you can locate it properly.
Note that page does not contain the actual path to it on the page, so don't get misled by that.
Now create a file (called test.jsp) in this directory with the following contents:
Then access it over the web. Here's how mine works.<html> <body bgcolor=white> Current time: <%= new java.util.Date() %> </body> </html>
JSP scripting elements let you insert code into the servlet that will be generated from the JSP page. There are three forms, and we have just looked at one of them:
are evaluated and inserted into the servlet's output.<%= expression %>
Create a directory lab11 and cd to it.
Create e1.jsp with the following contents:
<html><body bgcolor=white>
JSP Expressions: <ul>
<li> Current time: <%= new java.util.Date() %>
<li> Your hostname: <%= request.getRemoteHost() %>
<li> Your session ID: <%= session.getId() %>
<li> The value of a form's <code>param</code> parameter:
<%= request.getParameter("param") %>
</ul>
For the last one call your <code>.jsp</code> with
<blockquote><pre>e1.jsp?param=butterfly</pre></blockquote>
</body></html>
Here's how mine works. OK, let's look at some other examples.
If you want to do something more complex than insert a simple expression, JSP scriptlets let you insert
arbitrary code into the servlet's _jspService (which is called by service).
Scriptlets have the following form:
Here's an example:<% Java Code %>
Create this as
e2.jsp in your lab11 directory.
<html>
<head><title>Color Testing</title></head>
<%
String bgColor = request.getParameter("bgColor");
boolean hasExplicitColor;
if (bgColor != null) {
hasExplicitColor = true;
} else {
hasExplicitColor = false;
bgColor = "WHITE";
}
%>
<body bgcolor="<%= bgColor %>">
<h2 align="center">Color Testing</h2>
<%
if (hasExplicitColor) {
out.println("You supplied an explicit background color of " +
bgColor + ".");
} else {
out.println("Using default background color of WHITE. " +
"Supply the bgColor request attribute to try " +
"a standard color, an RRGGBB value, or to see " +
"if your browser supports X11 color names.");
}
%>
</body>
</html>
Here's mine called in three different ways: C0C0C0 provides a grey background
Let's review what we've done so far.
JSP scripting elements let you insert code into the servlet that will be generated from the JSP page.
There are three forms, and we have just looked at two of them:
are evaluated and inserted into the servlet's output.<%= expression %>
which are inserted into the servlet's<% code %>
_jspService method (called by service).
They are of this form
and are inserted into the body of the servlet class, outside of any existing methods.<%! code %>
Let's practice with declarations.
Create e3.jsp in your lab11 directory:
Then test it. (You can test mine). So now you've seen the three JSP scripting elements in action.<html><head><title>JSP declarations</title></head><body> <h1>JSP Declarations</h1> <p> <%! private int accessCount = 0; %> <h3>Accesses to page since server reboot: <%= ++accessCount %> </h3> </body></html>
You can now try to look over the JSP examples that Tomcat comes equipped with.
In class we will be going over the JSP topic in greater detail, and in a more structured way.
Here are a few more examples for your study and enjoyment.
For example you can easily predict the outcome of the presidential election with the following script:
<html><body>
<% if (Math.random() < 0.5) { %>
The new US president is Al Gore.
<% } else { %>
The new US president is George Bush.
<% } %>
</body></html>
This script illustrates conditional HTML. Here's how mine works.
A348/A548