asif

57
SERVER SIDE PROGRAMS

Upload: shaik-asif

Post on 13-Jul-2015

37 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Asif

SERVER SIDE PROGRAMS

Page 2: Asif

Where are we?Where are we?

1. Intro to

Java, Course

2. Java lang.

basics

3. Arrays

5. Encapsulation

4. Object

Classes 7. Inheritance

8. Polymorphism

10. JDBC

6. Exception

Handling

Introduction Object-oriented design Advanced topics

Newbie Programmers Developers ProfessionalsDesigners

JAVA

9. Abstract classes

and Interfaces

13. Servlets

14. JSP

Server-side coding

12. Networking

11.Streams

Page 3: Asif

Client

HTTP

Request

Request

HTTP

HTML

Response

<html>

<head>

<body>

<html>

<head>

<body>

Page 4: Asif
Page 5: Asif
Page 6: Asif

The Web page is based on data submitted by the user E.g., results page from search engines

The Web page is derived from data that changes

frequently E.g., a weather report or news headlines page

The Web page uses information from databases or

other server-side sources E.g., an e-commerce site could use a servlet to build a

Web page that lists the current price and availability of each

item that is for sale.

Why Build Web Pages Dynamically?

Page 7: Asif
Page 8: Asif

Key elements of a “request”

stream:

HTTP method (action to be

performed).

The page to access (a URL).

Form parameters.

Key elements of a “response”

stream:

A status code (for whether

the request was successful).

Content-type (text, picture,

html, etc…).

The content ( the actual

content).

HTTP Request HTTP Response

Page 9: Asif

• Where does Servlet come into the picture?

Web Server

Application

Helper

Application

Web Server machine

I can serve only

static HTML

pages

Dude ,Not a

problem. I

can handle

dynamic

requests.

“The Helper Application is nothing but a SERVLET”

Introduction – What is a Servlet

Page 10: Asif

What is java servlet ?

A servlet is a small Java program that runs within a Web server. Servlets

receive and respond to requests from Web clients, usually across HTTP,

the HyperText Transfer Protocol. Servlet is an opposite of applet as a

server-side applet. Applet is an application running on client while servlet

is running on server.

Client Server

Request

Response

Servlet

Page 11: Asif

Example use of servlet

Processing data POST over HTTPs using HTML form as purchase

order or credit card data

Allowing collaborative between people such as on-line

conferencing

Web Server

(Application Logic)

Database/ FileSystem

(Persistent Storage)Application/Browser

(User Interface)

Page 12: Asif

Why Use Servlets ?

One of the reasons that Java became

popular is Applets.– but there are problems with Applets

• Browser compatibility

Server-side Java– the code is executed on the server side not the client

side

– a dynamically loaded module that services requests

from a Web server

Page 13: Asif

Servlets (contd.)

– vs. Common Gateway Interface (CGI)• create new process for each request

– most platform independent CGI language - Perl• start a new instance of interpreter for every request

• CGI runs in a completely separate process from the Web

server

– vs. Server-Side JavaScript• only available on certain web servers

– vs. Active Server Pages (ASP)• only available on certain web servers

Page 14: Asif
Page 15: Asif

• What is a Web Container?

GET.

…..

Web

ServerServletWeb

Container

GET.

…..

GET.

…..

request

Client

Servlet Architecture -Web Container

Page 16: Asif

• How does the Container handle a request?

Web

Container

Servlet

Thread

Service()

doGet()

<Html>

<Body>

…….

</Body>

</Html>

request

response

response

Web

Server

Http request

Client

Servlet Architecture – Web Container

Page 17: Asif

• What is the role of Web Container ?

• Communication Support

• Lifecycle Management

Multi-threading support

• Security

• JSP Support

The CONTAINER

S1

S3

S4

S2

JSP1

The container can contain multiple Servlets & JSPs within it

Servlet Architecture – Web Container

Page 18: Asif

Example for Servers: Netscape Web servers

Microsoft's Internet Information Server (IIS),

the World Wide Web Consortium's Jigsaw Web server

Page 20: Asif

3

Apache

Apache is a very popular server

66% of the web sites on the Internet use Apache

Apache is:

Full-featured and extensible

Efficient

Robust

Secure (at least, more secure than other servers)

Up to date with current standards

Open source

Free

Why use anything else?

3

Apache

Apache is a very popular server

66% of the web sites on the Internet use Apache

Apache is:

Full-featured and extensible

Efficient

Robust

Secure (at least, more secure than other servers)

Up to date with current standards

Open source

Free

Page 21: Asif

• Before you can program with servlets, you must download and install the Apache group's implementation of servlets called Tomcat.

• Install the Tomcat with specific port number(default is 8080

• If the installation is successful, you get the Tomcat’s home page

Page 22: Asif
Page 23: Asif
Page 24: Asif
Page 25: Asif
Page 26: Asif
Page 27: Asif
Page 28: Asif
Page 29: Asif
Page 30: Asif
Page 31: Asif
Page 32: Asif
Page 33: Asif

• The Servlet lifecycle is simple, there is only one main state –

“Initialized”.

Initialized

Does not exist

init()destroy()

Service()

Servlet Lifecycle

The container calls

the init() before the

servlet can service

any client requests.

To initialize your

servlet before

handling any client

requests.

When a new request

for that servlet

comes in.

To determine which

HTTP method

should be called.

Page 34: Asif

GenericServlet

HttpServlet

Your Servlet

ServletInterface

Abstract class

Abstract class

Concrete class

If not overridden, implements init()

method from the ‘Servlet’ interface,

If not overridden, implements service()

method.

We implement the HTTP methods

here.

Servlet Lifecycle - Hierarchy

Page 35: Asif

Object model of Servlet Framework<<Interface>>

javax.servlet.Servlet

init( )

getServletConfig( )

service( )

getServletInfo( )

destroy( )

<<Interface>>

javax.servlet.ServletConfig

getInitParameter( )

getServletContext( )

getInitParameterNames( )

getServletName( )

javax.servlet.GenericServlet

init( )

getServletConfig( )

service( )

getServletInfo( )

destroy( )

getInitParameter( )

getServletContext( )

getInitParameterNames( )

getServletName( )

log( )

javax.servlet.http.HttpServletdoDelete( )

doGet( )

doOptions( )

doPost( )

doPut( )

doTrace( )

getLastModified( )

service( )

Basic Servlet

Page 36: Asif

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloServlet extends HttpServlet

{

public void doGet(HttpServletRequest request,

HttpServletResponse response)throws

ServletException,IOException

Page 37: Asif

The HTTP request method determines whether doGet() or

doPost() runs.

GET (doGet()) POST (doPost())

HTTP Request

The request contains only the

request line and HTTP header.

Along with request line

and header it also contains

HTTP body.

Parameter

passing

The form elements are passed

to the server by appending at

the end of the URL.

The form elements are

passed in the body of the

HTTP request.

Size The parameter data is limited

(the limit depends on the

container)

Can send huge amount of

data to the server.

Usage Generally used to fetch some

information from the host.

Generally used to process

the sent data.

Request and Response – GET v/s POST

Page 38: Asif

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloServlet extends HttpServlet

{

public void doGet(HttpServletRequest request, HttpServletResponse

response)throws ServletException,IOException

{

response.setContentType("text/html");

Page 39: Asif

HttpServletResponse Interface

void setContentType( String type )

Specifies the MIME( Multipurpose Internet Mail Extensions) type of the response to the browser. The MIME type helps the browser determine how to display the data (or possibly what other application to execute to process the data).

For example, MIME type "text/html" indicates that the response is an HTML document, so the browser displays the HTML page.

Page 40: Asif

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloServlet extends HttpServlet

{

public void doGet(HttpServletRequest request, HttpServletResponse

response)throws ServletException,IOException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

Page 41: Asif

ServletOutputStream getOutputStream() Obtains a byte-based output stream enabling binary data to be sent

to the client.

PrintWriter getWriter() Obtains a character-based output stream enabling text data to be

sent to the client.

Page 42: Asif

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloServlet extends HttpServlet

{

public void doGet(HttpServletRequest request, HttpServletResponse

response)throws ServletException,IOException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html><head><title>Hello World</title></head>");

out.println("<body><h1>Hello World</h1></body></html>");

}

}

Page 43: Asif
Page 44: Asif
Page 45: Asif
Page 46: Asif
Page 47: Asif
Page 48: Asif
Page 49: Asif
Page 50: Asif

How does the Container know which Servlet the client has

requested for?

A Servlet can have 3 names

Client known URL name

Deployer known secret

internal name

Actual file name

<web-app>

………

<servlet>

<servlet-name>Hello</servlet-name>

<servlet-class>HelloServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Hello</servlet-name>

<url-pattern>/HelloNova</url-pattern>

</servlet-mapping>

………..

………..

</web-app>

Web.xml

Servlet Architecture – Deployment Descriptor

Page 51: Asif
Page 52: Asif
Page 53: Asif
Page 54: Asif
Page 55: Asif
Page 56: Asif

<web-app>

………

<servlet>

<servlet-name>Hello</servlet-name>

<servlet-class>HelloServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Hello</servlet-name>

<url-pattern>/HelloNova</url-pattern>

</servlet-mapping>

………..

………..

</web-app>

Page 57: Asif