Chapter: Internet & World Wide Web HOW TO PROGRAM - JavaServer Pages (JSP): Bonus for Java™ Developers

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Scripting - JavaServer

JavaServer Pages often present dynamically generated content as part of an XHTML document sent to the client in response to a request.



JavaServer Pages often present dynamically generated content as part of an XHTML document sent to the client in response to a request. In some cases, the content is static, but is output only if certain conditions are met during a request (such as providing values in a form that submits a request). JSP programmers can insert Java code and logic in a JSP us-ing scripting.


1. Scripting Components


JSP scripting components include scriptlets, comments, expressions, declarations and es-cape sequences. This section describes each of these scripting components. Many of these scripting components are demonstrated in Fig. 31.4 at the end of Section 31.5.2.


Scriptlets are blocks of code delimited by <% and %>. They contain Java statements that the container places in method _jspService at translation time.


JSPs support three comment styles: JSP comments, XHTML comments and comments from the scripting language. JSP comments are delimited by <%-- and --%>. Such com-ments can be placed throughout a JSP, but not inside scriptlets. XHTML comments are delimited with <!-- and -->. These comments can be placed throughout a JSP, but not inside scriptlets. Scripting language comments are currently Java comments, because Java is the only JSP scripting language at the present time. Scriptlets can use Java’s single-line comments (delimited by/ and /) and multiline comments (delimited by /* and */).


JSP comments and scripting-language comments are ignored and do not appear in the response to a client. When clients view the source code of a JSP response, they will see only the XHTML comments in the source code. The different comment styles are useful for sep-arating comments that the user should be able to see from comments that document logic processed on the server.

A JSP expression, delimited by <%= and %>, contains a Java expression that is evalu-ated when a client requests the JSP containing the expression. The container converts the result of a JSP expression to a String object, then outputs the String as part of the response to the client.


Declarations (delimited by <%! and %>) enable a JSP programmer to define variables and methods. Variables become instance variables of the servlet class that represents the translated JSP. Similarly, methods become members of the class that represents the trans-lated JSP. Declarations of variables and methods in a JSP use Java syntax. Thus, a variable declaration must end in a semicolon, as in

<%! int counter = 0; %>

Special characters or character sequences that the JSP container normally uses to delimit JSP code can be included in a JSP as literal characters in scripting elements, fixed template data and attribute values using escape sequences. Figure 31.3 shows the literal character or characters and the corresponding escape sequences and discusses where to use the escape sequences.


2. Scripting Example


The JSP of Fig. 31.4 demonstrates basic scripting capabilities by responding to get re-quests. The JSP enables the user to input a first name, then outputs that name as part of the response. Using scripting, the JSP determines whether a firstName parameter was passed to the JSP as part of the request; if not, the JSP returns an XHTML document con-taining a form through which the user can input a first name. Otherwise, the JSP obtains the firstName value and uses it as part of an XHTML document that welcomes the user to JavaServer Pages.


    <?xml version = "1.0"?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"



    <!-- Fig. 10.4: welcome.jsp -->

    <!-- JSP that processes a "get" request containing data. -->


    <html xmlns = "">


      <!-- head section of document -->


      <title>Processing "get" requests with data</title>


        <!-- body section of document -->


<% // begin scriptlet


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


if ( name != null ) {


%> <%-- end scriptlet to insert fixed template data --%>



     Hello <%= name %>, <br />

     Welcome to JavaServer Pages!



<% // continue scriptlet


}    // end if

else {


%> <%-- end scriptlet to insert fixed template data --%>


     <form action = "welcome.jsp" method = "get">

     <p>Type your first name and press Submit</p>


     <p><input type = "text" name = "firstName" />

     <input type = "submit" value = "Submit" />




<% // continue scriptlet


}    // end else

        %> <%-- end scriptlet --%>


          </html>     <!-- end XHTML document -->



Fig. 31.4 Scripting a JavaServer Page (welcome.jsp)


Notice that the majority of the code in Fig. 31.4 is XHTML markup (i.e., fixed tem-plate data). Throughout the body element are several scriptlets (lines 17–23, 30–35 and 45–49) and a JSP expression (line 26). Note that three comment styles appear in this JSP.


The scriptlets define an if/else structure that determines whether the JSP received a value for the first name as part of the request. Line 19 uses method getParameter of JSP implicit object request (an HttpServletRequest object) to obtain the value for parameter firstName and assigns the result to variable name. Line 21 determines if name is not null, (i.e., a value for the first name was passed to the JSP as part of the request). If this condition is true, the scriptlet terminates temporarily so the fixed template data at lines 25–28 can be output. The JSP expression in line 26 outputs the value of vari-able name (i.e., the first name passed to the JSP as a request parameter. The scriptlet con-tinues at lines 30–35 with the closing curly brace of the if structure’s body and the beginning of the else part of the if/else structure. If the condition at line 21 is false, lines 25–28 are not output. Instead, lines 37–43 output a form element. The user can type a first name in the form and press the Submit button to request the JSP again and execute the if structure’s body (lines 25–28).


To test Fig. 31.4 in Tomcat, copy welcome.jsp into the jsp directory created in Section 31.3. Open your Web browser and enter the following URL to test wel-come.jsp:




When you first execute the JSP, it displays the form in which you can enter your first name, because the preceding URL does not pass a firstName parameter to the JSP. After you submit your first name, your browser should appear as shown in the second screen cap-ture of Fig. 31.4. Note: As with servlets, it is possible to pass get request arguments as part of the URL. The following URL supplies the firstName parameter to welcome.jsp:



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Copyright © 2018-2020; All Rights Reserved. Developed by Therithal info, Chennai.