









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










-^
Let’s look at our first simple interactive JSP examplenamed
helloJsp.jsp
. In his Hello User example, the HTML
page takes a user name from a HTML form and sends arequest to a JSP page, and JSP page generates adynamic HTML page based on the data which comeswith the request
-^
Basically, this JSP page takes a user name input from aWeb client and generates a dynamic page to greet theuser.
-^
The request may come from a Web form page request orfrom a query string following an URL address of this JSPpage. First, examine the Web form HTML file named index.html
placed in the
JSP
directory which is this Web
application ROOT directory under
webapps
.
Demo1
Please enter the user name :UserName : •^
Since index.html is the default html nameforTomcat that you even don’t need to specify theindex.html in the URL address of the browser.
-^
The page
helloJsp.jsp
is placed in the same directory as
index.html
and
WEB-INF
. All HTML tags can be used in a JSP
page since JSP is an extension of HTML. A JSP file can beplaced anywhere an HTML file can be placed. <%@ page import="java.util." info = “ This is a simplestJSP with Java code embedded” contentType=“text/html”*
%>
<%! Date today; %>
**<%-- This is a Simplest JSP (comment from JSP tag -- %> **
Hello <%= request.getParameter("userName")
%>!
**<% today = new Date(); %><%= today %> **
The second line is a JSP declaration element<%!... %> which tells the JSP engine to insert theenclosed java code into the generated Servlet javasource code somewhere outside of any method.
-^
Variable declarations or method declarations may beplaced here. A Date class variable (class reference)is declared here. Of course you can have an in-linevariable declaration with its assignment statementinstead of the wo separate scripting elements inabove JSP example.
You see two different comments here.<%--... %> is a JSP comment scripting elementwhich stays on the server while is aregular HTML comment which is sent back to theclient. The client can view the HTML comment withthe view-source option of Internet browsers.
-^
Everything not within JSP element tags will be sentto the client browser as literal data. The word “Hello”is displayed on the client browser screen followed bythe string returned from the next JSP expressionscripting element and the “!”.
The next JSP scripting element is scriptlet which issimply a Java Servlet code fragment. It is insertedinto the service() method of the generated Servletcode.
-^
Here, JSP instantiates a new instance of Date classand the current date is displayed in the nextexpression scripting element.
In this JSP you have seen two types of JSPconstructs: directive elements and scriptingelements (including expression scripting,declaration scripting, scriptlets scripting). We willdiscuss another type constructor, the action tagelement. soon.
-^
You can use the view option of the browser to view the sourceHTML code generated by JSP. You can see the HTMLcomments but you don’t see the JSP comments.
We can also use JSP expression language of the JSP 2.0 to rewrite
the helloJsp.jsp
as follows.
<%@ page isELIgnored = "false" %>
Hello, ${param['userName']}!
**** •^
where “
userName
” is the parameter name defined in
the HTML file.