![]()
shklar@cs.rutgers.eduLeon Shklar
![]()
Figure 1. Servlet invocation.
javax.servlet package Leon Shklar
![]()
HTTPServet or GenericServlet classes ServletContext object
import javax.servlet.*;
public class MyServlet
extends GenericServlet {
public void service (
ServletRequest request,
ServletResponse response
) throws ServletException, IOException {
ServletInputStream
in = request.getInputStream ();
ServletOutputStream
out = response.getOutputStream ();
...
}
}
Leon Shklar
![]()
RequestDispatcher - defines an object that receives client
requests and forwards them to a proper resource. Servlet - defines and object that extends functionalioty of a Web
server. ServletConfig - defines an object that controls servlet
configuration (unique for every servlet). ServletContext - defines an object that provides a servlet with
information about the environment. ServletRequest - defines an object that enables the servlet to
get data about a client request. ServletResponse - defines an object that enables the servlet to
respond to the client request. SingleThreadModel - empty interface, the purpose is to flag
servlets for sequential execution. GenericServlet - holds a reference to the ServletConfig
and ServletContext objects (created at initialization time), provides simple
implementations of the servlet lifecycle methods. ServletInputStream - input stream for reading clienbt requests. ServletOutputStream - abstract class, implemented by all servlet
engines, utilized to retrun data to clients. ServletException - thrown to indicate a servlet problem. UnavailableException - thrown to indicate servlet's unavailability. HttpServletRequest extends ServletRequest - defines an object
that enables the servlet to access http-specific request information from the client
request (e.g., getAuthType(), getCookies(), getHeader(String
name), getSession(), etc.) HttpServletResponse extends ServletResponse - defines an object
that represents an http response back to the client. HttpSession - provides an association between an HTTP client and
an HTTP session (implemented through cookies or URL rewriting). HttpSessionBindingListener - objects implement this interface if
they wish to be notified about being bound to or unbound from a session. HttpSessionContext - deprecated (originally introduced to define
common context for multiple sessions). Cookie - represents a Cookie as defined in RFC 2109. HttpServlet extends GenericSevlet - provides convenience in
iumplementing HTTP servlets (e.g., doGet(HttpServletRequest request,
HttpServletResponse response), doPost(HttpServletRequest request,
HttpServletResponse response), doPut(HttpServletRequest request,
HttpServletResponse response), etc.). HttpSessionBindingEvent - this event is communicated to a HttpSessionBindingListener.
HttpUtils - a collection of static utility methods useful to HTTP
servlets (e.g., parseQueryString(String s), etc.). Leon Shklar
![]()
Figure 2. Servlet Lifecycle.
init()
method to perform i/o-intensive setup once vs. per request (e.g., initializing access to
other network services or retrieving the state) service() call per request, class-static data may be used to share info
between requests destroy() method, after
which the class may become eligible for garbage collection Leon Shklar
![]()
Figure 3. Servlet security.
Leon Shklar
![]()
SERVLET tag <SERVLET NAME=ServletName> <PARAM NAME=param1 VALUE=val1> <PARAM NAME=param2 VALUE=val2> Alternative text </SERVLET>
Leon Shklar
![]()
Figure 4. Servlet performance.
CGI and FastCGI Leon Shklar
![]()
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* This is a simple example of an
* HTTP Servlet. It responds to the GET
* and HEAD methods of the HTTP protocol.
*/
public class SimpleServlet extends
HttpServlet {
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
ServletOutputStream out =
res.getOutputStream();
// set content type and other
// response header fields first
res.setContentType("text/html");
// then write the data of the response
out.println("<HEAD><TITLE>");
out.println("SimpleServlet Output");
out.println("</TITLE></HEAD><BODY>");
out.println("<h1>SimpleServlet Output</h1>");
out.println("<P>This is output from SimpleServlet.");
out.println("</BODY>");
out.close();
}
public String getServletInfo() {
return "A simple servlet";
}
}
import java.io.*;
import java.util.Date;
import java.util.Hashtable;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Date Servlet
*
* This is a simple servlet to
* demonstrate server-side include
* It returns a string representation
* of the current time.
*/
public class DateServlet extends
HttpServlet {
public void service(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
Date today = new Date();
res.setContentType("text/plain");
ServletOutputStream out =
res.getOutputStream();
out.println(today.toString());
}
public String getServletInfo() {
return "string representation of time";
}
}
Leon Shklar