Τεχνολογίες Παγκόσμιου Ιστού 
5Ρ δΚΏΝξΞΡ
Διαδικαστικά 
Θα ήθελα να μιλήσω με τους παρακάτω στα τελευταία 10’ του 
μαθήματος 
petros_dv, tsakatik, mirtwza, giannis156, apostoli, kir.christos, 
john_efthi, gavriili, somagion, papeslis, gi0rikas, thanoskaratzas, 
redsoukas, ntogkasxr, thomadak
Διαδικαστικά 
• Παρακαλώ δείτε τους βαθμούς της 1η εργασίας και στείλτε στον 
Έλβις τυχόν αντιρρήσεις σας 
• Η 2η εργαστηριακή άσκηση θα συζητηθεί την δεύτερη ώρα σήμερα, 
θα δωθεί αναλυτικά το απόγευμα και θα πρέπει να παραδωθεί στις 
8.00 της Δευτέρας.
Διαδικαστικά 
Σημαντική βοήθεια για την συνέχεια του μαθήματος και ιδιαίτερα στο 
θέμα των εργασιών θα βρείτε στα 
http://stackoverflow.com 
http://www.roseindia.net
2Ρ ξργισίι 
• Μελέτησε το 4ο κεφάλαιο και προπάθησε το αντίστοιχο τεστ. 
• Ξεκίνα την εξοικοίωσή σου με το https://netbeans.org 
• Εγκατέστησε στο VM σου στον Ωκεανό 
• LAMP 
• http://tomcat.apache.org 
• Wordpress
LAMP
Being a Servlet 
Chapter 4
From HMTL to 
servlet class
What else?
Be carefull!! 
getServerPort(), getLocalPort() & getRemotePort()
Response now 
Use the response for I/O when 
• There is no JSP 
• No JSP is allowed 
• Do not want to use JSP 
• Do not send HTML
ΠιρΏδξΚγΟι
Redirect 
sendRedirect() method: 
if (worksForMe) { 
// handle the request 
} else { 
response.sendRedirect(“http://www.oreilly.com”); 
} 
Redirect is not dispatch
Being a Web Application 
Chapter 5
Protecting my e-mail address 
PrintWriter out = response.getWriter(); 
out.println(“mav@uth.gr”); 
<servlet> 
<servlet-name>BeerParamTests</servlet-name> 
<servlet-class>TestInitParams</servlet-class> 
<init-param> 
<param-name>adminEmail</param-name> 
<param-value>mav@uth.gr</param-value> 
</init-param> 
</servlet> 
out.println(getServletConfig().getInitParameter(“adminEmail”));
Servlet initializations and ServletConfig 
The Container (after running the constructor) 
• Creates a unique ServletConfig 
• Reads servlet init parameters 
• Gives them to ServletConfig 
• Passes ServletConfig to servlet’s init() method
Δηλαδή
Testing Servlet Config
Re-deploying a servlet 
• Edit DD 
• Shut and restart the container 
• You better be careful what you put as init-param
Give JSP servlet’s init params 
• As a request attribute
Give JSP servlet’s init params 
• Using Context
Context & 
Servlet init 
parameters
Context & Servlet init parameters 
Container 
• Reads DD and creates name/ 
value string pairs for each 
<context-param> 
• Creates a new instance of 
ServletContext 
• Passes (as reference) pairs to 
ServletContext 
JSP is a servlet
What else is in 
ServletContext?
We need a listener 
Which 
Listens for context initialization events 
Runs specific code before servlet starts serving 
Notice, there is no main()!
ServletContextListener
Make and use a context listener 
Create a listener class 
Put the class in WEB-INF/classes 
Put <listener> element in DD 
• Three classes (and one DD) 
ServletContextListener.java 
Dog.java 
ListenerTester.java
Listener 
class
Attribute class 
Context init parameters as the 
argument for the constructor 
servlet will 
get dog from context 
call getBreed() 
print the value of breed
Servlet class
The DD 
(web.xml in 
WEB-INF)
Compile and Deploy 
Compile the three classes 
Create a new web app in Tomcat 
Create ListenerTest and WEB-INF 
Put web.xml in WEB-INF 
Make a classes directory underneath. 
Create a directory structure that matches your package 
structure 
Put web.xml and the three compiled files into their space 
Shutdown and restart Tomcat
The whole story
Context 
& 
Servlet 
init 
parameters
Context 
& 
Servlet 
init 
parameters 
Container 
• Reads 
DD 
and 
creates 
name/value 
string 
pairs 
for 
each 
<context-­‐param> 
• Creates 
a 
new 
instance 
of 
ServletContext 
• Passes 
(as 
reference) 
pairs 
to 
ServletContext
What 
else 
is 
in 
ServletContext? 
You 
may 
get 
them 
but 
NOT 
set 
them 
later
Initializing 
your 
web 
application 
with 
an 
object 
After 
initializing 
with 
a 
string 
of 
course
We 
need 
a 
listener 
(a 
Java 
object) 
• That 
– Listens 
for 
context 
initialization 
events 
– Runs 
specific 
code 
before 
servlet 
starts 
serving 
Notice! 
There 
is 
no 
main()
ServletContextListener
Listen 
to 
the 
Dog
Make 
and 
use 
a 
context 
listener 
• Create 
a 
listener 
class 
• Put 
the 
class 
in 
WEB-­‐INF/classes 
• Put 
<listener> 
element 
in 
DD 
• Three 
classes 
(and 
one 
DD) 
– ServletContextListener.java 
– Dog.java 
– ListenerTester.java
Listener 
class
Attribute 
class
Servlet 
class
The 
DD 
(the 
web.xml 
in 
WEB-­‐INF)
Review
The 
whole 
story 
(In 
pictures)
You 
can 
listen 
all 
kinds 
of 
events
Attributes 
• Who 
can 
put 
(and 
remove)? 
• Who 
can 
see? 
• For 
how 
long? 
SCOPE
Attributes 
are 
not 
Parameters!
The 
3 
Scopes
The 
3 
Scopes 
(API)
Beware 
of 
Attributes 
(1/2)
Beware 
of 
Attributes 
(2/2)
Thread-­‐Safe 
Context 
Attributes
Lock 
the 
Context 
(not 
the 
Service)
Thread-­‐Safe 
Session 
Attributes
SingleThreadModel 
No 
two 
threats 
will 
execute 
concurrently 
in 
the 
servlet’s 
service 
method. 
Two 
choices 
for 
STM 
implementation
Only 
request 
attributes 
and 
local 
variables 
are 
thread-­‐safe 
• Instance 
variables 
aren’t 
thread-­‐safe 
• Classes 
aren’t 
thread-­‐safe
Request 
attributes 
& 
Request 
dispatching
What 
is 
wrong 
here?

Ch5 beeing an application