Java
  Home arrow Java arrow Page 3 - The JSP Files (part 1): Purple Pigs In A Fruitbasket
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
JAVA

The JSP Files (part 1): Purple Pigs In A Fruitbasket
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2001-02-07


    Table of Contents:
  • The JSP Files (part 1): Purple Pigs In A Fruitbasket
  • Studying The Foundations
  • Java In A Teacup
  • Enter John Doe
  • Putting Two And Two Together
  • Basket Case
  • Alphabet Soup For The Soul

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    The JSP Files (part 1): Purple Pigs In A Fruitbasket - Java In A Teacup
    ( Page 3 of 7 )

    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 Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek