0% found this document useful (0 votes)
4 views33 pages

Wa0002

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views33 pages

Wa0002

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Servlets

 Servlet is a technology that is used to create web application

 Servlet is a web component that is deployed on the server to


create dynamic web page

 Servlet is an API that has interfaces and classes for building


server processes

Servlets - Dr.S.E
Servlets
Servlets are programs that run on a web server which are
counterparts of Applets/jscripts that run on web client

Internet uses Client/Server model where client web browsers


send requests and web servers respond with needed
information.

Servlets operation is also based on request/response processing


model.

Servlets can be loaded dynamically and run by servlet


container of JVM.
Servlets - Dr.S.E
Servlets
Servlets interact with clients via a request-response model
based on HTTP.

Therefore, a servlet container must support HTTP. A servlet


container also supports similar protocols such as HTTPS
(HTTP over SSL) for secure transactions.

Servlets - Dr.S.E
Hypertext Transfer Protocol Characteristics
 HTTP is media independent: It refers to any type of media
content that can be sent by HTTP as long as both the server
and the client can handle the data content

 HTTP is connectionless: It is a connectionless approach in


which HTTP client i.e., a browser initiates the HTTP request.
After the request is sent, the client disconnects from server and
waits for the response

 HTTP is stateless: The client and server are aware of each


other during a current request only. Afterwards, both of them
forget each other.

Servlets - Dr.S.E
Servlets vs. Common Gateway Interface

Perl 1

Browser 1
A CGI program needs to be loaded
Browser 2 Web Perl 2
and started for each CGI request
Server from client. Three requests are
Browser N
handled by three processes
Perl N

There is only a single Servlet


container JVM which answers all
Browser 1 requests concurrently.
Browser 2 Web Servlet
Server
Browser N A Servlet does not run as a separate
process but runs as separate thread.
Three requests are handled by three
threads
Servlets - Dr.S.E
How Servlets Work

Receive is servlet No
Request loaded?

Yes

is servlet No
current?
Load Servlet
Yes

Send Process Request


Response

Servlets - Dr.S.E
Web Server Environment

Servlets - Dr.S.E
Web server & Application server

Servlets - Dr.S.E
Servlet life cycle

1. init() method initializes the Servlet with configuration

2. service() method processes the client’s request

3. destroy() method destroys Servlet and its resources


Servlets - Dr.S.E
Servlet creation - APIs
1. Servlets can be written by implementing the Servlet interface
and defining its abstract methods.
javax.servlet.Servlet

2. Servlets can also be written by extending the GenericServlet


class and overriding its methods.
javax.servlet.GenericServlet

3. Servlets can also be written by extending the HttpServlet


class and overriding its methods which are Http specific.
javax.servlet.http.HttpServlet

Servlet hierarchy
interface Servlet >>>> class GenericServlet >>>> class HttpServlet
Servlets - Dr.S.E
Interface javax.servlet.Servlet
The Servlet interface defines methods
• to initialize a servlet
Life
• to receive and respond to client requests Cycle
• to destroy a servlet and its resources Methods
• to get any startup information
• to return basic information about itself, such as its author,
version and copyright.

Servlets - Dr.S.E
Interface Servlet – Abstract Methods
void init(ServletConfig config)
Initializes the servlet.

void service(ServletRequest req, ServletResponse res)


Carries out a single request from the client.

void destroy()
Cleans up whatever resources are being held

ServletConfig getServletConfig()
Returns a servlet config object, which contains any initialization
parameters and startup configuration for this servlet.

String getServletInfo()
Returns a string containing information about the servlet, such as its
author, version, and copyright.

Servlets - Dr.S.E
GenericServlet class - Methods
void init(ServletConfig config)
Initializes the servlet.

void service(ServletRequest req, ServletResponse res)


Carries out a single request from the client.

void destroy()
Cleans up whatever resources are being held

ServletConfig getServletConfig()
Returns a servlet config object, which contains any initialization
parameters and startup configuration for this servlet.

String getServletInfo()
Returns a string containing information about the servlet, such as its
author, version, and copyright.

Servlets - Dr.S.E
HttpServlet class- Methods
• void doGet (HttpServletRequest request,
HttpServletResponse response)
–handles GET requests

• void doPost (HttpServletRequest request,


HttpServletResponse response)
–handles POST requests

• void doPut (HttpServletRequest request,


HttpServletResponse response)
–handles PUT requests

• void doDelete (HttpServletRequest request,


HttpServletResponse response)
– handles DELETE requests
Servlets - Dr.S.E
Servlet Request Objects
They provide client request information to a servlet running
inside the servlet container of server

The servlet container creates a servlet request object and


passes it as an argument to the servlet's service method.

Servlets - Dr.S.E
ServletRequest - Methods
Return type Method name

java.lang.String getParameter (java.lang.String name)


Returns the value of a request parameter as a String, or
null if the parameter does not exist
java.lang.String getMethod()
Returns the name of the HTTP method with which request
is made, for example, GET, POST, or PUT

HttpSession getSession()
Returns the current session associated with this request, or
if the request does not have a session, creates one

Servlets - Dr.S.E
Servlet Response Objects

• Defines an object to help Servlet in sending a response to the


client.

• The servlet container creates a ServletResponse object and


passes it as an argument to the Servlet's service method.

Servlets - Dr.S.E
ServletResponse - Methods
Return type Method Name

java.io.PrintWriter getWriter()
Returns a PrintWriter object that can send
character text to the client

Void setContentType (java.lang.String type)


Sets the content type of the response being sent to the
client. The content type may include the type of
character encoding used, for example, text/html

getWriter() returns the object of PrintWriter Class, which is used to print


output in HTML format in the client’s web page as a response.
Servlets - Dr.S.E
Content Type

 Content Type is also known as MIME (Multipurpose internet


Mail Extension). It is a HTTP header that provides the
description about the response content sent to the client browser.

content types:
 text/html
 text/plain
 application/msword
 application/pdf
 image/jpeg
 video/quicktime
 audio/mp4

Servlets - Dr.S.E
Steps to test a Generic Servlet
import java.io.*;
import javax.servlet.*;

public class HelloServlet extends GenericServlet


{
public void service (ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<B> HELLO welcome “);
pw.close();
}
}
-----------------------------------------------------------------------------------------------
set path=c:\jdk1.3\bin; c:\jsdk2.0\bin - to access jdk and jsdk tools
set classpath=c:\jsdk2.0\lib\jsdk.jar - to access javax api

javac HelloServlet.java - compile the servlet and create class file

servletrunner -d G:\javaLab2\servletsdir - run servletrunner with dir spec .

Either type http://localhost:8080/servlet/HelloServlet


Or include this in form tag of html
Servlets - Dr.S.E
Differences between Get and Post methods
1) In case of Get request, only limited amount of data can be sent
because data is sent along with header. In case of Post request, large
amount of data can be sent because data is sent in body section.

2) Get request is not secure because data is exposed in URL


bar.Post request is secure because data is not exposed in URL bar.

3) Get request is idempotent. It means second request will be


ignored until response of first request is delivered.Post request is
non-idempotent

Servlets - Dr.S.E
Interactive HttpServlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
int n=Integer.parseInt(req.getParameter("t1"));
int f=1;
for(int i=1;i<=n;i++)f=f*i;

res.setContentType("text/html");
PrintWriter p=res.getWriter();
p.println("<h1>Factorial value</h1>");
p.println("N value : "+n+”<br>”);
p.println("Factorial Value :"+f);
}
}
Servlets - Dr.S.E
Client html document
<html>
<form name="frm" method="get"
action="http://localhost:8080/servlet/HServlet">
<input type="text" name="t1">
<input type="submit" value="N value">
</form>
</html>

 In the url specified, the protocol is clearly http, the resource is servlet type
and the class name is HServlet

Servlets - Dr.S.E
What is Apache Tomcat?
 Tomcat is a Servlet container or Http -Web server that interacts
with Servlets developed by Apache Software Foundation

 Representation and management of web applications

 A Servlet container is a mini server, serving html, jsp and


servlet components.

 Tomcat is an open-source, non commercial project

 Tomcat is written in Java (OS independent)

Servlets - Dr.S.E
Web Server Environment

Servlets - Dr.S.E
Tomcat Directory Structure
Tomcat-Base

conf logs webapps server work shared

server.xml ROOT myApp1 myApp2 lib classes

WEB-INF

web.xml Tomcat-Home
lib classes

bin common

lib classes
Servlets - Dr.S.E
Servlet sample program execution in server

After installing Tomcat Server on computer, follow the following steps

Step 1: Create directory structure for web application.


Step 2: Create a servlet class file
Step 3: Create Deployment Descriptor
Step 4: Start the server and deploy the application

Deployment Descriptor(DD) is a XML document that is used by web


Container to run Servlets and JSPs. DD is used for mapping URL to
Servlet class.

Servlets - Dr.S.E
TOMCAT Server
Environment
Create the above directory structure inside Apache-Tomcat\webapps directory. All html,
static files( images, css etc) are kept directly under Web application folder. All servlet
classes are kept inside classes folder. The web.xml(deployment descriptor) file is kept under
WEB-INF folder. Servlets - Dr.S.E
1.Full form of WAMP is Windows, Apache, MySQL and PHP.
2.This is a open source platform.
3. WAMP Server works on Windows Operating System only.
4. Apache is the web server
5. Mysql is the relational database management system.
6. PHP is the object-oriented scripting language.

Servlets - Dr.S.E
Servlets - Dr.S.E
LAMP

1.Full form of LAMP is Linux, Apache, MySQL and PHP.


2.This is a open source platform.
3. LAMP Server works on Linux Operating System only.
4. LAMP is a combined package of Linux, Apache, MySQL
and PHP.
5. Apache is the web server
6. Mysql is the relational database management system.
7. PHP is the object-oriented scripting language.
Servlets - Dr.S.E
Servlets - Dr.S.E
1.Full form of MAMP is MACINTOSH, Apache, MySQL and PHP.
2.This is a open source platform.
3. MAMP Server works on MAC Operating System only.
4.Apache is the web server
5. Mysql is the relational database management system.
6. PHP is the object-oriented scripting language.

Servlets - Dr.S.E

You might also like