JavaServletsIntro - Capitol College Faculty Pages

indexadjustmentInternet and Web Development

Nov 13, 2013 (3 years and 6 months ago)

45 views

Java Servlets


A servlet is just a Java class that runs on a
server


A servlet (for a web application) extends the
HttpServlet abstract class


public class ServletClassName extends
HttpServlet


{



// code here


}

Java Servlets


The servlet is part of a package




we need to include a package statement
at the beginning of the file for the servlet
class


Code inside the Java class is Java code


No special tags like in a JSP




we need to import the appropriate
classes

Java Servlets: classes to import


Typically, we need to import the following
classes:


HttpServlet


HttpServletRequest


// JSP: remember the implicit object request


HttpServletResponse


// JSP: remember the implicit object
response


….

Java Servlets: classes to import





PrintWriter


// JSP: remember the implicit object out


IOException, ServletException


/* Some methods (which we will override)
of the HttpServlet class throw these two
exceptions */

Java Servlets


Classes to import



Class

Package

HttpServlet

javax.servlet.http

HttpServletRequest

javax.servlet.http

HttpServletResponse

javax.servlet.http

PrintWriter

java.io

ServletException

javax.servlet

IOException

java.io

Java Servlets


package packageName;


import javax.servlet.http.Servlet;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import java.io.PrintWriter;


import java.io.IOException;


import javax.servlet.ServletException;


// other import statements as necessary


public class ServletClassName extends HttpServlet


{



// code here


}

Java Servlets


package packageName;


// Using bulk import statements


import javax.servlet.http.*;


import java.io.*;


import javax.servlet.ServletException;


// other import statements as necessary


public class ServletClassName extends HttpServlet


{



// code here


}

Java Servlets


Our servlet extends the abstract class HttpServlet
(javax.servlet.http package)


HttpServlet extends the abstract class
GenericServlet (javax.servlet package)


GenericServlet extends Object (java.lang package)




A number of methods are inherited from these
classes by our servlet class