Ch5 beeing an application

63
Τεχνολογίες Παγκόσμιου Ιστού 5η διάλεξη

description

Context, Config, listeners, ...

Transcript of Ch5 beeing an application

Page 1: Ch5   beeing an application

Τεχνολογίες Παγκόσμιου Ιστού5η διάλεξη

Page 2: Ch5   beeing an application

Διαδικαστικά

Θα ήθελα να μιλήσω με τους παρακάτω στα τελευταία 10’ του μαθήματος

petros_dv, tsakatik, mirtwza, giannis156, apostoli, kir.christos, john_efthi, gavriili, somagion, papeslis, gi0rikas, thanoskaratzas, redsoukas, ntogkasxr, thomadak

Page 3: Ch5   beeing an application

Διαδικαστικά

• Παρακαλώ δείτε τους βαθμούς της 1η εργασίας και στείλτε στον Έλβις τυχόν αντιρρήσεις σας

• Η 2η εργαστηριακή άσκηση θα συζητηθεί την δεύτερη ώρα σήμερα, θα δωθεί αναλυτικά το απόγευμα και θα πρέπει να παραδωθεί στις 8.00 της Δευτέρας.

Page 4: Ch5   beeing an application

Διαδικαστικά

Σημαντική βοήθεια για την συνέχεια του μαθήματος και ιδιαίτερα στο θέμα των εργασιών θα βρείτε στα

http://stackoverflow.com

http://www.roseindia.net

Page 5: Ch5   beeing an application

2η εργασία• Μελέτησε το 4ο κεφάλαιο και προπάθησε το αντίστοιχο τεστ.

• Ξεκίνα την εξοικοίωσή σου με το https://netbeans.org

• Εγκατέστησε στο VM σου στον Ωκεανό

• LAMP

• http://tomcat.apache.org

• Wordpress

Page 6: Ch5   beeing an application

LAMP

Page 7: Ch5   beeing an application

Being a ServletChapter 4

Page 8: Ch5   beeing an application

From HMTL to servlet class

Page 9: Ch5   beeing an application

What else?

Page 10: Ch5   beeing an application

Be carefull!!

getServerPort(), getLocalPort() & getRemotePort()

Page 11: Ch5   beeing an application

Response nowUse the response for I/O when • There is no JSP • No JSP is allowed • Do not want to use JSP • Do not send HTML

Page 12: Ch5   beeing an application

Παράδειγμα

Page 13: Ch5   beeing an application

Redirect

sendRedirect() method:

if (worksForMe) { // handle the request } else { response.sendRedirect(“http://www.oreilly.com”); }

Redirect is not dispatch

Page 14: Ch5   beeing an application

Being a Web ApplicationChapter 5

Page 15: Ch5   beeing an application

Protecting my e-mail addressPrintWriter out = response.getWriter(); out.println(“[email protected]”);

<servlet> <servlet-name>BeerParamTests</servlet-name> <servlet-class>TestInitParams</servlet-class>

<init-param> <param-name>adminEmail</param-name> <param-value>[email protected]</param-value> </init-param> </servlet>

out.println(getServletConfig().getInitParameter(“adminEmail”));

Page 16: Ch5   beeing an application

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

Page 17: Ch5   beeing an application

Δηλαδή

Page 18: Ch5   beeing an application

Testing Servlet Config

Page 19: Ch5   beeing an application

Re-deploying a servlet

• Edit DD

• Shut and restart the container

• You better be careful what you put as init-param

Page 20: Ch5   beeing an application

Give JSP servlet’s init params

• As a request attribute

Page 21: Ch5   beeing an application

Give JSP servlet’s init params• Using Context

Page 22: Ch5   beeing an application

Context & Servlet init parameters

Page 23: Ch5   beeing an application

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

Page 24: Ch5   beeing an application

What else is in ServletContext?

Page 25: Ch5   beeing an application

We need a listener

Which Listens for context initialization events Runs specific code before servlet starts serving

Notice, there is no main()!

Page 26: Ch5   beeing an application

ServletContextListener

Page 27: Ch5   beeing an application

Make and use a context listenerCreate 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

Page 28: Ch5   beeing an application

Listener class

Page 29: Ch5   beeing an application

Attribute classContext init parameters as the argument for the constructor

servlet will get dog from context call getBreed() print the value of breed

Page 30: Ch5   beeing an application

Servlet class

Page 31: Ch5   beeing an application

The DD (web.xml in WEB-INF)

Page 32: Ch5   beeing an application

Compile and DeployCompile 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

Page 33: Ch5   beeing an application

The whole story

Page 34: Ch5   beeing an application
Page 35: Ch5   beeing an application

Context  &  Servlet  init  parameters

Page 36: Ch5   beeing an application

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

Page 37: Ch5   beeing an application

What  else  is  in  ServletContext?

You  may  get  them  but  NOT  set  them

later

Page 38: Ch5   beeing an application

Initializing    your  web  application    

with  an  object  

After  initializing    with  a  string  of  course

Page 39: Ch5   beeing an application

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()

Page 40: Ch5   beeing an application

ServletContextListener

Page 41: Ch5   beeing an application

Listen  to  the  Dog

Page 42: Ch5   beeing an application

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

Page 43: Ch5   beeing an application

Listener  class

Page 44: Ch5   beeing an application

Attribute  class

Page 45: Ch5   beeing an application

Servlet  class

Page 46: Ch5   beeing an application

The  DD  (the  

web.xml  in  WEB-­‐INF)

Page 47: Ch5   beeing an application

Review

Page 48: Ch5   beeing an application

The  whole  story  (In  pictures)

Page 49: Ch5   beeing an application

You  can    listen    

all  kinds    of  events

Page 50: Ch5   beeing an application

Attributes

• Who  can  put  (and  remove)?  • Who  can  see?  • For  how  long?  

                   SCOPE

Page 51: Ch5   beeing an application

Attributes  are  not  Parameters!

Page 52: Ch5   beeing an application

The  3  Scopes

Page 53: Ch5   beeing an application

The  3  Scopes  (API)

Page 54: Ch5   beeing an application

Beware  of  Attributes  (1/2)

Page 55: Ch5   beeing an application

Beware  of  Attributes  (2/2)

Page 56: Ch5   beeing an application

Thread-­‐Safe  Context  Attributes

Page 57: Ch5   beeing an application
Page 58: Ch5   beeing an application

Lock  the  Context  (not  the  Service)

Page 59: Ch5   beeing an application

Thread-­‐Safe  Session  Attributes

Page 60: Ch5   beeing an application

SingleThreadModel  

Two  choices  for  STM  implementation

No  two  threats  will  execute  concurrently  in  the  servlet’s  service  method.  

Page 61: Ch5   beeing an application

Only  request  attributes  and  local  variables  are  thread-­‐safe

• Instance  variables  aren’t  thread-­‐safe  • Classes  aren’t  thread-­‐safe

Page 62: Ch5   beeing an application

Request  attributes  &  Request  dispatching

Page 63: Ch5   beeing an application

What  is  wrong  here?