Sie sind auf Seite 1von 15

Presentation by:

JOSHI
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
•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.HttpServle


t
SERVLET
Methods to provide service

public void service( ServletRequest req ,


ServletResponse res) throws IOException,
ServletException
public void doGet( ServletRequest req ,
ServletResponse res) throws IOException,
ServletException
public void doPost( ServletRequest req ,
ServletResponse res) throws IOException,
ServletException
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.
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
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
Servlet to Servlet Communication

res.sendRedirect( req.encodeDirectURL(
“URL?QueryString”));

Example:
res.sendRedirect( req.encodeDirectURL(
“http://server:8080/servlet/color”));
res.sendRedirect( req.encodeDirectURL(
“http://server:8080/color.html”));

Das könnte Ihnen auch gefallen