Vous êtes sur la page 1sur 19

Servlet Programming

High Level Perspective

1 - Initiate servlet from Browser 2 - load servlet 3 - access resources


1

Browser

HTTP Server

Network
Browser

Appl Server Servlet

Resources

Whats a Servlet?

Javas answer to CGI programming

Program runs on Web server and builds pages on the fly

When would you use servlets?


Page is based on user-submitted data e.g search engines Data changes frequently e.g. weather-reports Page uses information from a databases e.g. on-line stores

Introduction
Servlets are to the server what applets are to the browser Servlets extend the given environment with a powerful, portable, OO language. Servlets are a way to write business logic using Java, and deploy this business logic via thin clients. The Java servlet architecture provides an excellent framework for serverside processing. Servlets can take advantage of Java's memory management and rich set of APIs. Servlets can run on numerous platforms and HTTP servers without change. Servlets can do anything; they're just Java called by the HTTP server

Skills are Needed to Develop Servlets


Java

Including object-oriented skills Servlet APIs


javax.servlet.*; and javax.servlet.http.*; Add-ons if desired e.g session tracking JavaServer Pages If desired Web page flow forms, gets, posts, etc. HTML/XML

JavaScript
If desired
5

Servlet API
The Java Servlet API: Java Standard Extension Provides web application developers a simple consistent mechanism for extending the functionality of a web server.

Overview of Java Servlet API


Important Packages

javax.servlet Basic servlet definitions, e.g. What is a servlet what are the inputs and outputs ... Not tied to any specific protocol (e.g., HTTP) We don't use these low-level classes/interfaces often

javax.servlet.http
HTTP related definitions, extension of the basic interfaces to handle the HTTP protocol

You must learn this package well

Servlet API Continue


Servlet

javax.servlet.http
Interfaces HttpServletRequest HttpServletResponse HttpSession Classes
HttpServlet
Class extends

GenericServlet
Interface Class implements

HttpServlet
Cookie

doGet() doPost() service() ...

UserServlet
extends Class Class
Override one or more of... doGet() doPost() service() ...

Servlet Interface
The Servlet interface: Central abstraction in the Servlet API Specifies the contract between web container and servlet

All servlets implement this interface


directly or, by extending a class that implements it such as HttpServlet or GenricServlet Provides methods that manage the Servlet and its communications with clients

Servlet writers provide some or all of these methods while developing servlets
9

Servlet Interface Continue

When implementing javax.servlet.Servlet following five methods must be implemented public void intit(ServletConfig config) public void service(ServletRequest request,ServletResponse

response)
public void destroy() public ServletConfig getServletConfig()

public String getServletInfo()

10

GenricServlet Class

public abstract class GenricServlet implements Servlet, ServletConfig, Seraializable Provides basic implementation of Servlet Interface

This class has following three methods extra than Servlet interface

and SerletCofig
public init() public void log(String message)

public void log(String message,Throwable t)

11

ServletConfig interface
Provides a means to accessing the ServletConfig object associated with servlet This object provides access to cretin initialization parameters that can be configured while deploying servlet Methods in this interface public String getInitParameter(String name) public Enumeration getInitParameterNames() public ServletContext getServletContext() public void getServletName()

12

ServletContext

The notion of this interface is closely associated withnotion of web application Allows servlets in an application to share data Also provides methods by which servlet can access host web

container.

13

HttpServlet Class
public abstract class HttpServlet extends GenricServlet implements Serializable Provides a HTTP specific implementation of servlet interface This will most likely be the class that our servlet will extend

Methods in this class


public void service(ServletRequest req,ServletResponse res) protected void service(HttpServletRequest req,HttpServletResponse res) protected void doGet(HttpServletRequest req,HttpServletResponse res) protected void doPost(HttpServletRequest req,HttpServletResponse res) protected void doDelete(HttpServletRequest req,HttpServletResponse res) protected void doTrace(HttpServletRequest req,HttpServletResponse res) protected void doOptions(HttpServletRequest req,HttpServletResponse res) protected void doPut(HttpServletRequest req,HttpServletResponse res)

All the methods throws IOEXception and ServletException

14

ServletRequest and ServletResponse


Communication with the HTTP Server
Two objects are passed to the service(), doGet() and doPost() methods ServletRequest

HttpServletRequest for HttpServlets ServletResponse


HttpServletResponse for HttpServlets They encapsulate the data received from and sent to the client

Parameters passed by the client can be accessed using the methods of the ServletRequest
getInitParameter() getInitParameterNames()

getParameter()
getParameterNames getParameterValues() When multiple values can be returned for a single name (e.g., checkboxes)
15

ServletRequest and Response ContRequests


ServletRequest class
This class encapsulates the communication from the client to the server Allows the Servlet access to Names of the parameters passed in by the client The protocol being used by the client The names of the remote host that made the request and the server that received it

The input stream, ServletInputStream, through which the servlet gets data from clients that are using application protocols such as the HTTP POST and PUT methods
Subclasses of ServletRequest allow the servlet to retrieve more protocol-specific data Example: HttpServletRequest contains methods for accessing HTTP-specific header information

16

ServletRequest and ServletResponse ContResponses


ServletResponse class This class encapsulates the communication from the Servlet back to the client Gives the servlet methods for replying to the client Allows the servlet to set the content length and mime type of the reply Provides an output stream, ServletOutputStream, and a Writer through which the servlet can send the reply data Subclasses of ServletResponse give the servlet more protocolspecific capabilities. Example: HttpServletResponse contains methods that allow the servlet to manipulate HTTP-specific header information

17

SingleThreadedModel Interface

Marker interface Implement this interface to inform the container that it should make sure that only one thread is executing the servlet service() method at any given movement Resource intensive particularly if large no of concurrent requests are expected for servlet For SingleThreadedModel Containers may follow one of the following approach
Instance Pooling Request Serialization

Always try to avoid this model for performance implications


18

import java.io.*;

import javax.servlet.*;

Sample Servlet

//Apache Tomcat sample code

import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>");

out.println("<head>");
out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>");

}
}

Vous aimerez peut-être aussi