Article
The JSP Files - Parts 1 to 8: Tagged and Bagged
Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next
Java In A Teacup
In order to begin working on JSP, you need to get yourself copies of Sun's Java Development Kit, Apache's httpd Web server and mod_jserv module, and the Tomcat servlet engine, and configure them so that they're all working together. This tutorial assumes that you've got a JSP development environment set up -- in case you don't, take a look at "Slapping Together A JSP Development Environment", a tutorial which will guide you through the process.
With that out of the way, let's get down to the nitty-gritty of actually creating a JSP page. Open up a new file in your favourite text editor and type in the following lines of code:
<html>
<head>
</head>
<body>
<%
// asking for it!
out.println("Waiter, can I have a cup of Java, please?");
%>
</body>
</html>
Save this file with the extension .jsp - for example, "coffee.jsp" - in an appropriate location and then view it by pointing your browser to it - for example, http://localhost/jsp/coffee.jsp . You should see something like this:
<html>
<head>
</head>
<body>
Waiter, can I have a cup of Java, please?
</body>
</html>
And that, grasshopper, is your first scriptlet!
In JSP-lingo, a "scriptlet" is a block of code executed by the JSP engine when the user requests a JSP page. All scriptlets are enclosed within <%...%> tags (similar to ASP and PHP code), like this:
<%
... JSP code ...
out.println("Waiter, can I have a cup of Java, please?");
... JSP code ...
%>
Every JSP statement ends in a semi-colon - this convention is identical to that used in Perl, and omitting the semi-colon is one of the most common mistakes newbies make. Just as an example, here's what happens when you omit the semi-colon from the example above:
Error: 500
Location: /jsp/coffee.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to compile class:
Invalid type expression.
out.println("Waiter, can I have a cup of Java, please?")
^
: Invalid declaration.
out.write("\r\n\r\n\r\n");
^
2 errors
at org.apache.jasper.compiler.Compiler.compile
(Compiler.java, Compiled Code)
at org.apache.jasper.servlet.JspServlet.doLoadJSP
(JspServlet.java:462)
at
org.apache.jasper.servlet.JasperLoader12.loadJSP
(JasperLoader12.java:146)
at org.apache.jasper.servlet.JspServlet.loadJSP
(JspServlet.java:433)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.
loadIfNecessary(JspServlet.java:152)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.
service(JspServlet.java:164)
at org.apache.jasper.servlet.JspServlet.serviceJspFile
(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service
(JspServlet.java, CompiledCode)
at
javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService
(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service
(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService
(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service
(ContextManager.java:743)
at org.apache.tomcat.service.connector.Ajp12Connection
Handler.processConnection(Ajp12ConnectionHandler.java:166)
at org.apache.tomcat.service.TcpWorkerThread.runIt
(PoolTcpEndpoint.java, Compiled Code)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run
(ThreadPool.java, Compiled Code)
at java.lang.Thread.run(Thread.java, Compiled Code)
Whoops!
It's also possible to add comments to your JSP code, as in the example above. JSP supports both single-line and multi-line comment blocks - take a look:
<%
// this is a single-line comment
/* and this is a
multi-line
comment */
%>
Like PHP and Perl, white space is ignored in JSP.
Finally, the statement which actually prints output to the browser - as you'll see, this is done using the "out" object. Since JSP is based on Java, and Java is an object-oriented language, most of your JSP statements will include object references such as this one.
Copyright Melonfire, 2000. All rights reserved.