49
Unit 2 Presentation by AshishSingh T Bhatia Lecturer, CTI [email protected] HTTP Basics Servlet Basics Prepared using

Http and Servlet basics

Embed Size (px)

Citation preview

  • 1.Prepared usingUnit 2 HTTP Basics Servlet BasicsPresentation byAshishSingh T BhatiaLecturer, [email protected]

2. Assignment 11. Explain Internet and WWW.2. Explain the basic structure of HTML document withbrief history.3. What is JavaScript? Describe its evolution.4. Explain different type of datatypes available and usein JS.5. Write shortnote on DOM.6. List and explain different browsers objects.7. Explain all form elements.8. List and explain JavaScript Event handlers.9. Explain in detail Form Object and its use.10. Explain inbuilt objects in JavaScript. [ String and Date]I Ashish Bhatia 3. Assignment 111. Explain the functions and its use in JavaScript.12. Explain following with examples1. typeof2. instanceof3. prototype4. constructor5. caller6. toString7. valueOf13. Desribe the object creation in JavaScript with example.II Ashish Bhatia 4. HTTP HTTP stands for Hyper Text Transfer Protocol HTTP/0.9 HTTP/1.0 HTTP/1.1RFC ? RFC 1945RFC 2616 HTTP is Aplication Level Protocol for distributed, collaborative,hypermedia information systems. Generic stateless protocol which can be used for manytasks beyond its use for hypertext . Use by WWW since 1990. Use reliable TCP / IP connection.1 Ashish Bhatia 5. URL , URI , URN URL : Uniform Resource Locator URI : Uniform Resource Identifier URN : Uniform Resource Name2 Ashish Bhatia 6. URL , URI , URN Web Identifiers Classical View [ URI ] Identifier might specify location of resource ==> URL http:// identifies a resource via a representation of itsprimary access mechanism(e.g., its network "location"), Its name independent of location ==> URN urn:isbn :n-nn-nnnnnn-n3 Ashish Bhatia 7. Move Back to History4Ashish Bhatia 8. Move Back to History5Ashish Bhatia 9. Understanding a Bit here Take place of CGI scripts Run inside a JVM [ Making it safe and portable ] Operate solely in domain of server unlike applet they do not require support for Java on web Browser. Efficient and Scalable : Threads are used for request. Support for servlet Standalone Servlet Engines Sun Java web Server, JigSaw Server by W3C,Netscape Eneterprise Server, Lotus Domino Go Server6 Ashish Bhatia 10. Understanding a Bit here Add on servlet Engines Jserv [ Apache ] JRun [ Live Software ] WebSphere [ IBM ] ServletExec [ Atlantas ] Embeddable Servlet Engines7Ashish Bhatia 11. Coming back to HTTP Always client initiates a transaction by establishing a connection and sending HTTP request. Server has no way to callback or connect client. HTTP transaction begins with : Request from the client browser. Ends with Response from the server. Request Header consist of three parts Method URI Protocol Version Request Headers Entity Body8 Ashish Bhatia 12. Coming back to HTTP9 Ashish Bhatia 13. Coming back to HTTP10Ashish Bhatia 14. Coming back to HTTP11Ashish Bhatia 15. Coming back to HTTP12Ashish Bhatia 16. Coming back to HTTPServer process the request and send the response tothe client.HTTP/1.0 200 OK13Ashish Bhatia 17. Coming back to HTTP1xx: Informational - Request received,continuing process2xx: Success - The action was successfully received,understood, and accepted3xx: Redirection - Further action must be taken in orderto complete the request4xx: Client Error - The request contains bad syntax orcannot be fulfilled5xx: Server Error - The server failed to fulfill anapparently valid request14 Ashish Bhatia 18. Coming back to HTTPGETRetrieve information identified by Request-URIHEADSimilar to GET except that server will not return a message bodyPOST15 Ashish Bhatia 19. Coming back to HTTPPUTEnclosed entity be stored under the supplied Requested-URIIf entity exists it will be treated as modified version201 Created200 OK204 No ContentError Code501 Not Implemented16 Ashish Bhatia 20. Coming back to HTTPDELETE Delete the resource identified by the Request-URI 200 OK 202 accepted 204 No contentTRACEUsed to invoke a remote, application-layerloop-back of the request message. The finalrecipient of the request SHOULD reflect themessage received back to the client as theentity-body of a 200 (OK) response.17 Ashish Bhatia 21. Coming back to HTTPCONNECT Reserves for use with proxy that can dynamically switch to being a tunnel.18Ashish Bhatia 22. Prepared usingAre You Attentive ?How many methods we studied ?How request header looks ?How response header looks ? Lets move to Servlet Basics19Ashish Bhatia 23. Servlet Application ArchitectureA servlet is a Java class that can be loaded dynamically into and run by a special web server.Servlet Container / Servlet Engine : Servlet aware web server20Ashish Bhatia 24. Servlet Application ArchitectureProblem and Solution21 Ashish Bhatia 25. How Servlet WorksServlet is loaded in ServletContainer first.Servlet then is forwared theuser request, process it,and returns the responseto the servlet containerwhich in turns send responseback to server.Servlet stays in memorywaiting for other requestsUnloaded ??? Shortage of mem22 Ashish Bhatia 26. Tomcat as Servlet ContainerDesigned by Sun MicrosystemHanded the code the Apache 1999 Included as Jakarta Project Its one of recognised Servlet Container andused world wide OpenSourceCurrent version 7.023Ashish Bhatia 27. Getting up with TomcatGenreally two options available for tomcat installation Liked by many .exe Zip folderWhere to get Tomcat ? http://tomcat.apache.org/ http://tomcat.apache.org/download-70.cgi24 Ashish Bhatia 28. Getting up with Tomcat25 Ashish Bhatia 29. Setting up using zip / tar.gz versionSetting up on any platform Extract the zip / tar.gz to desired place Assumption Extracted folder D:tomcat JDK : C:Program FilesJavajdk Set two environment variables CATALINA_HOME=D:tomcat JAVA_HOME=C:Program FilesJavajdkIf exe : Double click ==> Get installed as window service26 Ashish Bhatia 30. Moving ahead to directory27Ashish Bhatia 31. Directory detailsbin Contains files for starting / stopping tomcat serverconf Contains xml fileslib Contains jar fileslogstempwebapps Working directory mapped to localhost [ see later ]work28Ashish Bhatia 32. Program Structure ProjectJSP pages,WEB-INF static html pages,applet classesclasses libtags web.xml .class files Library archives . tag files29 Ashish Bhatia 33. Directory detailsHttp://localhost:8080/Add This will call up index page[Default Page] When called the servlet object is created from Add.class30 Ashish Bhatia 34. Understanding the javax.servlet31Ashish Bhatia 35. Understanding the javax.servlet : interfaces14 interfaces : 7 implemented provided by container ServletConfig ServletContext ServletRequest ServletResponse RequestDispatcher FilterChain FilterConfig32Ashish Bhatia 36. Understanding the javax.servlet : interfaces14 interfaces : 7 left for developer Servlet ServletRequestListener ServletRequestAttributeListener ServletContextListener ServletContextAttributeListener SingleThreadModel Filter33 Ashish Bhatia 37. ServletContextServletConfig interface getServletContext() that returns ServletContextCommunicates with the container when you want toperform actions such as writing log files ordispatching requestOne ServletContext object per web application. Intialized when application starts Destroyed when application shut downsPersistence mechanism Attributes available through out.34Ashish Bhatia 38. Other interfaces brief introductionServletRequest and ServletResponse Provide the client request information and objectused to send the reponse to the client.RequestDispatcher Object that manages client request by directing themto appropriate resources to the server.Filter, FilterChain, FilterConfig Use for filtering35Ashish Bhatia 39. Understanding the javax.servlet : classesNine classes + 2 exception classes Generic Servlet ServletContextEvent ServletContextAttributeEvent ServletRequestEvent ServpetRequestAttributeEvent ServletInputStream ServletOutputStream ServletRequestWrapper ServletResponseWrapper ServletException UnavailableException36Ashish Bhatia 40. Understanding the javax.servlet : classes37Ashish Bhatia 41. Understanding the javax.servlet : classes38Ashish Bhatia 42. Serlvet Interface : LifeCycleServlet must implement the Servlet interface ORExtend from a class that has already implementedServlet Interface.public void init(ServletConfig c) throws ServletExceptionpublic void service(ServletRequest req, ServletResponseres) throws ServletException, IOExceptionpublic void destroy()public ServletConfig getServletConfig()public String getServletInfo() throws ServletException,IOException39Ashish Bhatia 43. GenericServlet ClassAbstract class implmenting Servlet Interface. public void init(ServletConfig config) public void init() public abstract void service(ServletRequest req, ServletResponse res) public void destroy() public ServletContext getServletContext() public java.util.Enumeration getInitParameterNames() public String getInitParameter(String name) public String getServletName() public void log(String msg) public void log(String message, java.lang.Throwable t)40 Ashish Bhatia 44. HttpServlet CassOverrides service method of GenericServlet class. public void init(ServletConfig config) public void init() public void service(ServletRequest req, ServletResponse res) public void service(HttpServletRequest req, HttpServletResponse res)public void destroy()41 Ashish Bhatia 45. HttpServlet Cassprotected void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOExceptionprotected void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOExceptionprotected void doHead(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOExceptionprotected void doPut(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException42Ashish Bhatia 46. HttpServlet Cassprotected void doOption(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOExceptionprotected void doTrace(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOExceptionprotected long getLastModified(HttpServlet req)throws ServletException,IOException43 Ashish Bhatia 47. Understanding GenericServlet and HttpServlet44Ashish Bhatia 48. Program StructureHelloWorld index.htmlWEB-INF classes lib web.xmlHelloWorld.class29 Ashish Bhatia 49. Prepared usingEnd of Session for today