asif
TRANSCRIPT
SERVER SIDE PROGRAMS
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
Client
HTTP
Request
Request
HTTP
HTML
Response
<html>
<head>
<body>
…
<html>
<head>
<body>
…
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?
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
• 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
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
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)
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
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
• What is a Web Container?
GET.
…..
Web
ServerServletWeb
Container
GET.
…..
GET.
…..
request
Client
Servlet Architecture -Web Container
• 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
• 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
Example for Servers: Netscape Web servers
Microsoft's Internet Information Server (IIS),
the World Wide Web Consortium's Jigsaw Web server
Developer(s) Apache Software Foundation
Stable release 7.0.37
(February 18, 2013;)
Preview release Non [±]
Development status Active
Written in Java
Operating system Cross-platform
Type Servlet container
HTTP web server
License Apache License 2.0
Website tomcat.apache.org/
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
• 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
• 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.
•
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
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
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
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
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");
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.
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();
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.
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>");
}
}
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
<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>