[go: up one dir, main page]

0% found this document useful (0 votes)
79 views52 pages

Chakradhar: Presentation by

The document discusses Common Gateway Interface (CGI) and servlets. CGI allows communication between a client and server-side applications, generating dynamic content. It has drawbacks like being slow and unsafe. Servlets were introduced to overcome CGI limitations. Servlets benefit from the Java platform and APIs, running more efficiently within a Java Virtual Machine. The document covers servlet lifecycles, APIs, deployment, and requesting servlets from clients.

Uploaded by

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

Chakradhar: Presentation by

The document discusses Common Gateway Interface (CGI) and servlets. CGI allows communication between a client and server-side applications, generating dynamic content. It has drawbacks like being slow and unsafe. Servlets were introduced to overcome CGI limitations. Servlets benefit from the Java platform and APIs, running more efficiently within a Java Virtual Machine. The document covers servlet lifecycles, APIs, deployment, and requesting servlets from clients.

Uploaded by

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

Presentation by:

Chakradhar
CGI
CGI
Common Gateway Interface (CGI)
• As Fire walls are maintained in internet we
can’t access the resources of one network
through the other network systems, Fire
wall allows only Http Protocol.
• CGI is the standard way of communication
between Client and Application on the
Server.
• It is an interface which allows to
communicate with an application on the
server.
CGI
• CGI allows to generate Dynamic content in
response to the request from client.
• Programmes available on server side are
called as CGI scripts. These are loaded
whenever it receives client request.
• Scripting Language for CGI script must be
in a position to read and write from standard
input & output Streams.
• CGI is introduced with PERL(Practical
Extraction and Reporting Language) which
is developed to overcome the problems of c-
language.
CGI
• CGI is commonly implemented by C, C++,
PERL and Java etc.
• With C and C++, CGI scripts are generated
as executable files and stores in CGI BIN
directory with executable permissions.
• C and C++ based CGI scripts will have
problems like Security problems, Platform
Dependent, and lack of proper support to
Strings.
• PERL implementation of CGI scripts has
overcome the above problems.
CGI
• PERL is mainly used to generate Reports
after reading the data. Hence it has
extended the support of Strings.
• PERL is secured Language because of lack
of pointers.
• PERL is interpreter based language, where
syntax is mixture of shell script and C-Lang.
As it is interpreter the programme can be
ported to any platform without changes.
• As it is Interpreter based, the performance
may come down.
CGI
Drawbacks of CGI
1. CGI is slow since for every client request a
new process starts. If number of clients
increases, less memory is available which
tends to bring down the performance.

It becomes slower if the application is


written in interpreter based language.

It becomes unsafe if they are written in


compiler based languages.
CGI
Drawbacks of CGI
2. Fast CGI is better in performance when
compare to CGI since it uses concept of
persistent process ( single process
provides response to the many clients if
they request for same script).

This concept is implemented using Java


but java has lack of env variable reading
support. Accessing such variables may
make a java programme dependent on
platform.
CGI
Drawbacks of CGI
3. Server Side Extension (API)
IIS  ISAPI
NServer  NSAPI

These have drawbacks as specific to web


server as script is based on API provided
by the vendor of web server.

They are not portable as specific to one


web server only.
SERVLET
SERVLET
Servlets are Java platform technology of choice
for extending and enhancing Web servers .

•component-based
•server- independent
•platform-independent
•protocol-independent
•fast and efficient
•most secured
SERVLET
• Servlets are used for building Web-based
applications, without the performance
limitations of CGI programs.
• Servlets have access to the entire family
of Java APIs, including the JDBC API to
access enterprise databases.
• Servlets can also access a library of
HTTP-specific calls and receive all the
benefits of the mature Java language,
including portability, performance,
reusability, etc.
SERVLET
Request WEB
SERVER
CLEINT Servlet Name
+ parameters

JVM

Response DATA
SERVLET BASE
SERVLET
SERVLET
•JVM loads the servlet on the request from
client if servlet is not loaded.
•Heavily used servlets has to be loaded on
starting of web server to avoid loading and
creating instance on every client request.
•On every client request a service method is
called by creating it as separate thread in JVM,
i.e. every client request has a thread created.
•Unloading of servlet depends on vender
specification i.e., unloads after response or if
no response for a specific amount of time etc.
Life Cycle of SERVLET
CLASS INSTANCE

INIT

DOPOST SERVICE DOGET

DESTROY
SERVLET API
Servlet is an API which is provided as servlet.jar
Servlet is an interface of package javax.servlet

javax.servlet.Servlet

javax.servlet.GenericServlet

For HTTP protocol javax.servlet.http.HttpServlet


SERVLET
Methods to provide service

public void service(ServletRequest req ,


ServletResponse res) throws IOException,
ServletException
public void doGet(HttpServletRequest req ,
HttpServletResponse res) throws
IOException, ServletException
public void doPost(HttpServletRequest req ,
HttpServletResponse res) throws
IOException, ServletException
import javax.servlet.*;

public class NewServlet extends GenericServlet


{
public void init(ServletConfig conf)
{initialisation code; }
public void service(ServletRequest req,
ServletResponse res) throws IOException,
ServletException
{service code;}
public void destroy() { destroy code; }
}
import javax.servlet.*;
import javax.servlet.http.*;

public class NewServlet extends HttpServlet


{
public void init(ServletConfig conf)
{initialisation code;}
public void service(HttpServletRequest req,
HttpServletResponse res) throws
IOException, ServletException
{service code;}
public void destroy(){ destroy code;}
}
SERVLET
HTTP Technology allows to request in two ways
GET : requested from address bar and
information parameters are given as Query
String which is part of URL and separated by ‘?’
GETURL?QueryString
POST : requested from form and large
amount of information can be send to input
streams.

• POST is most preferred send large amount of


data as data gets truncated in GET request.
import javax.servlet.*;
import javax.servlet.http.*;
public class NewServlet extends HttpServlet
{ public void init(ServletConfig conf) {init code;}
public void doGet(HttpServletRequest req,
HttpServletResponse res) throws IOException,
ServletException
{Get Service code;}
public void doPost(HttpServletRequest req,
HttpServletResponse res) throws IOException,
ServletException
{Post Service code;}
public void destroy() {destroy code;}
}
SERVLET Response
res.setContentType( “MIME TYPE”);
To set the header format of response

ServletOutputStream sos=res.getOutputStream();
To create OutputStream to send response

sos.println( “html tags/data” );


Method used to send the data with OutputStream
MIME Types
text/plain audio/midi
text/html audio/wav
text/java audio/all

image/gif
image/jpg
image/bmp
Deploying Servlet
1.Create a Servlet and save that as
ServletName.java file.
2.Compile that from the place of creation.
3.Create a Deployment Descripto with
web.xml file, which includes servlet name
and servlet mapping.
4.Deploy the class file and deployment
descriptor into webserver with the
specified process of deployment of a web
server.
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>ServletClassName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/vfolder/ServletName</url-pattern>
</servlet-mapping>
</web-app>
Servlet in Tomcat
•The hierarchie structure to deploy the servlet
file in tomcat web server.

TOMCAT

OwnFolder

WEB-INF web.xml

classes Class files

package folder Class files


Requesting from Client
Call The servlet by using the URL in client
application(HTML Form, Browser AddressBar,
Applet etc.) as…..
http://hostIpAddress:portofwebserver/ownfold
ername/servlet/ServletClassFileName

Example:
http://localhost:8080/chakri/servlet/Hello
http://127.0.0.1:8080/chakri/servlet/Hello
Requesting from Client

Call The servlet by using HTML Form


<form
action=“http://IpAddress:port/folder/servlet/Se
rvletClassFile” method=“post”>
<button type=“submit”>
</form>
Requesting from Client
Call The servlet by using Applet
String ues=URLEncoder.encode(“url”);
URL u=new URL(ues);
URLConnection con=u.openConnection();
BufferedReader br=new BufferedReader(new
InputStreamReader(con.getInputStream());
String s=br.readLine();
ta.setText(s); //ta is TextArea
Request Parameters
Enumeration e = req.getParameterNames()
To get the parameter names sent with the request

String name = (String) e.getNextElement()


To get the name stored in Enumeration Object

String[] s = req.getPrameterValues(String)
To get the parameter values of given parameter name
Request Headers
Enumeration e = req.getHeaderNames();
while (e.hasMoreElements())
{
String name = (String)e.nextElement();
String value = req.getHeader(name);
out.println(name + " = " + value);
}
COOKIES
Http is a stateless protocol. Every request is
treated as request from a new user, even though
the same client is requesting.
Http is stateless since the purpose of this
protocol is just to distribute the information and
not to retain information about client.
To maintain the inforamtion of client, Cookie is
invented by Netscape company.
Cookie is a part of Http.
COOKIES
Cookies are name, valued objects which are
created at server and stored on client side by the
server. Cookie contents the information of client
itself.
The next time client sends the request, cookie is
also send as part of request.
Every cookie has only one name and value.
Cookie will have the Max Age, Domain Name,
Path and Comment associated with it.
Cookie is stored as text file in c:\windows\temp
COOKIES
Max Age:
Default is upto destroying the browser.
-ve means valid till current session.
0 means deleting cookie on client side.
We can set the age of cookie explicitly as
24x60x60 for 1 day
2x24x60x60 for 2 days etc.
COOKIES
Domain :Every cookie will have domain apart
from maxage. Default of cookie is the domain
from which cookie is sent to the client.
Path :Cookie having a path will not be
send to server if request URL doen’t contain the
cookie path.
Comment :Some user agents have facility to
worn the clients before accepting coockies.
This working is through a dialog which can also
show comment of the cookie.
COOKIES
Cookie is a class present in javax.servlet.http
package.

Constructor:
Cookie(String name, String value)
res.addCookie(c);

Cookie[ ] c = req.getCookies();
COOKIES
Methods:
Int t=c.getMaxAge(), c.setMaxAge(int)
String dom=c.getDomain(), c.setDomain(String)
String p=c.getPath(), c.setPath(String)
String com=c.getComment(),
c.setComment(String)
String n=c.getName()
String v=c.getValue(), c.setValue(String)
COOKIES
String n = req.getParameter("cName");
if (n != null && n.length() > 0)
{
String v = req.getParameter("cValue");
Cookie c = new Cookie(n, v);
res.addCookie(c);
}
COOKIES
Cookie[] cookies = req.getCookies();
for (int i = 0; i < cookies.length; i++)
{
Cookie c = cookies[i];
String name = c.getName();
String value = c.getValue();
out.println(name + " = " + value);
}
SESSION
A Session begins when a client establishes a
connection with http server.
It is valid until the client is connected to server.
Session may also desposed if the client is idle
for more the a specific time, which is dependent
on server vendor/ Administrator.
For every clinet an object will be created at
server side which is called as HttpSession.
HttpSession stores the information of client on
the server.
SESSION
Session will have the features of storing and
retriving the information.
Session object implements the interface of
HttpSession.
Session is a concept which internally works
with cookie. It is called as Server Side Cookie.
Session information of a client is identified by
the server through the usage of cookie, which is
created and set by the web server(Servlet
Engine). Cookie is validated till the session is
valid.
SESSION
HttpSession session = req.getSession(true);
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements())
{
String n = (String)e.nextElement();
String v = session.getAttribute(name).toString();
out.println(n + " = " + v);
}
SESSION
Methods:
hs.putValue(String name, Object o);
Object o=hs.getValue(String name);
String n[ ]=hs.getVAlueNames();
hs.removeValue(String name);
String id=hs.getId();
long t=hs.getCreationTime();
long t=hs.getLastAccessedTime();
hs.invalidate();
hs.getAttributeNames();
SESSION
HttpSession session = req.getSession(true);
Date created = new
Date(session.getCreationTime());
Date accessed = new
Date(session.getLastAccessedTime());
out.println("ID " + session.getId());
out.println("Created: " + created);
out.println("Last Accessed: " + accessed);
Servlet to Servlet Communication
res.encodeRedirectURL(“URL?QueryString”));

Example:
res.encodeRedirectURL
(“http://server:8080/servlet/color”));
res.encodeRedirectURL
(“http://server:8080/color.html”));
Servlet to Servlet Communication

Servlet Dispacher
Servlet to Servlet Communication

ServletInclude
Servlet Load Balencing

Using Clustors concept


SSL

Using Https
Servlet InitParameters

Using conf.getInitParameter(“name”);
Servlet Side Include

Using .shtml

You might also like