Lab #06 Java Servlets

cabbagewheatInternet και Εφαρμογές Web

13 Νοε 2013 (πριν από 3 χρόνια και 5 μήνες)

107 εμφανίσεις

IVE(TY) Mobile Computing


LAB06


Java Servlets

_____________________________________________________________________________________________________

_____________________________________________________________________________
_____

File: MCOMP
\
Lab06.DOC




Page:
1


Lab #06



Java Servlets

_____________________________________________________________________


This laboratory session is used to practice the Java Servlets as middleware technology
in Tomcat Web Server. The concepts of the materials are based on the lec
ture given in
class.

_____________________________________________________________________


Set up classpath


Add the following to your user classpath:

C:
\
Apps
\
tomcat
\
common
\
lib
\
servlet
-
api.jar;


Copy the LabServlet



From

Server
:
LabServlet




To:

C:
\
Ap
ps
\
tomcat
\
webapps
\
LabServlet




HelloWorldServlet1

Compilation


Change directory to C:
\
Apps
\
tomcat
\
webapps
\
LabServlet
\
WEB
-
INF
\
classes



Compile:


javac HelloWorldServlet1.java


HelloWorldServlet2 and HelloWorldServlet3 Compilation



1.
You will find a subdirectory under
WEB
-
INF
\
classes
called





.


2. Change directory to …
\
WEB
-
INF
\
classes
\
labservlet



3. View
HelloWorldServlet2.java and HelloWorldServlet3.java




View HelloWorldServlet2.java which belongs to package
labservlet




View Hel
loWorldServlet3.java which belongs to package
labservlet

and has
doPost() method by using FORM.



4. Compile
HelloWorldServlet2.java and HelloWorldServlet3.java:




java
c


d

.. *.java

(Note: Check the date/time of classes)

IVE(TY) Mobile Computing


LAB06


Java Servlets

_____________________________________________________________________________________________________

_____________________________________________________________________________
_____

File: MCOMP
\
Lab06.DOC




Page:
2


Servlet Mapping in web.xml


Change directory to C:
\
Apps
\
tomcat
\
webapps
\
LabServlet
\
WEB
-
INF





<servlet>


<servlet
-
name>HelloWorldServlet1</servlet
-
name>


<servlet
-
class>HelloWorldServlet1</servlet
-
class>

</servlet>


<servlet>


<servlet
-
name>HelloServlet2</servlet
-
n
ame>


<servlet
-
class>
labservlet
.HelloWorldServlet2</servlet
-
class>

</servlet>


NOTE: What does
labservlet

refer to in labservlet.HelloWorldServlet2?


<servlet>


<servlet
-
name>HelloServlet3</servlet
-
name>


<servlet
-
class>labservlet.HelloWorldServlet3</servl
et
-
class>

</servlet>



What will the
Servlet Classes

which the following patterns refer to?


<url
-
pattern>/HelloWorldServlet1</url
-
pattern>





<url
-
pattern>*.hello</url
-
pattern>








<url
-
pattern>/hello2</url
-
pattern>





<url
-
pattern>/hello3</url
-
pattern>







Start Tomcat Server


Run
Startup.bat

under C:
\
Apps
\
tomcat
\
bin


Try with Netscape Browser



http://localhost:8899/
LabServlet
/HelloWorldServlet1


http://localhost:8899/
LabServlet
/anyXXX.hello



http://localhost:8899/
LabServlet
/hello2


http:
//localhost:8899/
LabServlet
/hello3 (NOTE: doGet() at first then doPost())


IVE(TY) Mobile Computing


LAB06


Java Servlets

_____________________________________________________________________________________________________

_____________________________________________________________________________
_____

File: MCOMP
\
Lab06.DOC




Page:
3


Modify the web.xml to include the following mapping:


<servlet
-
mapping>


<servlet
-
name>
HelloWorldServlet1
</servlet
-
name>


<url
-
pattern
>
/servlet/
HelloWorldServlet1
</ur
l
-
pattern>

</servlet
-
mapping>


Try with Netscape Browser:



http://localhost:8899/LabServlet/
servlet/
HelloWorldServlet
1



Keep HelloWorldServlet3.java and make a copy called MorningHongKong.java


Display “Good Morning, HongKong!!” on Browser;


Change the

mapping in web.xml as
morningHK
such that, in netscape URL

http://localhost:8899/LabServlet/
morningHK


ServletLogin Application


Change directory to C:
\
Apps
\
tomcat
\
webapps
\
LabServlet
\
WEB
-
INF
\
classes



Compile:


javac ServletLogin.java




Make Servlet Ma
pping in web.xml



<servlet>


<servlet
-
name>ServletLogin</servlet
-
name>


<servlet
-
class>ServletLogin</servlet
-
class>


</servlet>



<servlet
-
mapping>


<servlet
-
name>ServletLogin</servlet
-
name>


<url
-
pattern>/servlet/Serv
letLogin</url
-
pattern>


</servlet
-
mapping>


Try with Netscape Browser:




http://localhost:8899/LabServlet/
servlet/ServletLogin




IVE(TY) Mobile Computing


LAB06


Java Servlets

_____________________________________________________________________________________________________

_____________________________________________________________________________
_____

File: MCOMP
\
Lab06.DOC




Page:
4


Session Applied


If you have wrong user id or password, it keeps on asking. However, you enter corre
ctly, it
will not ask any more within the session which means you go to
www.yahoo.com

and come
back to the login session it will not ask userid/password and only display the result screen
because the
cookie

stored logi
n details (userid and password) in USER_KEY.



HttpSession session = req.getSession(true);


String user = (String) session.getValue(USER_KEY);


if (user == null) {


// No user
-

create the form to prompt the user


login(out, uri);



return;

}



(NOTE: Use same string for
UserID

and
Password
, such as
123/123
.)




You are required to make the following changes:


Modify ServletLogin

Application to access

the web server for the userid and
password:



http://ictlab.tyict.vtc.edu.hk/~andylai /mobile/login_demo.php


login_demo.php


<?php

$name=$_GET['name'];

$pwd=$_GET['password'];

if ($name=="a
ndy
" && $pwd=="cat")


echo "OK";

else if

($name=="paul" && $pwd=="panda")


echo "OK";

else if ($name=="jacky" && $pwd=="doctor")


echo "OK";

else if ($name=="peter" && $pwd=="a1 b")


echo "OK";

else


echo "invalid";

?>


Hint: Please refer to Slides 26
-
28 of Chapter 5 (J2ME Networking)



*** THE END ***