Docsity

First Simple Interactive JSP example, Lecture notes of Programming Languages

Toggle sidebar
bg1
First Simple Interactive JSP example
• Let’s look at our first simple interactive JSP example
named helloJsp.jsp. In his Hello User example, the HTML
page takes a user name from a HTML form and sends a
request to a JSP page, and JSP page generates a
dynamic HTML page based on the data which comes
with the request
• Basically, this JSP page takes a user name input from a
Web client and generates a dynamic page to greet the
user.
• The request may come from a Web form page request or
from a query string following an URL address of this JSP
page. First, examine the Web form HTML file named
index.html placed in the JSP directory which is this Web
application ROOT directory under webapps.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download First Simple Interactive JSP example and more Lecture notes Programming Languages in PDF only on Docsity!

First Simple Interactive JSP example

-^

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

.

First Simple Interactive JSP example

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.

First Simple Interactive JSP example

First Simple Interactive JSP example

-^

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 %> **

First Simple Interactive JSP example

•^

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.

First Simple Interactive 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 “!”.

First Simple Interactive JSP example

•^

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.

First Simple Interactive JSP example

•^

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.

First Simple Interactive JSP example

-^

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.

First Simple Interactive JSP example

•^

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.