asif
TRANSCRIPT
![Page 1: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/1.jpg)
SERVER SIDE PROGRAMS
![Page 2: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/2.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/3.jpg)
Client
HTTP
Request
Request
HTTP
HTML
Response
<html>
<head>
<body>
…
<html>
<head>
<body>
…
![Page 4: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/4.jpg)
![Page 5: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/5.jpg)
![Page 6: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/6.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/7.jpg)
![Page 8: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/8.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/9.jpg)
• 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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/10.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/11.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/12.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/13.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/14.jpg)
![Page 15: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/15.jpg)
• What is a Web Container?
GET.
…..
Web
ServerServletWeb
Container
GET.
…..
GET.
…..
request
Client
Servlet Architecture -Web Container
![Page 16: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/16.jpg)
• 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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/17.jpg)
• 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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/18.jpg)
Example for Servers: Netscape Web servers
Microsoft's Internet Information Server (IIS),
the World Wide Web Consortium's Jigsaw Web server
![Page 19: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/19.jpg)
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/
![Page 20: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/20.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/21.jpg)
• 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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/22.jpg)
![Page 23: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/23.jpg)
![Page 24: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/24.jpg)
![Page 25: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/25.jpg)
![Page 26: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/26.jpg)
![Page 27: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/27.jpg)
![Page 28: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/28.jpg)
![Page 29: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/29.jpg)
![Page 30: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/30.jpg)
![Page 31: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/31.jpg)
![Page 32: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/32.jpg)
![Page 33: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/33.jpg)
• 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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/34.jpg)
•
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/35.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/36.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/37.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/38.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/39.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/40.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/41.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/42.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/43.jpg)
![Page 44: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/44.jpg)
![Page 45: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/45.jpg)
![Page 46: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/46.jpg)
![Page 47: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/47.jpg)
![Page 48: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/48.jpg)
![Page 49: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/49.jpg)
![Page 50: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/50.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/51.jpg)
![Page 52: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/52.jpg)
![Page 53: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/53.jpg)
![Page 54: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/54.jpg)
![Page 55: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/55.jpg)
![Page 56: Asif](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/56.jpg)
<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](https://reader030.vdocument.in/reader030/viewer/2022032419/55a3e7fe1a28ab3c598b47fc/html5/thumbnails/57.jpg)