10 - OOP Web Solutions

jetmorebrisketSoftware and s/w Development

Aug 15, 2012 (4 years and 10 months ago)

226 views


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


OOP Web Solutions


Creating a Web App in NetBeans

Using JSPs for Presentation

Lesson 10


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
2



Static vs. Dynamic Web Applications

Using HTML we can create
static

web pages, i.e. the content
remains the same each time the page is loaded. While these are
easy to develop, they do not update themselves automatically (e.g.
from a database) so the content usually gets outdated.

Using JSPs (Java Server Pages) we can create
dynamic

web pages,
i.e. the content is generated on the fly, at the time when the page is
loaded. Usually dynamic web pages have a connection to a
database, such that updates in the database would automatically
result in the dynamic page being updated. These are called
database
-
driven

websites.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
3

JSPs

JSPs are
server
-
side

components, i.e. they are hosted on a
Web
Server
, and receive requests from web browsers on client computers.
The first time a request is received, the JSP is converted into a
Servlet
.
Every time a request is received, the
Servlet

is run on the server, and
the formulated response is then sent back to the client computer.



MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
4

JSPs

A JSP page is a mixture of normal HTML code (which generates the
static content of the page), as well as JSP fragments (which generate
the dynamic content of the page).


Since we have said that JSP is server
-
side, JSPs need to lie on a web
server. There are different web servers available, the 2 most
commonly used for JSPs are
Apache Tomcat

and
Glassfish
, which are
very easily integrated into
NetBeans
.


We’ll start creating our first Web Application in the next slides…



MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
5

Web Application using
NetBeans

We choose “
Java Web
” from the Categories section and “
Web
Application
” from the Projects section.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
6

Web Application using
NetBeans

In the next steps we choose a name for our application, and
choose the server we’re going to use.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
7

Web Application using
NetBeans

NetBeans

creates the following project for us:

index.jsp

is the welcome page, created by
NetBeans

automatically

The
WebPages

folder will hold the JSP files

Normal Java classes will be in the Source
Packages


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
8

Web Application using
NetBeans

We can run our Web
Application. When we run,
the application is deployed
on the web server, and
opened in the default
browser. By default it should
simply display a welcome
message.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
9

JSP Fragments

Declaration:
To declare variables and methods

<%
!

int

i

= 0;
%>


Expression:
Insert values directly to output stream

<%
=

5+4

%>


<%

code fragment

%>


<%
@

page import=“
java.util.List
"
%>

<%
--

comment
--
%>

Comment:
Not visible when page is displayed

Import
: Used to import classes/packages

Scriptlet
:
Any Java Code, usually IF, Loops


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
10

An Example

<html>

<body>


<h3>The date today is: </h3>


<!
--

Display today's date
--
>


<%= new Date() %>




<!
--

Skip Line
--
>


<br><br>


<!
--

Declare Counter
--
>


<%! int counter = 1; %>




<!
--

Loop to display 20 numbers
--
>


<% while (counter <= 20) { %>




<%= counter %>


<br>


<% counter++; } %>




</body>

</html>


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
11



Activity

Create a web page which displays 20 random numbers from 1
to 100. At the bottom of the page display the total of the
numbers.

?


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
12

Passing Parameters

From within JSP fragments we have access to an object called
request
. If one JSP calls another, this is the object which is passed.
Therefore in it we can store
request parameters
.


Have a look at the following form:

<form method="get">


Name:


<input type="text" name="name" size="20"/> <
br
><
br
>


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

</form>

Since the form tag has no action attribute, clicking the submit button
would only cause the current page to reload. The only difference
would be that in the request object now we would have a parameter
called “
name
”, with its value depending on what the user entered.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
13

Passing Parameters

<body>


<h1>Hello Visitor!</h1>



<form method="get">


Name:


<input type="text" name="name" size="20"/> <
br
><
br
>


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


</form>



<% if ((
request.getParameter
("name") != null) &&


(
request.getParameter
("name").length() > 0))


{ %>



Welcome <%=
request.getParameter
("name")%>


<%}


%>


</body>

Can you deduce
what the code
on the right is
doing?

What would the value of
request.getParameter
("name")
be the first time the page
loads? And what would be its value after the user clicks submit and the same page
is reloaded?


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
14

Calling another JSP

<body>


<h1>Hello Visitor!</h1>



<form method="get“
action=“another.jsp”
>


Name:


<input type="text" name="name" size="20"/>



<
br
><
br
>


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


</form>

</body>

We can implement
something similar to
the code in previous
slide by having just
the form in one JSP
page, and using the
action attribute to
let another page
process the
parameters…

<body>


<% if ((
request.getParameter
("name") != null) &&


(
request.getParameter
("name").length() > 0))


{ %>


Welcome <%=
request.getParameter
("name")%>


<%}


%>

</body>

index.jsp

another.jsp


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
15



Activity

Create a web page (
index.jsp
) with a form which asks the user
to input a random number from 1
-
10. The action of the form
should redirect to
result.jsp
.

?

result.jsp should generate a random number from 1
-
10. If this
matches with the one the user would have input, tell the user
that he won.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
16

What have you learnt today?

Questions


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
17

References



http://www.sharpened.net/helpcenter/answers/static_and_dynamic_web_pages




http://www.oracle.com/technetwork/java/javaee/jsp/index.html




http://www.jsptut.com/




http://www.apl.jhu.edu/~hall/java/Servlet
-
Tutorial/