+ All Categories
Home > Documents > Definition Servlet: Servlet is a java class which extends the functionality of web server by...

Definition Servlet: Servlet is a java class which extends the functionality of web server by...

Date post: 23-Dec-2015
Category:
Upload: brianne-brown
View: 228 times
Download: 0 times
Share this document with a friend
22
Servlet
Transcript
Page 1: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Servlet

Page 2: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

DefinitionServlet:

Servlet is a java class which extends the functionality of web server by dynamically generating web pages.

Web server: It is a server side technology which handles client request and

give appropriate response to original client.

Web browser: It is a client side technology which knows how to connect with

web server on internet and how to request a page.

Page 3: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

CGI(Common Gateway Interface)

client server CGI DB

request

response

request

response

process

response

Page 4: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

servlet

client

servlet

DB

request

response

request

response

server

Page 5: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Servlet BasicServlet is a java class which extends the functionality of web

server by dynamically generating web pages.A servlet is a java programming language class that is used to

extend the capabilities of server that host application accessed by means of a request.

Servlet can respond to any type of request, they are commonly used to extend the applications hosted by web server.

A runtime environment known as a servlet container manages servlet loading and unloading and works with the web server to direct requests to servlets and to send output back to web clients. javax.servlet javax.servlet.http

Packages provide interface and classes for writing servlets.

Page 6: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Architecture of servletThe javax.servlet package provides interfaces and classes

for writing servlets.The architecture of the package is described below:

Servlet

GenericServlet

HttpServlet

MyServlet

Page 7: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Javax.ServletThis package contains classes and interfaces

not specific to any particular protocol.These classes define the relationship between

a servlet and the runtime environment provided by the servlet container.

Page 8: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Javax.servlet.GenericServletA base class for servlets that do not use HTTP

protocol-specific features.GenericServlet implements the basic features

of all servlets, including initialization, request handling and termination.

Page 9: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Javax.servlet.http.HttpServletAbstract base classOperate in an HTTP environmentHttpServlet provides specific method• GET• POST• PUT• DELETE• HEAD• OPTIONS• TRACE requestHttpServlet override doGet() , doPost().

Page 10: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

MyServletMyServlet class is our own java class in that

class we can write our code of program.

Page 11: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

LifeCycle of servletThe servlet engine does the following:Loads a servlet when it is first requested.Calls the servlet’s init() method.Handles any number of requests by calling

the servlet’s service() method.When shutting down, calls the destroy()

method of each active servlet.

Page 12: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

init()Syntax:

public void init(ServletConfig config)throws ServletException

When reqest for a servlet is received by the servlet engine, it checks to see if the servlet is already loaded. If not, the servlet engine uses a class loader to get the particular servlet class required, and then invokes its constructor to get an instance of the servlet.

After the servlet is loaded, but before it services any requests, the servlet engine calls an init method.

This method is called only once just before the servlet place into service.

Page 13: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

service()Syntax:

Public void service(ServletRequest req,ServletResponse res)

After the init() complete successfully the servlet is able to accept request.

By default, only single instance of the servlet is created, and the servlet container dispatches each request.

Page 14: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

doGet(),doPost()Syntax:

Pubic doGet/doPost(HttpServletRequest req,

HttpServeltResponse res)

GET requests are handled by doGet().POST requests are handled by doPost().

Page 15: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Destroy()Syntax:

Public void destroy()

The servlet specification allows a servlet container to unload a servlet at any time.

This may be done to conserve system resources or in preparation for servlet container shutdown.

Release any resources allocated during init().

Page 16: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

First Example of GenericServlet import java.io.*; import javax.servlet.*;

public class SimpleServlet extends GenericServlet { public void service(ServletRequest req,ServletResponse

res)throws ServletException,IOException { res.setContentType("text/html"); PrintWriter out=res.getWriter(); out.println("<b><i>Example of generic servlet</i></b>"); out.close(); } }

Page 17: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Servlet Life Cycle Exampleservlet Life cycle Example

Page 18: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Reading Request Headers(1)Reading headers is straightforward; just call

the getHeader method of HttpServletRequest with the name of the header.

This call returns a String if the specified header was supplied in the current request, null otherwise.

getMethod()The getMethod method returns the main

request method (normally, GET or POST, but methods like HEAD, PUT, and DELETE are possible).

Page 19: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Reading Request Headers(2)getRequestURI()

The getRequestURI method returns the part of the URL that comes after the host and port but before the form data. For example, for a URL of http://randomhost.com/servlet/search.BookSearch?subject=jsp, getRequestURI would return "/servlet/search.BookSearch".

getQueryString()The getQueryString method returns the form data. For

example, with http://randomhost.com/servlet/search.BookSearch?subject=jsp, getQueryString would return "subject=jsp".

Page 20: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

Reading Request Headers(3)getProtocol()

The getProtocol method returns the third part of the request line, which is generally HTTP/1.0 or HTTP/1.1.

getHeaders() In most cases, each header name appears only once in the

request. Occasionally, however, a header can appear multiple times, with each occurrence listing a separate value. Accept-Language is one such example. You can use getHeaders to obtain an Enumeration of the values of all occurrences of the header.

Page 21: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

getHeaders()Accept

This header specifies the MIME types that the browser or other clients can handle.

Accept-LanguageThis header specifies the client's preferred languages in case the

servlet can produce results in more than one language. The value of the header should be one of the standard language codes such as en, en-us, da, etc.

User-AgentThis header identifies the browser or other client making the

request and can be used to return different content to different types of browsers.

Most Internet Explorer versions list a "Mozilla" (Netscape) version .

Page 22: Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.

getHeaders()Accept-Encoding

This header designates the types of encodings that the client knows how to handle. If the server receives this header, it is free to encode the page by using one of the formats specified (usually to reduce transmission time), sending the Content-Encoding response header to indicate that it has done so.

Hostbrowsers and other clients are required to specify this

header, which indicates the host and port as given in the original URL.

Connection This header indicates whether the client can handle persistent

HTTP connections. In HTTP 1.0, a value of Keep-Alive means that persistent

connections should be used.


Recommended