Home arrow Java & J2EE arrow Page 3 - The JSP Files (part 1): Purple Pigs In A Fruitbasket

Java In A Teacup - Java

Get to grips with Java Server Pages with this introductorytutorial and find out how to use one of the more powerful server-sidelanguages around. This first part explains the history and basics of JSPdocuments, and also illustrates variables, includes and the String object.

TABLE OF CONTENTS:
  1. The JSP Files (part 1): Purple Pigs In A Fruitbasket
  2. Studying The Foundations
  3. Java In A Teacup
  4. Enter John Doe
  5. Putting Two And Two Together
  6. Basket Case
  7. Alphabet Soup For The Soul
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 9
February 07, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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" at , 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> </font>
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(JspSe rvlet.java:152) at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.ja va:164) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java, Compiled Code) 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:79 7) at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743) at org.apache.tomcat.service.connector.Ajp12ConnectionHandler.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.

 
 
>>> More Java & J2EE Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: