Home arrow Java & J2EE arrow Page 4 - Developing JavaServer Pages

How to Create a JSP - Java

In this excerpt from chapter 4 of Joel Murach's Java Servlets and JSP, you'll learn how to develop a web application that consists of HTML pages and JavaServer Pages (JSPs). As you will see, JSPs work fine as long as the amount of processing that's required for each page is limited. When you complete this chapter, you should be able to use JSPs to develop simple web applications of your own.

  1. Developing JavaServer Pages
  2. The Code for the HTML Page that Calls the JSP
  3. Imitating HTML
  4. How to Create a JSP
  5. How to Use the Methods of the Request Object
  6. Retrieving Multiple Values
  7. How to Request a JSP
  8. Using Get and Post Methods
  9. Using the Post Method
  10. Managing Java Classes
  11. Class Location
By: Joel Murach
Rating: starstarstarstarstar / 65
May 12, 2004

print this article



Now that you have a general idea of how JSPs are coded, you're ready to learn some specific skills for creating a JSP. To start, you need to know more about coding scriptlets and expressions.

How to Code Scriptlets and Expressions

Figure 4 summarizes the information you need for coding scriptlets and expressions within a JSP. To code a scriptlet, for example, you code Java statements that end with semicolons within the JSP scriptlet tags. To code an expression, you code any Java expression that evaluates to a string. Since primitive data types like integers or doubles are automatically converted to strings, you can also use expressions that evaluate to these data types.

Figure 4: How to code scriptlets and expressions

The syntax for a JSP scriptlet

<% Java statements %>

The syntax for a JSP expression

<%= any Java expression that can be converted to a string %>

The syntax for getting a parameter from the implicit request object


Examples that use scriptlets and expressions

A scriptlet and expression that display the value of the firstName parameter

   String firstName = request.getParameter("firstName");

The first name is <%= firstName %>.

An expression that displays the value of the firstName parameter

The first name is <%= request.getParameter("firstName") %>.

Two scriptlets and an expression that display an HTML line 5 times

  int numOfTimes = 1;
  while (numOfTimes <= 5){
  <h1> This line is shown <%= numOfTimes %%> of 5 times in a JSP.</h1>


Within a scriptlet, you can code one or more complete Java statements. Because these statements are Java statements, you must end each one with a semicolon.

Within a JSP expression, you can code any Java expression that evaluates to a string. This includes Java expressions that evaluate to any of the primitive types, and it includes any object that has a toString method. Because a JSP expression is an expression, not a statement, you don't end it with a semicolon.

When you're coding a scriptlet or an expression, you can use any of the methods of the implicit request object. In this figure, only the getParameter method is used, but you'll learn about two more methods of the request object in the next figure.

In this figure, the first two examples show different ways that you can display the value of a parameter. The first example uses a scriptlet to return the value of the firstName parameter and store it in a String object. Then, this example uses an expression to display the value. In contrast, the second example uses an expression to display the value of the firstName parameter without creating the firstName object.

The last example in this figure shows how two scriptlets and an expression can be used to display an HTML line five times while a Java variable within the HTML line counts from 1 to 5. Here, the first JSP scriptlet contains the code that begins a while loop. Then, a line of HTML code uses a JSP expression to display the current value of the counter for the loop. And finally, the second scriptlet contains the code that ends the loop.

Remember: this is from chapter four of Joel Murach's Java Servlets and JSP (Mike Murach & Associates, ISBN 1890774189, 2003). Grab a copy at your favorite book store today!

 Buy this book now.

>>> More Java & J2EE Articles          >>> More By Joel Murach

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: