Ch5 beeing an application

Click here to load reader

  • date post

  • Category


  • view

  • download


Embed Size (px)


Context, Config, listeners, ...

Transcript of Ch5 beeing an application

  • 1. 5

2. 10 petros_dv, tsakatik, mirtwza, giannis156, apostoli, kir.christos,john_efthi, gavriili, somagion, papeslis, gi0rikas, thanoskaratzas,redsoukas, ntogkasxr, thomadak 3. 1 2 , 8.00 . 4. http://stackoverflow.com 5. 2 4 . VM LAMP Wordpress 6. LAMP 7. Being a ServletChapter 4 8. From HMTL toservlet class 9. What else? 10. Be carefull!!getServerPort(), getLocalPort() & getRemotePort() 11. 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 12. 13. RedirectsendRedirect() method:if (worksForMe) {// handle the request} else {response.sendRedirect(;}Redirect is not dispatch 14. Being a Web ApplicationChapter 5 15. Protecting my e-mail addressPrintWriter out = response.getWriter();out.println([email protected]);[email protected](getServletConfig().getInitParameter(adminEmail)); 16. Servlet initializations and ServletConfigThe Container (after running the constructor) Creates a unique ServletConfig Reads servlet init parameters Gives them to ServletConfig Passes ServletConfig to servlets init() method 17. 18. Testing Servlet Config 19. Re-deploying a servlet Edit DD Shut and restart the container You better be careful what you put as init-param 20. Give JSP servlets init params As a request attribute 21. Give JSP servlets init params Using Context 22. Context &Servlet initparameters 23. Context & Servlet init parametersContainer Reads DD and creates name/value string pairs for each Creates a new instance ofServletContext Passes (as reference) pairs toServletContextJSP is a servlet 24. What else is inServletContext? 25. We need a listenerWhichListens for context initialization eventsRuns specific code before servlet starts servingNotice, there is no main()! 26. ServletContextListener 27. Make and use a context listenerCreate a listener classPut the class in WEB-INF/classesPut element in DD Three classes (and one DD) 28. Listenerclass 29. Attribute classContext init parameters as theargument for the constructorservlet willget dog from contextcall getBreed()print the value of breed 30. Servlet class 31. The DD(web.xml inWEB-INF) 32. Compile and DeployCompile the three classesCreate a new web app in TomcatCreate ListenerTest and WEB-INFPut web.xml in WEB-INFMake a classes directory underneath.Create a directory structure that matches your packagestructurePut web.xml and the three compiled files into their spaceShutdown and restart Tomcat 33. The whole story 34. Context&Servletinitparameters 35. Context&ServletinitparametersContainer ReadsDDandcreatesname/valuestringpairsforeach CreatesanewinstanceofServletContext Passes(asreference)pairstoServletContext 36. WhatelseisinServletContext?YoumaygetthembutNOTsetthemlater 37. InitializingyourwebapplicationwithanobjectAfterinitializingwithastringofcourse 38. Weneedalistener(aJavaobject) That Listensforcontextinitializationevents RunsspecificcodebeforeservletstartsservingNotice!Thereisnomain() 39. ServletContextListener 40. ListentotheDog 41. Makeanduseacontextlistener Createalistenerclass PuttheclassinWEB-INF/classes PutelementinDD Threeclasses(andoneDD) 42. Listenerclass 43. Attributeclass 44. Servletclass 45. TheDD(theweb.xmlinWEB-INF) 46. Review 47. Thewholestory(Inpictures) 48. Youcanlistenallkindsofevents 49. Attributes Whocanput(andremove)? Whocansee? Forhowlong?SCOPE 50. AttributesarenotParameters! 51. The3Scopes 52. The3Scopes(API) 53. BewareofAttributes(1/2) 54. BewareofAttributes(2/2) 55. Thread-SafeContextAttributes 56. LocktheContext(nottheService) 57. Thread-SafeSessionAttributes 58. SingleThreadModelNotwothreatswillexecuteconcurrentlyintheservletsservicemethod.TwochoicesforSTMimplementation 59. Onlyrequestattributesandlocalvariablesarethread-safe Instancevariablesarentthread-safe Classesarentthread-safe 60. Requestattributes&Requestdispatching 61. Whatiswronghere?