Académique Documents
Professionnel Documents
Culture Documents
Browser
HTTP Server
Network
Browser
Resources
Whats a Servlet?
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
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.
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
javax.servlet.http
Interfaces HttpServletRequest HttpServletResponse HttpSession Classes
HttpServlet
Class extends
GenericServlet
Interface Class implements
HttpServlet
Cookie
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
Servlet writers provide some or all of these methods while developing servlets
9
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()
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)
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
14
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
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
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
import java.io.*;
import javax.servlet.*;
Sample Servlet
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>");
}
}