Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
The JSP Files (part 1): Purple Pigs In A Fruitbasket
Get to grips with Java Server Pages with this introductory tutorial, which covers variables, includes and the String object.

| 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" at http://www.melonfire.com/community/columns/trog/article.php?id=48,  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(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:797)
   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.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback