Enterprise Systems: JEE Laboratory

fortunabrontideInternet and Web Development

Nov 13, 2013 (3 years and 5 months ago)

64 views


Enterprise Systems
: J
EE Laboratory


Part A: Installing Blazix

and running the Web Server


1.

Download the Blazix web server from here
:

http://www.blazix.com/
. Install the web server onto
your u: drive. We need to change the port that the web server is run
ning on from 80 to 8082, in
order to prevent any conflicts with Oracle which is running on port 80 in some of the labs. To do
so, open the web.ini file in the u:
\
blazix directory and change the server.port number on the third
line to 8082.




2.

Start the

web server by executing blxweb.cmd in your Blazix folder. The webfiles folder in the
Blazix folder is the directory that stores all the files to be served by your web server. Delete all
files currently located there and insert a single text file named ind
ex.html containing some plain
text. (You may wish to format your text so that it is displayed as HTML).




3.

Point your browser to http://localhost:8082/ and you will see your default page being served.
You can change the URL so that it uses your IP addr
ess (which you can find out using ipconfig
at the command prompt), so that your URL is now http://147.252.001.123:8082/ or something
similar. You can also use your host name instead of your IP address. Using this, view the pages
being served by your neighb
our.




4.

You can put more pages and subdirectories in your webfiles folder, which can then be viewed
using the full path:


http://localhost:8082/myfolder/mypage.html




Part
B
:
Running a servlet



5.

We are now going to create your first servlet
-

down
load
FirstServlet.java
. To compile you must
include the Blazix.jar file in your classpath, as this contains the required classes. You should
redirect your compiled cla
ss to the classes directory on your web server, as this is where Blazix
will look for them:


:/> javac
-
d u:
\
blazix
\
classes
-
classpath .;u:
\
blazix
\
blazix.jar FirstServlet.java



6.

Edit your web.ini file in your Blazix folder
-

this contains information o
n all your servlets. Put
the following lines at the end of the file:


servlet.name: first

servlet.first.className: FirstServlet

servlet.first.url: /first

The first line identifies the name of your servlet, the second line shows which compiled class
this se
rvlet uses, and the third line gives the URL of the servlet
-

note, these do not need to be
called "first", they can be given any name, once the same name is used in the HTML form.



7.

Stop and restart your web server so it can pick up the new web.ini. Y
ou can stop your server by
typing CTRL
-
C in the DOS window opened up for Blazix. Restart by executing blxweb.cmd.




8.

Download
FirstServlet.html

(Right Click, and S
ave Target As) and save it in your
u:
\
blazix
\
webfiles
\

folder. Then direct your browser to http://localhost:8082/FirstServlet.html.
This give you a HTML form which you can use to send information to your server side servlet.
note how the form in the HTML s
ource code gives the relative URL of the servlet to be executed
as the action attribute. Click on the button to see the servlet running.




9.

Repeat steps 5
-
8 for
Add
erServlet.java

and
SimpleAdder.html

(remember to download with
Right Click
-
> Save Target As). Remember to put the following files in your web.ini file.


servlet.name:

adder

servlet.adder.className: AdderServlet

servlet.adder.url: /adder


Part
C
:
Combining
servlet
s




10.

The following example uses three servlets to make a shopping cart example.

Download
Selection.java
,
Checkout.java

and
Weight.java

as well as the HTML
page,
ShoppingCart.html
.

Each servlet must be registered separately, as given below:

servlet.name: shopping

servlet.shopping.className: Selection

servlet.shopping.url:

/shopping


servlet.name: weight

servlet.weight.className: Weight

servlet.weight.url: /weight


servlet.name: checkout

servlet.checkout.className: Checkout

servlet.checkout.url: /checkout

Restart the server.

Notice how state is maintained between each of th
e three servlets using the built in sessions.



11.

Cookies allow state to be maintained for longer than normal sessions. In this example we want to
maintain a set of user preferences for longer than a single session
-

so we use cookies
-

once a
user ente
rs their name and preferred colours, they are written to a cookie. This information can
then be retreived from the cookie at a later date. This is how web applications like hotmail
remember who you are when you log in.


Download
CookieAdder.java
,
GetPreferences.java

and
ShowSum.java

as well as the HTML
page,
CookieAdder.html
.

Register each of the servlets, as shown here:

servlet.name: cookie_adder

servlet.cookie_a
dder.className: CookieAdder

servlet.cookie_adder.url: /cookie_adder


servlet.name: showsum

servlet.showsum.className: ShowSum

servlet.showsum.url: /showsum


servlet.name: pref

servlet.pref.className: GetPreferences

servlet.pref.url: /pref

Restart the serve
r.

Notice how the state is maintained even after a session has ended
-

to see this use the
application, close your browser and start again. You will not be prompted for your preferences a
second time.



12
.

Download
adder.zip

and extract the contents to the webfiles directory in your Blazix folder. Start
Blazix and direct your browser to http://localhost:8082/SimpleAdderX.html. Examine the code
to see how it differs from the s
ervlet equivalent.



Note that JSP

such as Adder.jsp

are just quick ways to write servlets
-

they are still compiled
with their classes going to the jspdir folder. All the objects available in the servlets (session,
request, response, out and others) are i
mplicitly available in the JSP.


1
3
.

The program
ReadFromURL.java

shows how Java code can be used to read data from URL with
HTTP GET using the Java HTTP classes.

Try
this out with the following:


:/> java ReadFromURL http://www.comp.dit.ie

:/> java ReadFromURL http://dictionary.reference.com/search?q=java



14
.

The program
SendToURL.j
ava

shows how to pass arguments to a servlet, or any HTTP server
side application using POST.


Try it out by creating a servlet/JSP that uses POST and then running:

:/> java SendToURL http://localhost/my_servlet a=arg1 b=arg2 c=arg3