http basic and servlet basic

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

Upload: ashishspace

Post on 17-May-2015

1.087 views

Category:

Education


0 download

DESCRIPTION

This slides shows basic of HTTP. How it works in terms of request and response. IT also shows how to use tomcat.

TRANSCRIPT

Page 1: HTTP Basic and Servlet Basic

Unit 2

Presentation by

AshishSingh T Bhatia

Lecturer, CTI

[email protected]

HTTP BasicsServlet Basics

Prepared using

Page 2: HTTP Basic and Servlet Basic

Assignment 1

1. Explain Internet and WWW.

2. Explain the basic structure of HTML document with brief history.

3. What is JavaScript? Describe its evolution.

4. Explain different type of datatypes available and use in 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

Page 3: HTTP Basic and Servlet Basic

Assignment 1

11. Explain the functions and its use in JavaScript.

12. Explain following with examples

1. typeof

2. instanceof

3. prototype

4. constructor

5. caller

6. toString

7. valueOf

13. Desribe the object creation in JavaScript with example.

II Ashish Bhatia

Page 4: HTTP Basic and Servlet Basic

HTTP

● HTTP stands for Hyper Text Transfer Protocol

● HTTP is ● Aplication Level Protocol for distributed, collaborative,

hypermedia information systems.● Generic stateless protocol which can be used for many

tasks beyond its use for hypertext .● Use by WWW since 1990.● Use reliable TCP / IP connection.

1 Ashish Bhatia

HTTP/0.9 HTTP/1.0 HTTP/1.1

RFC ? RFC 1945 RFC 2616

Page 5: HTTP Basic and Servlet Basic

URL , URI , URN

● URL : Uniform Resource Locator● URI : Uniform Resource Identifier● URN : Uniform Resource Name

2 Ashish Bhatia

Page 6: HTTP Basic and Servlet Basic

URL , URI , URN

● Web Identifiers● Classical View

● [ URI ] Identifier might specify● location of resource ==> URL

● http://● identifies a resource via a representation of its

primary access mechanism (e.g., its network "location"),

● Its name independent of location ==> URN● urn:isbn :n-nn-nnnnnn-n

3 Ashish Bhatia

Page 7: HTTP Basic and Servlet Basic

Move Back to History

4 Ashish Bhatia

Page 8: HTTP Basic and Servlet Basic

Move Back to History

5 Ashish Bhatia

Page 9: HTTP Basic and Servlet Basic

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 Server

6 Ashish Bhatia

Page 10: HTTP Basic and Servlet Basic

Understanding a Bit here

● Add – on servlet Engines● Jserv [ Apache ]● JRun [ Live Software ]● WebSphere [ IBM ]● ServletExec [ Atlantas ]

● Embeddable Servlet Engines

7 Ashish Bhatia

Page 11: HTTP Basic and Servlet Basic

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 Body

8 Ashish Bhatia

Page 12: HTTP Basic and Servlet Basic

Coming back to HTTP

9 Ashish Bhatia

Page 13: HTTP Basic and Servlet Basic

Coming back to HTTP

10 Ashish Bhatia

Page 14: HTTP Basic and Servlet Basic

Coming back to HTTP

11 Ashish Bhatia

Page 15: HTTP Basic and Servlet Basic

Coming back to HTTP

12 Ashish Bhatia

Page 16: HTTP Basic and Servlet Basic

Coming back to HTTP

● Server process the request and send the response to the client.

● HTTP/1.0 200 OK

13 Ashish Bhatia

Page 17: HTTP Basic and Servlet Basic

Coming back to HTTP

● 1xx: Informational - Request received, continuing process

● 2xx: Success - The action was successfully received, understood, and accepted

● 3xx: Redirection - Further action must be taken in order to complete the request

● 4xx: Client Error - The request contains bad syntax or cannot be fulfilled

● 5xx: Server Error - The server failed to fulfill an apparently valid request

14 Ashish Bhatia

Page 18: HTTP Basic and Servlet Basic

Coming back to HTTP

● GET ● Retrieve information identified by Request-URI

● HEAD● Similar to GET except that server will not return

a message body ● POST

15 Ashish Bhatia

Page 19: HTTP Basic and Servlet Basic

Coming back to HTTP

● PUT● Enclosed entity be stored under the supplied

Requested-URI● If entity exists it will be treated as modified version● 201 – Created● 200 – OK● 204 – No Content● Error Code● 501 – Not Implemented

16 Ashish Bhatia

Page 20: HTTP Basic and Servlet Basic

Coming back to HTTP

● DELETE● Delete the resource identified by the Request-URI● 200 – OK● 202 – accepted● 204 – No content

● TRACE● Used to invoke a remote, application-layer

loop-back of the request message. The final

recipient of the request SHOULD reflect themessage received back to the client as the entity-body of a 200 (OK) response.

17 Ashish Bhatia

Page 21: HTTP Basic and Servlet Basic

Coming back to HTTP

● CONNECT ● Reserves for use with proxy that can dynamically

switch to being a tunnel.

18 Ashish Bhatia

Page 22: HTTP Basic and Servlet Basic

Are You Attentive ?

How many methods we studied ?

How request header looks ?

How response header looks ?

Prepared using

Lets move to Servlet Basics

19 Ashish Bhatia

Page 23: HTTP Basic and Servlet Basic

Servlet Application Architecture

● A 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 server

20 Ashish Bhatia

Page 24: HTTP Basic and Servlet Basic

Servlet Application Architecture

● Problem and Solution

21 Ashish Bhatia

Page 25: HTTP Basic and Servlet Basic

How Servlet Works

● Servlet is loaded in Servlet Container 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 memory waiting for other requests

● Unloaded ??? Shortage of mem

22 Ashish Bhatia

Page 26: HTTP Basic and Servlet Basic

Tomcat as Servlet Container

● Designed by Sun Microsystem ● Handed the code the Apache – 1999

● Included as Jakarta Project ● Its one of recognised Servlet Container and

used world wide● OpenSource

● Current version 7.0

23 Ashish Bhatia

Page 27: HTTP Basic and Servlet Basic

Getting up with Tomcat

● Genreally two options available for tomcat installation

● Liked by many .exe ● Zip folder

● Where to get Tomcat ?● http://tomcat.apache.org/● http://tomcat.apache.org/download-70.cgi

24 Ashish Bhatia

Page 28: HTTP Basic and Servlet Basic

Getting up with Tomcat

25 Ashish Bhatia

Page 29: HTTP Basic and Servlet Basic

Setting up using zip / tar.gz version

● Setting up on any platform● Extract the zip / tar.gz to desired place

● Assumption● Extracted folder D:\tomcat● JDK : C:\Program Files\Java\jdk

● Set two environment variables● CATALINA_HOME=D:\tomcat● JAVA_HOME=C:\Program Files\Java\jdk

● If exe : ● Double click ==> Get installed as window service

26 Ashish Bhatia

Page 30: HTTP Basic and Servlet Basic

Moving ahead to directory

27 Ashish Bhatia

Page 31: HTTP Basic and Servlet Basic

Directory details

● bin● Contains files for starting / stopping tomcat server

● conf● Contains xml files

● lib● Contains jar files

● logs● temp● webapps

● Working directory mapped to localhost [ see later ]● work

28 Ashish Bhatia

Page 32: HTTP Basic and Servlet Basic

Program Structure

29 Ashish Bhatia

Project

WEB-INF

classes lib tags

JSP pages, static html pages, applet classes

web.xml .class files Library archives . tag files

Page 33: HTTP Basic and Servlet Basic

Directory details

● Http://localhost:8080/Add● This will call up index page

[Default Page]● When called the servlet object

is created from Add.class

30 Ashish Bhatia

Page 34: HTTP Basic and Servlet Basic

Understanding the javax.servlet

31 Ashish Bhatia

Page 35: HTTP Basic and Servlet Basic

Understanding the javax.servlet : interfaces

32 Ashish Bhatia

● 14 interfaces : 7 implemented provided by container● ServletConfig● ServletContext● ServletRequest● ServletResponse● RequestDispatcher● FilterChain● FilterConfig

Page 36: HTTP Basic and Servlet Basic

Understanding the javax.servlet : interfaces

33 Ashish Bhatia

● 14 interfaces : 7 left for developer● Servlet● ServletRequestListener● ServletRequestAttributeListener● ServletContextListener● ServletContextAttributeListener● SingleThreadModel● Filter

Page 37: HTTP Basic and Servlet Basic

ServletContext

34 Ashish Bhatia

● ServletConfig interface● getServletContext() that returns ServletContext

● Communicates with the container when you want to perform actions such as writing log files or dispatching request

● One ServletContext object per web application.● Intialized when application starts ● Destroyed when application shut downs

● Persistence mechanism● Attributes available through out.

Page 38: HTTP Basic and Servlet Basic

Other interfaces brief introduction

35 Ashish Bhatia

● ServletRequest and ServletResponse● Provide the client request information and object

used 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 filtering

Page 39: HTTP Basic and Servlet Basic

Understanding the javax.servlet : classes

36 Ashish Bhatia

● Nine classes + 2 exception classes● Generic Servlet● ServletContextEvent● ServletContextAttributeEvent● ServletRequestEvent● ServpetRequestAttributeEvent● ServletInputStream● ServletOutputStream● ServletRequestWrapper● ServletResponseWrapper● ServletException● UnavailableException

Page 40: HTTP Basic and Servlet Basic

Understanding the javax.servlet : classes

37 Ashish Bhatia

Page 41: HTTP Basic and Servlet Basic

Understanding the javax.servlet : classes

38 Ashish Bhatia

Page 42: HTTP Basic and Servlet Basic

Serlvet Interface : LifeCycle

39 Ashish Bhatia

● Servlet must implement the Servlet interface ORExtend from a class that has already implementedServlet Interface.

● public void init(ServletConfig c) throws ServletException● public void service(ServletRequest req, ServletResponse

res) throws ServletException, IOException● public void destroy()

● public ServletConfig getServletConfig()● public String getServletInfo() throws ServletException,

IOException

Page 43: HTTP Basic and Servlet Basic

GenericServlet Class

40 Ashish Bhatia

● Abstract 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)

Page 44: HTTP Basic and Servlet Basic

HttpServlet Cass

41 Ashish Bhatia

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

Page 45: HTTP Basic and Servlet Basic

HttpServlet Cass

42 Ashish Bhatia

● protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

● protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

● protected void doHead(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

● protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

Page 46: HTTP Basic and Servlet Basic

HttpServlet Cass

43 Ashish Bhatia

● protected void doOption(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

● protected void doTrace(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

● protected long getLastModified(HttpServlet req)throws ServletException,IOException

Page 47: HTTP Basic and Servlet Basic

Understanding GenericServlet and HttpServlet

44 Ashish Bhatia

Page 48: HTTP Basic and Servlet Basic

Program Structure

29 Ashish Bhatia

HelloWorld

WEB-INF

classes lib

index.html

web.xml HelloWorld.class

Page 49: HTTP Basic and Servlet Basic

End of Session for today

Prepared using