Home arrow Java arrow JavaServer Pages
JAVA

JavaServer Pages


If you've taken a look at J2EE and JavaServer Pages technology, and want to examine JSP more closely, you've come to the right place. This article, the first of two parts, is excerpted from chapter three of Beginning J2EE 1.4 From Novice to Professional, written by James L. Weaver, Kevin Mukhar, and Jim Crume (Apress, 2004; ISBN: 1590593413).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 10
November 03, 2005
TABLE OF CONTENTS:
  1. · JavaServer Pages
  2. · Writing JSP Pages
  3. · Scriptlets
  4. · Template Data
  5. · Try It Out: Deploying the Web Application in J2EE
  6. · Try It Out: Deploying the Web Application in Tomcat
  7. · Action Elements

print this article
SEARCH DEVARTICLES

JavaServer Pages
(Page 1 of 7 )

In the previous chapters, we briefly introduced the J2EE and JavaServer Pages (JSP) technologies; in this chapter, well start to take a much more detailed look at JSP.

JSP pages are components in a web, or J2EE, application that consist of HTML with Java code added to the HTML. You might ask, Whats so different about that? Ive been putting JavaScript into my HTML for years. The difference is that JavaScript runs on the client, whereas the code in a JSP runs on the server. JavaScript can only affect the particular page in which it is embedded; code in a JSP can access data across the entire web application.

In this chapter we will begin to look at how to create JSP pages for your web application. We will look at:

  • The basic structure of JSP pages, and how to write a JSP page
  • How to use directive, scripting, and action elements
  • How to access the implicit objects of the page
  • How servers translate and compile JSP pages
  • How to handle errors and exceptions
  • How to forward and include pages from a JSP page

     

Introduction to JSP

As components in a J2EE application, JSP pages run on a server and respond to requests from clients. These clients are usually users accessing the web application through a web browser. The protocol used by clients to call the HTML and JSP pages in our J2EE application is HTTP, the same protocol used by browsers to get HTML pages from a web server.

For the moment well concentrate on the basics of creating JSP pages, but well look at the underlying HTTP protocol in Chapter 5.

Developing JSP Pages

In order to create a JSP page that can respond to client requests, there are a number of things we need to do. Firstly, of course, we need to write the JSP page. At some point, this page is translated and compiled into a Java class. This can happen before the page is loaded to a server, or it can happen at the time the client makes a request. The page executes inside a JSP container. A container is a piece of software that loads and manages J2EE components, in this case JSP pages. This container can be part of the web server, or it can run separately from the web server.

We can divide this process into three steps:

  • CreationThe developer creates a JSP source file that contains HTML and embedded Java code.

     

  • DeploymentThe JSP is installed into a server. This can be a full J2EE server or a stand-alone JSP server.

     

  • Translation and compilationThe JSP container translates the HTML and Java code into a Java code source file. This file is then compiled into a Java class that is executed by the server. The class file created from the JSP is known as the JSP page implementation class.

     

Note that this last step can actually occur at various times. Even though it is listed last here, you can translate and compile the JSP prior to deployment, and deploy the class file directly. Compiling first allows us to catch and fix syntax errors in our code prior to deployment. Alternatively, the JSP container can compile the JSP when it is deployed to the server. Finally, the usual process is that when the first request is made for the JSP, the server translates and compiles the JSP. This is known as translation at request time.

Basic JSP Lifecycle

Once compilation is complete, the JSP lifecycle has these phases:

  • Loading and instantiationThe server finds the Java class for the JSP page and loads it into the Virtual Machine. After the class is loaded, the JVM creates one or more instances of the page. This can occur right after loading, or it can occur when the first request is made.

     

  • InitializationThe JSP page is initialized. If you need to execute code during initialization, you can add a method to the page that will be called during initialization.

     

  • Request processingThe page responds to a request. After performing its processing, a response is returned to the client. The response consists solely of HTML tags or other data; none of the Java code is sent to the client.

     

  • End of lifeThe server stops sending requests to the JSP. After all current requests are finished processing, any instances of the class are released. If you need code to execute and perform any cleanup actions, you can implement a method that will be called before the class instance is released.

When a client sends a request for a JSP, the web server gives the request to the JSP container, and the JSP container determines which JSP page implementation class should handle the request. The JSP container then calls a method of the JSP page implementation class that processes the request and returns a response through the container and web server to the client:

 

Although weve seen how JSP works, we havent yet addressed the question of why we need JSP. The JSP home page (http://java.sun.com/products/jsp/) says, Web developers and designers use JavaServer Pages technology to rapidly develop and easily maintain information-rich, dynamic web pages that leverage existing business systems. JSP pages can be rapidly developed and easily maintained because they are based on HTML and XML. Documents with markup such as HTML are easy to understand and there are many automated tools for dealing with HTML and XML documents. JSP pages are dynamic because they can contain Java code, which can process the request and tailor the response based on the request. All the power of Java sits behind every JSP page.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials