WebSphere 101

greasyservantInternet and Web Development

Jul 30, 2012 (5 years and 3 months ago)

290 views

IBM Websphere Studio Application Developer (WSAD):

“Hello World” Hands
-
On


1.

Start WSAD (Desktop > Java Tools >
WebSphere Studio>WSAD 5.1.2 work
-

H
drive
)


A
ssuming that you’re logged in with your course account and that you’d want to

save and retrieve your

Web
S
ph
ere projects from this location
.



2.

Create a new dynamic web project (File > New > Dynamic Web Project). Call it
MyProject1

and click
Finish
.





3.

If prompted to repair the server configuration, click
OK
.





4.

If prompted to switch perspective to “We
b Perspective,” click
Yes
.





5.

Expand the
MyProject1

folder on the Project Navigator pane and you should see
the following:




6.

With
MyProject1

selected, create a new HTML page (New > HTML/XHTML)
called
testing123.htm
. Click
Finish
. This will add the web
page to the project.
Note that the document is automatically added to the folder
/MyProject1/WebContent
. All dynamic contents that are to be served up from
this web app are to be placed under this folder. (More on this later.)





7.

Replace the default con
tent “Place content here” with “
Hello World Page Served
Up!
” Note that this is in the
Design

mode. One can switch to the
Source

view by
clicking on the Source tab at the bottom of the pane.



8.

Save the contents of the file (CTRL
-
S).



9.

Now, serve up the page

using the
WSAD Test Environment
. (Right click on the
testing123.htm

and select
Run on Server …
)





10.

You will be prompted with a window to choose the server that you wish to run
this document on. The first time you run this, you will be prompted to create

a
new server. Select the
WebSphere version 5.
1
, Test Environment
. Click
Next
.
You will see that the default port number for the server is
9080
. Click
Finish
.


11.




The server should start up (you will see many messages being echoed in the
Console pane in
the process) and eventually you will see a browser pane showing
the HTML page. That’s it! Note the URL of the browser window, i.e.,
http://localhost:9080/MyProject1/testing123.htm
.
You can tes
t this outside of
WSAD (e.g., in IE or Netscape) and it should work just fine.




12.

Miscellaneous notes: From the echoed messages, note on the last line that the
server is “open for e
-
business.” Also, note that the server is listening to the default
port o
f 9080. Lastly, you can confirm the “Running” status of the server by
clicking on the
Servers

tab at the bottom pane.





13.

Save the file
Login.htm

onto d:
\
temp.
Now, import the file i
nto Web Content
folder of MyProject1 (Select the
WebContent

folder of
MyProject1
, right click,
select
Import
, File System,
Browse

the
d:
\
Temp

folder, check the
Login.htm

file, and click
Finish
.





14.

Double click on the Login.htm file and note the statement

. (If you’re in the Design
mode, switch to the Source mode.) WSAD recognizes that the ACTION attribute
is referencing a program that does not exist. Thus, it gives a broken link warning.

15.

Now create a servlet, call it
LoginServlet

(File > New > Servlet).

Click
Finish
.
Note that by default the source file for the servlet goes to the folder
/MyProject1/JavaSource
.






16.

Replace the auto
-
generated contents with the following:


import

java.io.*;

import

javax.servlet.*;

import

javax.servlet.http.*;


public

cl
ass

LoginServlet

extends

HttpServlet

implements

Servlet

{


public

void

doGet(HttpServletRequest

req,

HttpServletResponse

resp)



throws

ServletException,

IOException

{



String

userName

=req.getParameter(
"userName"
);



String

password

=req.getParameter(
"pa
ssword"
);



String

outStr

=

"Hi "

+

userName

+

"<br>"
;



if

(userName.length()

<

5)




outStr

+=

"Your login info is incorrect. Try again."
;



else




outStr

+=

"Welcome back. Please buy something this time :)"
;



ServletOutputStream

out

=

resp.getOutputSt
ream();



out.println(outStr);


}



public

void

doPost(HttpServletRequest

req,

HttpServletResponse

resp)



throws

ServletException,

IOException

{



}


}



17.

Save the servlet (CTRL
-
S).

18.

Now restart the project. Click on servers options
at

the bottom
pane
and
r
ight
-
click

on
the
server.
Select
‘Restart Project’ and then click on ‘DefaultEAR’.





19.

Now, serve up the
Login.htm

page (Right click on the
Login.htm

and select
Run
on Server …
). Choose the
Use an existing server

option with the
WebSphere
v5.1

Test Enviro
nment
. Click
Finish
.






20.

Click
Login Now

button with the username “John”. (It doesn’t matter what you
type for password as the servlet doesn’t check it!) What do you get? Try username
“Billy” this time. You should get in this time.





21.

Let’s try debugging with WSAD. Introduce a bug by changing the
LoginServlet.java

class so that
userName

becomes
username

in the statement


String

userName

=req.getParameter(
"username"
);



22.

Now go back to the
Web Browser

pane (within WSAD)

and serve up the
Login.htm

again. You should get the
dreaded HTTP 500 (Internet server error)
message.



23.

Look at the Console pane and you will see the details of the error (you may need
to scroll up so that you get to see the origin of the actual error).

It is the ubiquitous
NullPointerException
and it occurred at line 13.


….

[3/22/04 21:51:17:242 CST] 76b44936 WebGroup E SRVE0026E: [Servlet
Error]
-
[LoginServlet]:
java.lang.NullPointerException



at LoginServlet.doGet(LoginServlet.java:
1
2
)

at javax.
servlet.http.HttpServlet.service(HttpServlet.java:740)

at
com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletIns
tance.java:110)





24.

Set a breakpoint at the beginning of the
doGet

method (double click on the right
edge stripe).





25.

This time, start the server in the Debug mode by right clicking on the
Login.htm
,
select
Debug on Server …





26.

Choose the existing server when prompted and choose
Restart

when given the
message that the server is not running in debug mode.





27.

You sho
uld see the debugger started with a new orientation of the windows. You
are in the
Debug Perspective
.





28.

Click the
Login Now

button to start the process. When prompted to
step into

the
“Step
-
by
-
Step Debug,” click
OK
.





29.

You are immediately brought to
the breakpoint set earlier. Now
Step Over

the
statement and you will see that the variable
userName

is null. Step Over a couple
more times and you get to the null pointer location.




30.

Now fix the bug (change the code (from username back to userName) dir
ectly in
the editor pane). Click on the
Web Browser

pane. Click on the Go back arrow.
Resubmit by clicking on
Login Now
. The serv
let should again work properly.


31.

Terminate the debug process (click on the square tool (
Terminate
) a few spaces
to the right of

Step Over
). You can switch back to the
Web Perspective

by click
on the
Globe/WWW

icon on the ri
ght most edge of the workspace.


32.

That’s all folks!