Learning JSP

Elements of JSP

Previous  Next


Whenever a .jsp is requested for the first time, the server does the following:

1. Translates the .jsp page into a servlet
2. Compiles the servlet into a class file
3. Executes the servlet (response is sent to the client)

Subsequent requests (as long as the .jsp page is unchanged) use the same loaded class file.

 

Anatomy of a JSP Page

A JSP page is a mixture of standard HTML tags, web page content, and some dynamic content that is specified using JSP constructs. Everything except the JSP constructs is called Template Text.

 

JSP Constructs

1. JSP Comments: Different from HTML comments

		<!-- an HTML comment -->
		<%-- a JSP comment --%>

JSP comments are used for documenting JSP code and are not visible client-side (using browser's View Source option) where as HTML comments are visible.

2. (JAVA) Expressions

		<%= some_java_expression %>

Example:
<%= new java.util.Date().toString() %>

Output of the expression is placed in the HTML template at the same location.

There are some pre-defined Java variables/objects available for use in expressions (provide access to important servlet functionality):

request
This is the same object as HttpServletRequest parameter in th get/post methods. Same methods (like, getParameter, getAttribute, etc) can be applied to it.

out
The servlet printwriter.

session
Same as servlet session object.

Example:

   <HTML>
    <HEAD>
     <TITLE>JSP Expressions: Predefined Objects</TITLE>
    </HEAD>
    <BODY>
     <H1>Using Predefined Objects</H1>
     <UL>
      <LI> Your Hostname: <%= request.getRemoteHost() %>
      <LI> Your Session ID: <%= session.getId() %>
      <LI> The value of INFO parameter: <%= request.getParameter("INFO") %>
    </BODY>
   </HTML>


EXERCISE:

Place above in a file called, Objects.jsp and point your browser to:

http://server.address/youraccount/Objects.jsp?INFO=DC+GoGo+is+funky

 

3. Scriptlets

Scriptlets are arbitrary pieces of Java code inserted in the page using the format:

		<% some_java_code %>

Example 1

   <HTML>
    <HEAD>
     <TITLE>JSP: Scriptlets</TITLE>
    </HEAD>
     <%
        String bgColor = request.getParameter("COLOR");
       
        if (bgColor == null)
           bgColor = "WHITE";
        
     %>
    <BODY BGCOLOR="<%= bgColor %>" >
     <H1>Example Scriptlet: Sets background color</H1>
       
    </BODY>
   </HTML>

 

EXERCISE

Enter the code above in a file called, bgcolor.jsp and run it using the following URLs:

http://server.address/youraccount/bgcolor.jsp?COLOR=FF0000
http://server.address/youraccount/bgcolor.jsp?COLOR=00FF00
http://server.address/youraccount/bgcolor.jsp

Example 2

   <HTML>
    <HEAD>
     <TITLE>JSP: Scriptlets 2</TITLE>
    </HEAD>
     <% String bgColor = request.getParameter("COLOR"); %>
       
     <% if (bgColor == null) { %>
           <BODY BGCOLOR="FFFFFF" >
     <% } else { %>
           <BODY BGCOLOR="<%= bgColor %>" >
     <% } %>
       
     <H1>Example Scriptlet: Conditionally sets background color</H1>
     <% if (bgColor == null) { %>
           You did not supply a color, I used white.
     <% } else { %>
           Here is the color you requested.
     <% } %>
       
    </BODY>
   </HTML>

EXERCISE

Enter the code above in a file called, bgcolor2.jsp and run it using the following URLs:

http://server.address/youraccount/bgcolor2.jsp?COLOR=FF0000
http://server.address/youraccount/bgcolor2.jsp?COLOR=00FF00
http://server.address/youraccount/bgcolor2.jsp


Previous  Next

Created by Deepak Kumar (dkumar@acm.org)